ScrollView Fragment上的IllegalStateException

时间:2013-07-01 19:29:22

标签: android android-fragments scrollview

我有一个布局文件,其子视图包含在LinearLayout ScrollView中。总结一下布局文件:

<ScrollView android:id="@+id/scrollView">
    <LinearLayout android:id="@+id/scrollViewChildLayout">
        ... lots of text and image views ...
    </LinearLayout>
</ScrollView>

我正在使用SherlockListFragment与其他SherlockFragment一起创建类似于Google详述的标题+文章片段here我的标题片段正在使用ListView },而我的文章片段使用上述ScrollView布局。

目前,我正在为手机编码,所以在处理片段时我正在使用FrameLayout替换技术。我可以很好地使用我的“文章”片段,但是当我按下时,会发生以下错误。这似乎是因为ScrollView而发生的,因为仅使用常规的LinearLayout交换它就可以正常工作。有谁知道为什么会引起这个问题,并且可以指出解决它的正确方法?

E/AndroidRuntime: FATAL EXCEPTION: main
        java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
        at android.view.ViewGroup.addViewInner(ViewGroup.java:3339)
        at android.view.ViewGroup.addView(ViewGroup.java:3210)
        at android.view.ViewGroup.addView(ViewGroup.java:3155)
        at android.view.ViewGroup.addView(ViewGroup.java:3131)
        at android.support.v4.app.NoSaveStateFrameLayout.wrap(NoSaveStateFrameLayout.java:40)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:915)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
        at android.support.v4.app.BackStackRecord.popFromBackStack(BackStackRecord.java:764)
        at android.support.v4.app.FragmentManagerImpl.popBackStackState(FragmentManager.java:1493)
        at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:477)
        at android.support.v4.app.FragmentActivity.onBackPressed(FragmentActivity.java:174)
        at com.tomra.recycle.AccountActivity.onBackPressed(AccountActivity.java:129)
        at android.app.Activity.onKeyUp(Activity.java:2145)
        at android.view.KeyEvent.dispatch(KeyEvent.java:2633)
        at android.app.Activity.dispatchKeyEvent(Activity.java:2375)
        at com.actionbarsherlock.app.SherlockFragmentActivity.dispatchKeyEvent(SherlockFragmentActivity.java:122)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1847)
        at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3701)
        at android.view.ViewRootImpl.handleImeFinishedEvent(ViewRootImpl.java:3651)
        at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:2818)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5041)
        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)

1 个答案:

答案 0 :(得分:0)

奇怪的行为,但改变onCreateView中的布局通胀解决了这个问题。似乎视图保留了以前的视图并尝试对其进行充气。将savedInstanceState覆盖更改为false已修复。更新了onCreateView,如下所示。

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.wtf(TAG, "onCreateView");

        mInflater = inflater;

        // rootView = mInflater.inflate(R.layout.ach_list_layout, container, savedInstanceState);
        rootView = mInflater.inflate(R.layout.ach_list_layout, container, false);

        return rootView;
    }