Reputation: 3828
Let me explain the structure of my application, In my app i have 3 main tabs implemented using Fragments. And one of the main tab contains 3 sub tabs in its fragment, and one sub tab contains listview. Now i want to start an Activity when an item from the list is selected.
Here is onItemClickListener for the listview:
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View view, int position,long id)
{
RecordObjects obj=(RecordObjects)lv.getItemAtPosition(position);
String name=obj.getName();
String url=obj.getUrl();
intent=new Intent(getActivity(),player.class);
intent.putExtra("task","Draft");
intent.putExtra("name",name);
intent.putExtra("read_path",url);
startActivityForResult(intent, 0);
}
});
And the error i got when i click on list item is:
05-09 16:19:07.723: D/PhoneWindow(7043): couldn't save which view has focus because the focused view android.widget.LinearLayout@44ecc1d8 has no id.
05-09 16:19:07.741: D/AndroidRuntime(7043): Shutting down VM
05-09 16:19:07.741: W/dalvikvm(7043): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
05-09 16:19:07.751: E/AndroidRuntime(7043): Uncaught handler: thread main exiting due to uncaught exception
05-09 16:19:07.771: E/AndroidRuntime(7043): java.lang.RuntimeException: Unable to pause activity {com.m2.smartGui/com.m2.smartGui.smartGuiPagerFragmentActivity}: java.lang.NullPointerException
05-09 16:19:07.771: E/AndroidRuntime(7043): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3162)
05-09 16:19:07.771: E/AndroidRuntime(7043): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3119)
05-09 16:19:07.771: E/AndroidRuntime(7043): at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3102)
05-09 16:19:07.771: E/AndroidRuntime(7043): at android.app.ActivityThread.access$2400(ActivityThread.java:119)
05-09 16:19:07.771: E/AndroidRuntime(7043): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1870)
05-09 16:19:07.771: E/AndroidRuntime(7043): at android.os.Handler.dispatchMessage(Handler.java:99)
05-09 16:19:07.771: E/AndroidRuntime(7043): at android.os.Looper.loop(Looper.java:123)
05-09 16:19:07.771: E/AndroidRuntime(7043): at android.app.ActivityThread.main(ActivityThread.java:4363)
05-09 16:19:07.771: E/AndroidRuntime(7043): at java.lang.reflect.Method.invokeNative(Native Method)
05-09 16:19:07.771: E/AndroidRuntime(7043): at java.lang.reflect.Method.invoke(Method.java:521)
05-09 16:19:07.771: E/AndroidRuntime(7043): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-09 16:19:07.771: E/AndroidRuntime(7043): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-09 16:19:07.771: E/AndroidRuntime(7043): at dalvik.system.NativeStart.main(Native Method)
05-09 16:19:07.771: E/AndroidRuntime(7043): Caused by: java.lang.NullPointerException
05-09 16:19:07.771: E/AndroidRuntime(7043): at android.support.v4.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:1576)
05-09 16:19:07.771: E/AndroidRuntime(7043): at android.support.v4.app.FragmentManagerImpl.saveAllState(FragmentManager.java:1617)
05-09 16:19:07.771: E/AndroidRuntime(7043): at android.support.v4.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:481)
05-09 16:19:07.771: E/AndroidRuntime(7043): at com.m2.smartGui.smartGuiPagerFragmentActivity.onSaveInstanceState(smartGuiPagerFragmentActivity.java:78)
05-09 16:19:07.771: E/AndroidRuntime(7043): at android.app.Activity.performSaveInstanceState(Activity.java:1022)
05-09 16:19:07.771: E/AndroidRuntime(7043): at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1180)
05-09 16:19:07.771: E/AndroidRuntime(7043): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3144)
05-09 16:19:07.771: E/AndroidRuntime(7043): ... 12 more
What is the error here? Or is it possible to start an activity from fragments? If No, what is the best practice?
Upvotes: 0
Views: 2064
Reputation: 16393
According to the documentation, the SavedInstanceState bundle is created by saving the views and data from all views/widgets with an id attached. It sounds like Che Jami is right and there is a bug when trying to save the bundle and a view/widget has focus but no id.
Simple fix would be to add an id to that LinearLayout for your listview row.
Upvotes: 5