指定的子项已在SwipeRefreshLayout中具有父Exception

时间:2016-06-06 15:12:41

标签: android android-recyclerview swiperefreshlayout

我有以下布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/feedRefresh"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/feed"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@android:color/transparent"
                android:scrollbars="vertical" />

        </LinearLayout>
</android.support.v4.widget.SwipeRefreshLayout>

以下片段类:

public class FeedFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable final Bundle savedInstanceState) {
        SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout) inflater.inflate(R.layout.fragment_feed, container, false);

        RecyclerView feed = (RecyclerView) swipeRefreshLayout.findViewById(R.id.feed);
        feed.setLayoutManager(new LinearLayoutManager(getActivity()));
        List<String> idsList = new ArrayList<>();
        feed.setAdapter(new FeedAdapter(idsList.toArray(new String[idsList.size()])));

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //Do something
            }
        });

        return feed;
    }
}

当我在设备上运行代码时,出现以下错误:

Caused by: 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:4309)
                                                                                  at android.view.ViewGroup.addView(ViewGroup.java:4145)
                                                                                  at android.view.ViewGroup.addView(ViewGroup.java:4086)
                                                                                  at android.view.ViewGroup.addView(ViewGroup.java:4059)
                                                                                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1083)
                                                                                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
                                                                                  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
                                                                                  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
                                                                                  at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:339)
                                                                                  at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:601)
                                                                                  at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1238)
                                                                                  at android.app.Activity.performStart(Activity.java:6268)
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2413)
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520) 
                                                                                  at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363) 
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                  at android.os.Looper.loop(Looper.java:148) 
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5466) 
                                                                                  at java.lang.reflect.Method.invoke(Native Method) 
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

我知道更换

时会出现错误
SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout) inflater.inflate(R.layout.fragment_feed, container, false);

SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout) inflater.inflate(R.layout.fragment_feed, container, true);

但正如你所见,我false站在那里。我多次用Google搜索这个错误,但总是找到人们以true作为第三个参数的答案。

我完全不知道,我做错了什么。 任何帮助很多赞赏!

2 个答案:

答案 0 :(得分:2)

最后一行对我来说很奇怪

return feed;

我认为您不想退回您的RecyclerView。您将希望返回片段的膨胀布局。所以试试

return swipeRefreshLayout;

答案 1 :(得分:0)

您必须将视图返回到onCreateView方法

View feedView=inflater.inflate(R.layout.fragment_feed, container, false);
 SwipeRefreshLayout swipeRefreshLayout=(SwipeRefreshLayout) feedView.findViewById(R.id.feedRefresh);
 RecyclerView feed = (RecyclerView) feedView.findViewById(R.id.feed);

//Your Code


return feedView;