3

所以我首先要说我已经看到其他几个人发布了完全相同的错误。但是,我正处于尝试了所有十种不同解决方案并且仍然获得完全相同的堆栈跟踪和 ANR 的阶段。我使用的是标准的 Tabs + Swipe 模板,除了将第一页设置为返回我将在下面显示的 ListView 片段外,没有对其进行任何更改。

下面是我的片段的代码。CacheFrament只是一个扩展的超类android.support.v4.app.Fragment。在这种情况下,我尝试只返回列表视图及其父视图,但均无济于事。

public class RoutesFragment extends CacheFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.list_view_layout, container);
        listView.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, new String[]{"No Routes Available"}));

        return view;
    }
}

这是我用于片段的布局,我最初将 ListView 作为父级,这也不起作用

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <ListView xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:id="@+id/main_list_view"/>

</LinearLayout>

最后,这是我不断得到的堆栈跟踪。请注意,这与我每次遇到的错误完全相同。

07-28 11:54:43.747  15074-15074/? 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:3387)
        at android.view.ViewGroup.addView(ViewGroup.java:3258)
        at android.view.ViewGroup.addView(ViewGroup.java:3203)
        at android.view.ViewGroup.addView(ViewGroup.java:3179)
        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.run(BackStackRecord.java:682)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
        at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
        at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:1064)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:911)
        at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1432)
        at android.view.View.measure(View.java:15364)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4824)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.view.View.measure(View.java:15364)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
        at android.view.View.measure(View.java:15364)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4824)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2359)
        at android.view.View.measure(View.java:15364)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1974)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1217)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1390)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1110)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4472)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
        at android.view.Choreographer.doCallbacks(Choreographer.java:555)
        at android.view.Choreographer.doFrame(Choreographer.java:525)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
        at android.os.Handler.handleCallback(Handler.java:615)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4918)
        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:1004)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
        at dalvik.system.NativeStart.main(Native Method)

任何帮助是极大的赞赏。这个错误基本上是在拖延一切。

4

1 回答 1

10
View view = inflater.inflate(R.layout.list_view_layout, null);

膨胀视图时应该使用空容器,否则容器将成为膨胀视图的父级。

于 2013-07-28T18:05:28.313 回答