将数据从活动传递到BroadcastReceiver会导致NullPointerException

时间:2015-06-06 13:21:25

标签: android android-intent nullpointerexception broadcastreceiver android-pendingintent

我有一个BroadcastReceiver,它通过Intent从Activity获取数据, 与PendingIntent。

发送数据:

Intent intent = new Intent(addOne.this,AlarmReceiver.class);

intent.putExtra("msg", title.getText().toString());
intent.putExtra("note", note.getText().toString());
int id =...;

PendingIntent alarmIntent = PendingIntent.getBroadcast(getApplicationContext(), id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmMgr.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);

获取数据:

@Override
public void onReceive(Context context2, Intent intent) {

       String msg=intent.getStringExtra("msg");
       String title=intent.getStringExtra("msg");
       ...
}

当调用onReceive时,我得到一个NullPointerException。

我的BroadcastReceiver-manifest:

    <receiver android:name=".AlarmReceiver" android:enabled="true">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

1 个答案:

答案 0 :(得分:0)

将您的待处理意图设置为如下所示,然后尝试

PendingIntent alarmIntent = PendingIntent.getBroadcast(getApplicationContext(), id, intent, PendingIntent.FLAG_CANCEL_CURRENT);