2013-01-21 13 views
0

這裏是以下代碼:如何從Android中的新版本中的BroadcastReceiver中的Intent中複製數據?

public class NotificationsReceiver extends BroadcastReceiver { 
    public static final String INTENT_EXTRA_NOTIFICATION_INFO="intent_extra_notification_info"; 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     NotificationInfo info=(NotificationInfo)intent.getSerializableExtra(INTENT_EXTRA_NOTIFICATION_INFO); 
     NotificationTextInfo fields=DataSourceWrapper.getInstance().getNotificationTextInfo(info); 
     NotificationManager manager=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); 
     Notification notification=new Notification(android.R.drawable.alert_dark_frame, "1", info.getId()); 
     notification.when=new Date().getTime(); 
     notification.setLatestEventInfo(context, fields.getTitle(), fields.getDescription(), null); 
     manager.notify((int)info.getId(), notification); 
    } 
} 

BroadcastReceiver存在AlarmManager。 當它第一次工作的時候一切都很好,但是當它第二次執行時,當我從Intent得到一些信息時,它會連續顯示一個NullPointerException,因此Intent在第一次執行後將會清除。

現在我的問題是:如何將Intent的數據複製到新的數據中以修復NullPointerException

的logcat:

01-23 17:00:00.578: E/receiver(8442): receive 
01-23 17:00:00.608: E/AndroidRuntime(8442): FATAL EXCEPTION: main 
01-23 17:00:00.608: E/AndroidRuntime(8442): java.lang.RuntimeException: Unable to start receiver com.ulnda.mypsych.receivers.NotificationsReceiver: java.lang.NullPointerException 
01-23 17:00:00.608: E/AndroidRuntime(8442):  at android.app.ActivityThread.handleReceiver(ActivityThread.java:2408) 
01-23 17:00:00.608: E/AndroidRuntime(8442):  at android.app.ActivityThread.access$1500(ActivityThread.java:139) 
01-23 17:00:00.608: E/AndroidRuntime(8442):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) 
01-23 17:00:00.608: E/AndroidRuntime(8442):  at android.os.Handler.dispatchMessage(Handler.java:99) 
01-23 17:00:00.608: E/AndroidRuntime(8442):  at android.os.Looper.loop(Looper.java:154) 
01-23 17:00:00.608: E/AndroidRuntime(8442):  at android.app.ActivityThread.main(ActivityThread.java:4945) 
01-23 17:00:00.608: E/AndroidRuntime(8442):  at java.lang.reflect.Method.invokeNative(Native Method) 
01-23 17:00:00.608: E/AndroidRuntime(8442):  at java.lang.reflect.Method.invoke(Method.java:511) 
01-23 17:00:00.608: E/AndroidRuntime(8442):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
01-23 17:00:00.608: E/AndroidRuntime(8442):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
01-23 17:00:00.608: E/AndroidRuntime(8442):  at dalvik.system.NativeStart.main(Native Method) 
01-23 17:00:00.608: E/AndroidRuntime(8442): Caused by: java.lang.NullPointerException 
01-23 17:00:00.608: E/AndroidRuntime(8442):  at com.ulnda.mypsych.db.DataSourceWrapper.getNotificationTextInfo(DataSourceWrapper.java:68) 
01-23 17:00:00.608: E/AndroidRuntime(8442):  at com.ulnda.mypsych.receivers.NotificationsReceiver.onReceive(NotificationsReceiver.java:27) 
01-23 17:00:00.608: E/AndroidRuntime(8442):  at android.app.ActivityThread.handleReceiver(ActivityThread.java:2397) 
01-23 17:00:00.608: E/AndroidRuntime(8442):  ... 10 more 
+0

請添加logcat輸出。 – rekire

+0

請檢查更新 – user1841247

回答

1

似乎DataSourceWrapper.getInstance()返回null,所以你需要檢查返回值執行另外的代碼之前不爲空:

<ReturnClassName> instance = DataSourceWrapper.getInstance(); 
if(instance != null) { 
    NotificationTextInfo fields = instance.getNotificationTextInfo(info); 
    // ... 

請注意,您需要交換<ReturnClassName>與你真正的班級名稱我不知道DataSourceWrapper類。

+0

但它是有趣的,如果你谷歌的DataSourceWrapper和Android這個問題是第二個結果。 – rekire

相關問題