我正在努力学习如何制作应用;当我去另一个活动时,我的应用程序崩溃,然后尝试回到我原来的活动。这是我的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不为null;但是,当我尝试从捆绑中获取字符串时,为什么会崩溃?我怎样才能解决这个问题?当我继续活动时,我希望课程和数字的价值相同。
这是我的完整代码:
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 ComponentInfo{com.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)
注意:这个问题不重复,我不是试图将活动的结果传递给上一个活动;我正试图保留这个状态,这样当我回去时就会一样。
答案 0 :(得分:0)
将您的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);
}
答案 1 :(得分:0)
您的代码中是否有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"
尝试测试课程和数字变量的初始化:
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");
}
从您的代码中,我不确定您的课程和编号应该从哪里分配(listview等),但这些需要在某个时刻分配 - 代码当前放置当前值(即使为null进入额外的。