java - 如何将BroadcastReceiver中的Intent中的数据复制到Android中的新接收器中?

标签 java android

下面是代码:

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 复制到新的 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

最佳答案

看来DataSourceWrapper.getInstance()返回 null,因此在执行其他代码之前需要检查返回值是否不为 null:

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

请注意,您需要兑换 <ReturnClassName> 你的真实类(class)名称我不知道 DataSourceWrapper类。

关于java - 如何将BroadcastReceiver中的Intent中的数据复制到Android中的新接收器中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14444480/

相关文章:

Java按空格将字符串转换为字符串数组

Android - 在 ADB 工作时无法在 Ubuntu 上的 Eclipse 中安装/调试应用程序

java - 无法创建 PoolableConnectionFactory((SSL)加密。错误 : Signature algorithm mismatch".)

java - 如何在不扩展 fragment 类的情况下使用 getchildfragmentmanager ?

java - 如何使用Java注解来引导Android的Proguard?

android - 在 Titanium 模块中包含 Google Play 服务

android - 设计开关的样式

android - 当应用程序由于 intent 过滤器而启动时 onCreate() 是否仍然被调用?

java - I18n - JSF 变量值翻译

Java 将通用链表转换为通用数组