2013-02-20 41 views
0

我已經閱讀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; 
}} 

當我運行應用程序並打開地圖時,它很好,但最終崩潰時,手機剛剛坐在那裏,我不知道它在做什麼,也許這是與位置監聽器有關?我非常感謝幫助解決這個問題,因爲我在這一點上毫無頭緒。

+0

你嘗試使用'applicationContext'在您註冊'PendingIntent'?你也可以嘗試使用'PendingIntent.getActivity(背景下,0,NULL,0);'和'PendingIntent.FLAG_UPDATE_CURRENT'標誌 – iTech 2013-02-20 18:36:39

+0

我很抱歉,我居然不知道該怎麼回答你的問題,我不知道ApplicationContext需要什麼。你能更詳細地解釋一下你認爲我應該嘗試一下嗎?謝謝。 – deucalion0 2013-02-20 18:44:44

回答

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,但是你傳遞一個null意圖。你想要這行代碼做什麼?

+0

我不知道我想要它做什麼,我一直在從我從多個資源收集的代碼構建這個應用程序,現在我完全失去了。對不起,我不能給你更多的信息。 – deucalion0 2013-02-20 18:45:50

+0

當您收到您的鄰近意圖或僅顯示通知時,您是否想要開始一個活動? – Sam 2013-02-20 18:54:38

+0

只需顯示通知將是完美的!謝謝!! – deucalion0 2013-02-20 18:56:38

2

在ProximityIntentReceiver - 變線

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

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

您還應該補充說明爲什麼應該採取這種行動以及爲什麼您所描述的解決方案可以提供幫助。 – 2013-04-16 15:46:32