gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 11:48:24 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com