我想填充我的RecyclerView與列表和我的應用程序崩潰與「這個孩子已經有一個父母」的錯誤。 現在我認爲它可能與新的android studio和新的設計庫有關。RecyclerView崩潰的應用程序(由於新的設計庫?)
我曾嘗試都假的,真正的爭論,同時呼籲RecyclerView適配器
這裏的充氣方法是我的代碼:
活動:
RecyclerView usersRecyclerView = (RecyclerView) findViewById(R.id.users_recyclerView);
usersAdapter = new UsersAdapter(appUsers);
usersRecyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);
usersRecyclerView.setLayoutManager(mLayoutManager);
usersRecyclerView.setAdapter(usersAdapter);
activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.sarigbrand.japanesequickcourse.ChatActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_chat" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
content_chat.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.sarigbrand.japanesequickcourse.ChatActivity"
tools:showIn="@layout/activity_chat"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/heb_loading"
android:id="@+id/loading_textView"
android:textColor="@color/colorRed500"
android:layout_gravity="center_horizontal"
android:textStyle="bold"
android:layout_marginTop="16dp"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:id="@+id/users_recyclerView">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
UsersAdapter:
public class UsersAdapter extends RecyclerView.Adapter<UsersAdapter.UserHolder> {
ArrayList<AppUser> appUsers;
public UsersAdapter (ArrayList<AppUser> appUsers){
this.appUsers = appUsers;
}
public static class UserHolder extends RecyclerView.ViewHolder {
public TextView userNameTV;
public UserHolder(TextView tv) {
super(tv);
userNameTV = tv;
}
}
@Override
public UsersAdapter.UserHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_user_list_view, parent, false);
TextView tv = (TextView) v.findViewById(R.id.user_name_textView);
UserHolder userHolder = new UserHolder(tv);
return userHolder;
}
@Override
public void onBindViewHolder(UserHolder holder, int position) {
AppUser user = appUsers.get(position);
holder.userNameTV.setText(user.userName);
}
@Override
public int getItemCount() {
return appUsers.size();
}
}
錯誤日誌:
FATAL EXCEPTION: main Process: com.example.sarigbrand.japanesequickcourse, PID: 8642
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.support.v7.widget.RecyclerView$5.addView(RecyclerView.java:585)
at android.support.v7.widget.ChildHelper.addView(ChildHelper.java:107)
at android.support.v7.widget.RecyclerView$LayoutManager.addViewInt(RecyclerView.java:6254)
at android.support.v7.widget.RecyclerView$LayoutManager.addView(RecyclerView.java:6212)
at android.support.v7.widget.RecyclerView$LayoutManager.addView(RecyclerView.java:6200)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1384)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1333)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:562)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2900)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3071)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.support.design.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:1037)
at android.support.design.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:747)
at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1156)
at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:760)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2678)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2171)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1931)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.vie
從不使用'LayoutInflater.from()'綁定到活動的東西。在託管'Activity'上調用'getLayoutInflater()'並使用它。 – CommonsWare
嗨@CommonsWare我得到了答案,這不是佈局充氣器,但我想聽聽更多有關充氣器問題,是不是所有的視圖綁定到一個活動? – Roee
也我不明白如何獲得託管活動的佈局inflater – Roee