0

次のコードは次のとおりです。

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ます。初めて動作するときはすべて問題ありませんが、2回目に実行すると、NullPointerExceptionから情報を取得すると が連続して表示されるIntentためIntent、最初の実行後に明確になります。

Intent今私の質問は次のとおりです。修正するためにデータを新しいものにコピーするにはどうすればよいNullPointerExceptionですか?

ログキャット:

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
4

1 に答える 1

1

nullを返すように見えるDataSourceWrapper.getInstance()ので、他のコードを実行する前に、戻り値が null でないかどうかを確認する必要があります。

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

<ReturnClassName>そのクラスを知らない本当のクラス名と交換する必要があることに注意してくださいDataSourceWrapper

于 2013-01-21T18:17:27.457 に答える