私はこれに関する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 com.example.newmaps.ProximityIntentReceiver@41550140
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
Mapクラス内のコードは、ロケーションリスナーとともに近接アラートを作成します。
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) {
}
}
Proximityクラスは次のとおりです。
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;
}}
アプリケーションを実行して地図を開くと問題ありませんが、電話がちょうどそこにあるときに最終的にクラッシュします。それが何をしているのかわかりません。おそらく、ロケーションリスナーと関係がありますか?私はこの時点で無知なので、これを修正するためにいくつかの助けをいただければ幸いです。