I'm trying to learn how to make an app; my app crashes when I go to another activity, and then try to go back to my original activity. This is in my 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");
I know that when I tap back from the activity, savedInstanceState is not null; however, why does it crash when I try to get the string from the bundle? How can I fix this? I want the value of course and number to be the same when I continue the activity.
This is my full code:
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);
}
}
Log:
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)
note: this question is not a duplicate, I'm not trying to pass a result from an activity to the previous activity; I'm trying to preserve the state so that it's the same when I go back.