0

我已经阅读了几乎所有关于 SO 的帖子,并尝试了所有我认为它们是相对的解决方案,但我仍然不知道为什么会发生这种情况。这是日志猫:

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;
}}

当我运行应用程序并打开地图时它很好,但最终在手机坐在那里时崩溃了,我不知道它在做什么,也许它与位置监听器有关?我将非常感谢一些帮助来解决这个问题,因为我现在一无所知。

4

2 回答 2

2

在 ProximityIntentReceiver - 更改行

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, null, 0);   

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); 
于 2013-04-16T15:24:36.567 回答
1

来自 LogCat:

Caused by: java.lang.NullPointerException
    at android.app.PendingIntent.getActivity(PendingIntent.java:195)
    at com.example.newmaps.ProximityIntentReceiver.onReceive(ProximityIntentReceiver.java:36)

当您致电时,问题似乎与此行有关getActivity()

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, null, 0);

这令人困惑。PendingIntent 的目的是启动一个组件,在本例中是一个 Activity,但您传递的是一个nullIntent。你想让这行代码做什么?

于 2013-02-20T18:38:00.047 回答