让我解释一下我的应用程序的结构,在我的应用程序中,我有 3 主要标签使用片段实施。和主要标签之一 在其片段中包含 3个子标签,并且包含一个子标签 的列表视图即可。现在,我希望在列表中的项目时启动活动 被选中。
这是列表视图的onItemClickListener:
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);
}
});
我点击列表项时得到的错误是:
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
这里有什么错误?或者是否可以从片段开始活动?如果不是,最佳做法是什么?
答案 0 :(得分:5)
根据文档,SavedInstanceState包是通过保存附加了id的所有视图/小部件中的视图和数据来创建的。这听起来像Che Jami是正确的,并且在尝试保存包时存在错误,并且视图/窗口小部件具有焦点但没有id。
简单的解决方法是为listview行添加一个id到LinearLayout。