java - 接收器到警报 Activity 包返回 null,即使分配了默认包值

标签 java android android-intent android-bundle

我试图将字符串从广播接收器传递到警报 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/

相关文章:

java - 将 ant build.xml 导入 Eclipse 时显示 "javac"任务在目标 "compile"中未找到

java - Android - 在单独的线程中从 for 循环更新进度条

java - 使用 @Where 子句的 Hibernate @OneToOne 映射

java - 我如何知道 Android 使用的图像的像素密度版本是什么?

android - onOptionsItemSelected 问题

android - 如何在 Web View 或 JavaScript 中向 Camera Intent 或 Gallery Intent 添加裁剪功能

java - Java程序中如何获取环境变量(被外部修改)的修改值?

android - 获取 EditTextPreference 值

android - 如何设置edittext以显示搜索按钮或键盘上的输入按钮?

java - 使用Intent打开本地PDF文件