我已經閱讀SO對此幾乎每一個崗位,並試圖所有的解決方案,我覺得他們是相對的,當接收廣播的意圖,但我仍然不知道爲什麼會這樣。這裏是logcat的:錯誤傳遞多個點
02-20 18:10:27.959: D/ProximityIntentReceiver(17636): entering receiver
02-20 18:10:27.959: W/dalvikvm(17636): threadid=1: thread exiting with uncaught exception (group=0x40adf9f0)
02-20 18:10:27.959: E/AndroidRuntime(17636): FATAL EXCEPTION: main
02-20 18:10:27.959: E/AndroidRuntime(17636): java.lang.RuntimeException: Error receiving broadcast Intent { act=com.javacodegeeks.android.lbs.ProximityAlert flg=0x10 (has extras) } in [email protected]
02-20 18:10:27.959: E/AndroidRuntime(17636): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:737)
02-20 18:10:27.959: E/AndroidRuntime(17636): at android.os.Handler.handleCallback(Handler.java:605)
02-20 18:10:27.959: E/AndroidRuntime(17636): at android.os.Handler.dispatchMessage(Handler.java:92)
02-20 18:10:27.959: E/AndroidRuntime(17636): at android.os.Looper.loop(Looper.java:137)
02-20 18:10:27.959: E/AndroidRuntime(17636): at android.app.ActivityThread.main(ActivityThread.java:4424)
02-20 18:10:27.959: E/AndroidRuntime(17636): at java.lang.reflect.Method.invokeNative(Native Method)
02-20 18:10:27.959: E/AndroidRuntime(17636): at java.lang.reflect.Method.invoke(Method.java:511)
02-20 18:10:27.959: E/AndroidRuntime(17636): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817)
02-20 18:10:27.959: E/AndroidRuntime(17636): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
02-20 18:10:27.959: E/AndroidRuntime(17636): at dalvik.system.NativeStart.main(Native Method)
02-20 18:10:27.959: E/AndroidRuntime(17636): Caused by: java.lang.NullPointerException
02-20 18:10:27.959: E/AndroidRuntime(17636): at android.app.PendingIntent.getActivity(PendingIntent.java:195)
02-20 18:10:27.959: E/AndroidRuntime(17636): at com.example.newmaps.ProximityIntentReceiver.onReceive(ProximityIntentReceiver.java:36)
02-20 18:10:27.959: E/AndroidRuntime(17636): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:728)
02-20 18:10:27.959: E/AndroidRuntime(17636): ... 9 more
這裏是我的地圖類中的代碼,我與位置監聽器一起打造一個接近警報:
private void addProximityAlert(Double latitude, Double longitude, String poiName) {
Bundle extras = new Bundle();
extras.putString("name", poiName);
extras.putInt("id", requestCode);
Intent intent = new Intent(PROX_ALERT_INTENT);
intent.putExtra(PROX_ALERT_INTENT, extras);
PendingIntent proximityIntent = PendingIntent.getBroadcast(Map.this, requestCode , intent, PendingIntent.FLAG_CANCEL_CURRENT);
lm.addProximityAlert(
latitude, // the latitude of the central point of the alert region
longitude, // the longitude of the central point of the alert region
POINT_RADIUS, // the radius of the central point of the alert region, in meters
PROX_ALERT_EXPIRATION, // time for this proximity alert, in milliseconds, or -1 to indicate no expiration
proximityIntent // will be used to generate an Intent to fire when entry to or exit from the alert region is detected
);
requestCode++;
IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT);
registerReceiver(new ProximityIntentReceiver(), filter);
Toast.makeText(getApplicationContext(),"Alert Added"+requestCode,Toast.LENGTH_SHORT).show();
}
public class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
Toast.makeText(Map.this, "Distance from Point:", Toast.LENGTH_LONG).show();
}
public void onStatusChanged(String s, int i, Bundle b) {
}
public void onProviderDisabled(String s) {
}
public void onProviderEnabled(String s) {
}
}
這裏是接近類:
public class ProximityIntentReceiver extends BroadcastReceiver {
private static final int NOTIFICATION_ID = 1000;
@Override
public void onReceive(Context context, Intent intent) {
String key = LocationManager.KEY_PROXIMITY_ENTERING;
Boolean entering = intent.getBooleanExtra(key, false);
if (entering) {
Log.d(getClass().getSimpleName(), "entering receiver");
}
else {
Log.d(getClass().getSimpleName(), "exiting");
}
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, null, 0);
Notification notification = createNotification();
notification.setLatestEventInfo(context,
"Proximity Alert!", "You are approaching: " +intent.getBundleExtra("deucalion0.ProximityAlert.").get("name"), pendingIntent);
//here-------------------------------------
notificationManager.notify(intent.getBundleExtra("deucalion0.ProximityAlert.").getInt("id"), notification);
}
private Notification createNotification() {
Notification notification = new Notification();
notification.icon = R.drawable.ic_launcher;
notification.when = System.currentTimeMillis();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.ledARGB = Color.WHITE;
notification.ledOnMS = 300;
notification.ledOffMS = 1500;
return notification;
}}
當我運行應用程序並打開地圖時,它很好,但最終崩潰時,手機剛剛坐在那裏,我不知道它在做什麼,也許這是與位置監聽器有關?我非常感謝幫助解決這個問題,因爲我在這一點上毫無頭緒。
你嘗試使用'applicationContext'在您註冊'PendingIntent'?你也可以嘗試使用'PendingIntent.getActivity(背景下,0,NULL,0);'和'PendingIntent.FLAG_UPDATE_CURRENT'標誌 – iTech 2013-02-20 18:36:39
我很抱歉,我居然不知道該怎麼回答你的問題,我不知道ApplicationContext需要什麼。你能更詳細地解釋一下你認爲我應該嘗試一下嗎?謝謝。 – deucalion0 2013-02-20 18:44:44