保留片段实例时removeAllViews()错误

时间:2014-06-17 18:40:25

标签: android

我正在尝试在方向更改时保留我的片段实例。我在片段中使用了以下代码。

private View fragmentLayoutView;
private ListView myListView;

@Override
public void onCreate(Bundle savedInstanceState) {
    ...
    setRetainInstance(true);
    ...
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){

    if(fragmentLayoutView == null){
        fragmentLayoutView = inflater.inflate(R.layout.myfragmentLayout,container,false);
    }
    else{
        ((ViewGroup) myListView.getParent()).removeAllViews();
    }
    return fragmentLayoutView;
    }

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    myListView = (ListView) getActivity().findViewById(R.id.listview1);
    myCustomCursorAdaptor = new MyCustomCursorAdaptor(getActivity());
    myListView.setAdapter(myCustomCursorAdaptor);
    ...
    }

但是onCreateView()返回后我收到错误。 removeAllViews()行似乎正在产生问题。

Logcat:

06-17 23:57:08.631: E/AndroidRuntime(1149): FATAL EXCEPTION: main
06-17 23:57:08.631: E/AndroidRuntime(1149): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp.www/com.mypackage.myActionBarActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2362)
06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3895)
06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.app.ActivityThread.access$800(ActivityThread.java:168)
06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1335)
06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.os.Looper.loop(Looper.java:137)
06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.app.ActivityThread.main(ActivityThread.java:5493)
06-17 23:57:08.631: E/AndroidRuntime(1149):     at java.lang.reflect.Method.invokeNative(Native Method)
06-17 23:57:08.631: E/AndroidRuntime(1149):     at java.lang.reflect.Method.invoke(Method.java:525)
06-17 23:57:08.631: E/AndroidRuntime(1149):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
06-17 23:57:08.631: E/AndroidRuntime(1149):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
06-17 23:57:08.631: E/AndroidRuntime(1149):     at dalvik.system.NativeStart.main(Native Method)
06-17 23:57:08.631: E/AndroidRuntime(1149): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:719)
06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
06-17 23:57:08.631: E/AndroidRuntime(1149):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:361)
06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.app.Activity.setContentView(Activity.java:1956)
06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.support.v7.app.ActionBarActivity.superSetContentView(ActionBarActivity.java:216)
06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.support.v7.app.ActionBarActivityDelegateICS.setContentView(ActionBarActivityDelegateICS.java:110)
06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:76)
06-17 23:57:08.631: E/AndroidRuntime(1149):     at com.mypackage.myActionBarActivity.onCreate(myActionBarActivity.java:20)
06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.app.Activity.performCreate(Activity.java:5372)
06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
06-17 23:57:08.631: E/AndroidRuntime(1149):     ... 12 more
06-17 23:57:08.631: E/AndroidRuntime(1149): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.view.ViewGroup.addViewInner(ViewGroup.java:3739)
06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.view.ViewGroup.addView(ViewGroup.java:3610)
06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.view.ViewGroup.addView(ViewGroup.java:3555)
06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.view.ViewGroup.addView(ViewGroup.java:3531)
06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.support.v4.app.NoSaveStateFrameLayout.wrap(NoSaveStateFrameLayout.java:40)
06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:915)
06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1093)
06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:310)
06-17 23:57:08.631: E/AndroidRuntime(1149):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
06-17 23:57:08.631: E/AndroidRuntime(1149):     ... 25 more

没有错误,但如果我尝试以下选项,listView将从布局中消失:

  • 删除if-else并且每次只是膨胀
  • 在片段布局上而不是在listView - ((ViewGroup) fragmentLayoutView.getParent()).removeAllViews();
  • 上执行getParent

我做错了什么?

1 个答案:

答案 0 :(得分:0)

我必须删除片段布局的所有父级视图:

((ViewGroup) fragmentLayoutView.getParent()).removeAllViews();

然后在初始化适配器和listview之前检查适配器是否为null:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    if(myCustomCursorAdaptor == null){
        myListView = (ListView) getActivity().findViewById(R.id.listview1);
        myCustomCursorAdaptor = new MyCustomCursorAdaptor(getActivity());
        myListView.setAdapter(myCustomCursorAdaptor);
    }
    ...
}