当我尝试从 savedInstanceState 获取额外内容时,应用程序崩溃了
Posted
技术标签:
【中文标题】当我尝试从 savedInstanceState 获取额外内容时,应用程序崩溃了【英文标题】:App is crashing when I try to get extras from savedInstanceState when it's not null 【发布时间】:2016-01-11 23:57:36 【问题描述】:我正在尝试学习如何制作应用程序;当我去另一个活动时,我的应用程序崩溃了,然后尝试回到我原来的活动。这是在我的 oncreate 中:
Bundle bundle;
if (savedInstanceState != null)
bundle = savedInstanceState;
else
bundle = getIntent().getExtras();
course = bundle.getString("com.naomi.classAlert.course");
number = bundle.getString("com.naomi.classAlert.number");
我知道当我从活动中点击返回时,savedInstanceState 不为空;但是,为什么当我尝试从包中获取字符串时它会崩溃?我怎样才能解决这个问题?当我继续活动时,我希望 course 和 number 的值相同。
这是我的完整代码:
public class classList extends AppCompatActivity
ListView listView;
SimpleCursorAdapter mAdapter;
static private String[] classes = "class1","class2";
String course;
String number;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
Bundle bundle;
if (savedInstanceState != null)
bundle = savedInstanceState;
else
bundle = getIntent().getExtras();
course = bundle.getString("com.naomi.classAlert.course");
number = bundle.getString("com.naomi.classAlert.number");
Toast.makeText(getApplicationContext(), course+number,
Toast.LENGTH_LONG).show();
setContentView(R.layout.activity_class_list);
listView = (ListView) findViewById(R.id.list);
// ALL THE DIFFERENT SECTIONS
String[] values = new String[]
course+number+" 001", course+number+" 002", course+number+" 003"
;
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_2, android.R.id.text1, values);
listView.setAdapter(adapter);
// ListView Item Click Listener
listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
// ListView Clicked item index
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) listView.getItemAtPosition(position);
// Show Alert
Toast.makeText(getApplicationContext(),
"Position :"+ itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG)
.show();
Intent intent1 = new Intent(classList.this, pickSection.class);
intent1.putExtra("com.naomi.classAlert.classList.course",course);
intent1.putExtra("com.naomi.classAlert.classList.number", number);
intent1.putExtra("com.naomi.classAlert.classList.section",position+1);
startActivity(intent1);
);
@Override
public void onSaveInstanceState(Bundle outState)
super.onSaveInstanceState(outState);
outState.putString("com.naomi.classAlert.course", course);
outState.putString("com.naomi.classAlert.number", number);
日志:
10-14 00:16:08.411 19529-19529/com.example.naomikoo.classalert D/OpenGLRenderer﹕ Enabling debug mode 0
10-14 00:16:13.325 19529-19532/com.example.naomikoo.classalert D/dalvikvm﹕ GC_CONCURRENT freed 125K, 2% free 9350K/9504K, paused 4ms+3ms, total 29ms
10-14 00:16:15.254 19529-19529/com.example.naomikoo.classalert D/AndroidRuntime﹕ Shutting down VM
10-14 00:16:15.254 19529-19529/com.example.naomikoo.classalert W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41d3c930)
10-14 00:16:15.262 19529-19529/com.example.naomikoo.classalert E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.naomikoo.classalert/com.example.naomikoo.classalert.classList: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5039)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.naomikoo.classalert.classList.onCreate(classList.java:39)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5039)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
注意:这个问题不是重复的,我不是想将一个活动的结果传递给前一个活动;我正在尝试保留状态,以便在我回去时它是相同的。
【问题讨论】:
How to manage `startActivityForResult` on Android?的可能重复 你想达到什么目的?保存活动的状态? @NikoYuwono 我正在尝试保存和加载活动的状态,以便当我去另一个活动然后返回时,“课程”和“数字”仍然具有相同的值 你在哪里保存课程和号码到捆绑包中?你重写了onSaveInstanceState
方法吗?
@AkashSingh 是的,我已经覆盖了 onSaveInstanceState 方法;我认为我正在那里保存课程和编号(方法在代码底部)
【参考方案1】:
将您的 onSaveInstanceState
更改为以下内容:
@Override
public void onSaveInstanceState(Bundle outState)
outState.putString("com.naomi.classAlert.course", course);
outState.putString("com.naomi.classAlert.number", number);
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(outState);
【讨论】:
不幸的是,它没有帮助;当我尝试从 onCreate 中的包中获取字符串时,我的应用程序仍然崩溃 你能发布你的日志吗?bundle = getIntent().getExtras();
语句在 else 条件下有什么作用?
如果捆绑包为空,那么我会得到意图中的所有额外内容;我不认为这部分有问题,因为当我第一次进入屏幕时它工作正常。只有当我回到那个屏幕时它才会崩溃
你能发布classList
活动是如何从它的开始活动开始的吗?【参考方案2】:
您的代码中有 onRestoreInstanceState 吗?不确定您是否已完整发布。
@Override
public void onRestoreInstanceState(Bundle savedInstanceState)
super.onRestoreInstanceState(savedInstanceState);
course = savedInstanceState.getString("com.naomi.classAlert.course");
number = savedInstanceState.getString("com.naomi.classAlert.number");
Toast.makeText(getApplicationContext(), "Course: " + course + " ", number: " + number, Toast.LENGTH_LONG).show();
我还建议定义几个常量并使用它们来代替
public static final String COURSENAME = "com.naomi.classAlert.course"
public static final String COURSENUMBER = "com.naomi.classAlert.number"
尝试测试 course 和 number 变量的初始化:
String course = "No Class";
String number = "99999";
也试试这个
if (savedInstanceState != null)
bundle = savedInstanceState;
course = bundle.getString("com.naomi.classAlert.course");
number = bundle.getString("com.naomi.classAlert.number");
从您的代码中,我不确定您的课程和编号应该从哪里分配(列表视图等),但这些需要在某些时候分配 - 代码当前将当前值(即使为 null)放入额外的。
【讨论】:
那是我的全部代码,但我尝试添加 onRestoreInstanceState;不幸的是,我仍然遇到崩溃:/ 按照我的阅读方式,除了通过最初为空的 Bundle 之外,我看不到课程或编号正在初始化。您可以尝试在 If 条件内移动 course = bundle.getString 和 number=bundle.getString - 在 bundle = savedInstanceState; 之后这需要为课程和编号分配值,因为我没有看到它们在您的代码中被分配值(例如,来自列表视图、默认值等)。在声明它们以验证是否放置了其他东西时尝试为它们分配一个默认值在那里。以上是关于当我尝试从 savedInstanceState 获取额外内容时,应用程序崩溃了的主要内容,如果未能解决你的问题,请参考以下文章
什么是 onCreate(Bundle savedInstanceState)
Android savedInstanceState 总是返回 null
如何在不发送 savedInstanceState 的情况下启动新的 Android 活动?