I’m passing an object from activity to another. Then from that activity to another one.
When I try to access an attribute of the passed object from the 3rd activity, i get a null pointer exception.
I’ve set the attribute also. So i don’t understand the reason for it.
Code from the first activity:
private Task t;
public void onClick(View v) {
if(v == this.btnAdd){
this.addItem(this.txtTask.getText().toString());
this.addTask(this.txtTask.getText().toString());
}
}
private void addTask(String taskName){
if(taskName.length()>0){
t = new Task(taskName);
this.tasks.add(t);
}
}
public void onItemClick(AdapterView<?> parent, View view, int position,
long id3) {
Intent myIntent = new Intent(getApplicationContext(), TabSwitch.class);
myIntent.putExtra("taskItem", t);
startActivity(myIntent);
}
});
From the second activity:
private TextView selectedTask;
Intent i=getIntent();
Task taskItem = (Task) i.getSerializableExtra("taskItem");
i = new Intent().setClass(this, Info.class);
i.putExtra("taskItem", taskItem);
From the 3rd activity:
Intent i=getIntent();
Task task =(Task) i.getSerializableExtra("taskItem");
selectedTask.setText(task.getTaskName());
Here is the log:
02-13 22:03:02.868: E/AndroidRuntime(572): FATAL EXCEPTION: main
02-13 22:03:02.868: E/AndroidRuntime(572): java.lang.RuntimeException: Unable to start activity ComponentInfo{sam.todo.OnTime/sam.todo.OnTime.TabSwitch}: java.lang.RuntimeException: Unable to start activity ComponentInfo{sam.todo.OnTime/sam.todo.OnTime.Info}: java.lang.NullPointerException
02-13 22:03:02.868: E/AndroidRuntime(572): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-13 22:03:02.868: E/AndroidRuntime(572): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-13 22:03:02.868: E/AndroidRuntime(572): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-13 22:03:02.868: E/AndroidRuntime(572): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-13 22:03:02.868: E/AndroidRuntime(572): at android.os.Handler.dispatchMessage(Handler.java:99)
02-13 22:03:02.868: E/AndroidRuntime(572): at android.os.Looper.loop(Looper.java:123)
02-13 22:03:02.868: E/AndroidRuntime(572): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-13 22:03:02.868: E/AndroidRuntime(572): at java.lang.reflect.Method.invokeNative(Native Method)
02-13 22:03:02.868: E/AndroidRuntime(572): at java.lang.reflect.Method.invoke(Method.java:507)
02-13 22:03:02.868: E/AndroidRuntime(572): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-13 22:03:02.868: E/AndroidRuntime(572): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-13 22:03:02.868: E/AndroidRuntime(572): at dalvik.system.NativeStart.main(Native Method)
02-13 22:03:02.868: E/AndroidRuntime(572): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{sam.todo.OnTime/sam.todo.OnTime.Info}: java.lang.NullPointerException
02-13 22:03:02.868: E/AndroidRuntime(572): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-13 22:03:02.868: E/AndroidRuntime(572): at android.app.ActivityThread.startActivityNow(ActivityThread.java:1487)
02-13 22:03:02.868: E/AndroidRuntime(572): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
02-13 22:03:02.868: E/AndroidRuntime(572): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
02-13 22:03:02.868: E/AndroidRuntime(572): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:654)
02-13 22:03:02.868: E/AndroidRuntime(572): at android.widget.TabHost.setCurrentTab(TabHost.java:326)
02-13 22:03:02.868: E/AndroidRuntime(572): at android.widget.TabHost.addTab(TabHost.java:216)
02-13 22:03:02.868: E/AndroidRuntime(572): at sam.todo.OnTime.TabSwitch.onCreate(TabSwitch.java:30)
02-13 22:03:02.868: E/AndroidRuntime(572): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-13 22:03:02.868: E/AndroidRuntime(572): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-13 22:03:02.868: E/AndroidRuntime(572): ... 11 more
02-13 22:03:02.868: E/AndroidRuntime(572): Caused by: java.lang.NullPointerException
02-13 22:03:02.868: E/AndroidRuntime(572): at sam.todo.OnTime.Info.onCreate(Info.java:34)
02-13 22:03:02.868: E/AndroidRuntime(572): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-13 22:03:02.868: E/AndroidRuntime(572): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-13 22:03:02.868: E/AndroidRuntime(572): ... 20 more
Does your Activity maintain its life cycle properly? Looks like not. If you leave, say, activity A in favor of activity B (so the stack of activities is A > B), then once activity A becomes invisible the OS may decide to kill it (there are hacks to prevent it, but those are hacks, so you better avoid them). So when you come back to the activity A from B, the OS will restore the activity A for you (however it’s you who is responsible for proper activity state maintaining – the Activity API has special callbacks for that – persist/restore your tasks at those points). Here is the official tutorial on this: Managing the Activity Lifecycle.