gpt4 book ai didi

java - 执行savedInstanceState.containsKey()时崩溃

转载 作者:行者123 更新时间:2023-12-02 12:32:29 27 4
gpt4 key购买 nike

使用此代码时出现崩溃:

if (!savedInstanceState.containsKey("mybool")) {
// my code
}

我的onCreate():

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);}

我的代码中的其他地方:

savedInstanceState.putBoolean("mybool", true);  

最佳答案

savedInstanceState 首次启动 Activity 时将为 null

重新创建 Activity 后,

savedInstanceState不会null(例如,由于配置更改,系统会终止应用程序的进程) .

@Override
protected void onCreate(Bundle savedInstanceState) {
if (null == savedInstanceState) {
// activity is being launched first time
} else {
// is called after configuration change or framework restore your app
}
}

关于java - 执行savedInstanceState.containsKey()时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45233784/

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