传递多个点时接收广播Intent时出错

时间:2013-02-20 18:22:28

标签: android android-intent android-broadcast

我几乎阅读了关于此的所有帖子,并尝试了所有解决方案,我觉得他们是相对的,但我仍然不知道为什么会这样。这是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;
}}

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

2 个答案:

答案 0 :(得分:2)

在ProximityIntentReceiver中 - 更改行

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

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

答案 1 :(得分: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的目的是启动一个Component,在本例中是一个Activity,但是你传递的是null Intent。您希望这行代码做什么?