我试图将字符串从广播接收器传递到警报 Activity ,但我不断收到空指针异常。
我检查了广播接收器中的值,与警报 Intent bundle 的字符串不为空。
我将字符串附加到 Intent 并启动警报 Activity Intent ,如下所示:
@Override
public void onReceive(Context context, Intent intent) {
Intent normalReminderIntent = new Intent(context.getApplicationContext(), ReminderAlarmActivity.class);
// Bundle information and add to the intent for use in the alarm notification
Bundle extras = new Bundle();
extras.putString("Title", title);
extras.putString("Description", description);
intent.putExtras(extras);
// Start the reminder activity
context.getApplicationContext().startActivity(normalReminderIntent);
}
然后在警报 Activity 中,我做了 Intent 获取额外内容,甚至设置了默认值,但我仍然得到空指针。我这样做是这样的:
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
setTheme(R.style.AppTheme_Dialog);
super.onCreate(savedInstanceState);
setContentView(R.layout.alarm_activity_reminder);
// Get the bundled info for title and description
Intent intent = getIntent();
String title = (String) intent.getExtras().getString("Title", "");
String description = (String) intent.getExtras().getString("Description", "");
}
看看 getString 方法,我不明白为什么我会得到一个空指针,而不是默认值:
public String getString(String key, String defaultValue) {
throw new RuntimeException("Stub!");
}
在这里搜索了很多问题后,我不确定为什么我会得到一个空指针。
预先感谢您的帮助
最佳答案
在onReceive中使用normalReminderIntent.putExtras
代替intent.putExtras
关于java - 接收器到警报 Activity 包返回 null,即使分配了默认包值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48048396/