RecyclerView throwing java.lang.IllegalStateException: 指定的 child 已经有一个 parent
RecyclerView throwing java.lang.IllegalStateException: The speicified child already has a parent
我的 RecyclerView 在 Coordinator 布局中的片段内膨胀时抛出上述错误。与 parent 布局有任何联系吗?我已经查看了与此错误相关的所有答案,但 none 可以解决我的问题。这是片段+适配器的代码
public static class EditJob extends Fragment {
ServerRequests serverRequests;
UserLocalStore userLocalStore;
User receivedUser;
EditText searchJob;
ImageView searchBtn;
RecyclerView editJobRecycler;
MyRecyclerAdapter editJobAdapter;
ArrayList<Job> arrayJob;
TextView noJobSearch;
@Override
public void onAttach(Context context) {
super.onAttach(context);
serverRequests = new ServerRequests(context);
userLocalStore = new UserLocalStore(context);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_edit_job, container, false);
receivedUser = userLocalStore.getAllDetails();
arrayJob = new ArrayList<>();
noJobSearch = (TextView) view.findViewById(R.id.noJobEdit);
searchJob = (EditText) view.findViewById(R.id.etEditJob);
searchBtn = (ImageView) view.findViewById(R.id.editJobImage);
editJobRecycler = (RecyclerView) view.findViewById(R.id.jobEditRecycler);
editJobAdapter = new MyRecyclerAdapter(arrayJob);
editJobRecycler.setLayoutManager(new LinearLayoutManager(getContext()));
editJobRecycler.setAdapter(editJobAdapter);
searchBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
searchJob();
}
});
return view;
}
public void searchJob() {
arrayJob.clear();
serverRequests.searchJobListings(searchJob.getText().toString(), new GetJobObjectsCallBack() {
@Override
public void done(Job[] returnedJobs) {
if (returnedJobs == null || returnedJobs.length == 0) {
noJobSearch.setVisibility(View.VISIBLE);
} else {
noJobSearch.setVisibility(View.GONE);
for (int i = 0; i < returnedJobs.length; i++) {
arrayJob.add(i, returnedJobs[i]);
}
editJobAdapter.notifyDataSetChanged();
}
}
});
}
private class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.VH> {
ArrayList<Job> jobArray;
public MyRecyclerAdapter(ArrayList<Job> jl) {
jobArray = jl;
}
@Override
public VH onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater li = LayoutInflater.from(parent.getContext());
View v = li.inflate(R.layout.job_item_list, parent);
return new VH(v);
}
@Override
public void onBindViewHolder(VH holder, int position) {
Job current = jobArray.get(position);
holder.jobID.setText(current.getID());
holder.jobDescription.setText(current.getDescription());
holder.jobDomain.setText(current.getDomain());
holder.jobExperience.setText(current.getExperience());
holder.jobRemarks.setText(current.getRemarks());
holder.jobLocation.setText(current.getLocation());
holder.jobPosition.setText(current.getID());
}
@Override
public int getItemCount() {
return jobArray.size();
}
public class VH extends RecyclerView.ViewHolder {
TextView jobID, jobPosition, jobExperience, jobRemarks, jobLocation, jobDescription, jobDomain;
EditText etjobID, etjobPosition, etjobExperience, etjobRemarks, etjobLocation, etjobDescription, etjobDomain;
Button bEditJob;
public VH(View v) {
super(v);
jobID = (TextView) v.findViewById(R.id.returnedJobID);
jobExperience = (TextView) v.findViewById(R.id.returnedJobExperience);
jobRemarks = (TextView) v.findViewById(R.id.returnedJobRemarks);
jobLocation = (TextView) v.findViewById(R.id.returnedJobLocation);
jobDescription = (TextView) v.findViewById(R.id.returnedJobDescription);
jobDomain = (TextView) v.findViewById(R.id.returnedJobDomain);
jobPosition = (TextView) v.findViewById(R.id.returnedJobPosition);
etjobID = (EditText) v.findViewById(R.id.etreturnedJobID);
etjobPosition = (EditText) v.findViewById(R.id.etreturnedJobPosition);
etjobExperience = (EditText) v.findViewById(R.id.etreturnedJobExperience);
etjobRemarks = (EditText) v.findViewById(R.id.etreturnedJobRemarks);
etjobLocation = (EditText) v.findViewById(R.id.etreturnedJobLocation);
etjobDescription = (EditText) v.findViewById(R.id.etreturnedJobDescription);
etjobDomain = (EditText) v.findViewById(R.id.etreturnedJobDomain);
bEditJob = (Button) v.findViewById(R.id.bEditJob);
}
}
}
}
XML 片段:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
style="@style/FloatingTextView"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:animateLayoutChanges="true"
android:elevation="6dp">
<EditText
android:id="@+id/etEditJob"
style="@style/FloatingTextView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerVertical="true"
android:layout_margin="0.5dp"
android:layout_toLeftOf="@+id/editJobDivider"
android:elevation="0dp"
android:hint="Enter a search term"
android:translationZ="0dp" />
<View
android:id="@+id/editJobDivider"
android:layout_width="1dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/editJobImage"
android:alpha="0.2"
android:background="@color/Black" />
<ImageView
android:id="@+id/editJobImage"
style="@style/FloatingTextView"
android:layout_width="47dp"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_margin="1dp"
android:alpha="0.5"
android:clickable="true"
android:elevation="0dp"
android:padding="10dp"
android:src="@drawable/ic_search_black_48dp"
android:translationZ="0dp" />
</RelativeLayout>
<TextView
android:id="@+id/noJobEdit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="No Jobs match your query"
android:visibility="gone" />
<android.support.v7.widget.RecyclerView
android:id="@+id/jobEditRecycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="2dp" />
</LinearLayout>
XML 为 activity:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/adminCoordinator"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/adminViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
<!-- <android.support.design.widget.NavigationView
android:id="@+id/nav_drawer"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/drawer_header"
app:menu="@menu/menu_drawer_admin" />
</android.support.v4.widget.DrawerLayout> -->
我已经尝试了所有方法,包括 parent.removeAllViews() 但似乎没有任何效果。我也用几乎相同的代码实现了其他 RecyclerViews,但它们都工作得很好。任何帮助将不胜感激。
尝试将其更改为...
View v = li.inflate(R.layout.job_item_list, parent,false);
我的 RecyclerView 在 Coordinator 布局中的片段内膨胀时抛出上述错误。与 parent 布局有任何联系吗?我已经查看了与此错误相关的所有答案,但 none 可以解决我的问题。这是片段+适配器的代码
public static class EditJob extends Fragment {
ServerRequests serverRequests;
UserLocalStore userLocalStore;
User receivedUser;
EditText searchJob;
ImageView searchBtn;
RecyclerView editJobRecycler;
MyRecyclerAdapter editJobAdapter;
ArrayList<Job> arrayJob;
TextView noJobSearch;
@Override
public void onAttach(Context context) {
super.onAttach(context);
serverRequests = new ServerRequests(context);
userLocalStore = new UserLocalStore(context);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_edit_job, container, false);
receivedUser = userLocalStore.getAllDetails();
arrayJob = new ArrayList<>();
noJobSearch = (TextView) view.findViewById(R.id.noJobEdit);
searchJob = (EditText) view.findViewById(R.id.etEditJob);
searchBtn = (ImageView) view.findViewById(R.id.editJobImage);
editJobRecycler = (RecyclerView) view.findViewById(R.id.jobEditRecycler);
editJobAdapter = new MyRecyclerAdapter(arrayJob);
editJobRecycler.setLayoutManager(new LinearLayoutManager(getContext()));
editJobRecycler.setAdapter(editJobAdapter);
searchBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
searchJob();
}
});
return view;
}
public void searchJob() {
arrayJob.clear();
serverRequests.searchJobListings(searchJob.getText().toString(), new GetJobObjectsCallBack() {
@Override
public void done(Job[] returnedJobs) {
if (returnedJobs == null || returnedJobs.length == 0) {
noJobSearch.setVisibility(View.VISIBLE);
} else {
noJobSearch.setVisibility(View.GONE);
for (int i = 0; i < returnedJobs.length; i++) {
arrayJob.add(i, returnedJobs[i]);
}
editJobAdapter.notifyDataSetChanged();
}
}
});
}
private class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.VH> {
ArrayList<Job> jobArray;
public MyRecyclerAdapter(ArrayList<Job> jl) {
jobArray = jl;
}
@Override
public VH onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater li = LayoutInflater.from(parent.getContext());
View v = li.inflate(R.layout.job_item_list, parent);
return new VH(v);
}
@Override
public void onBindViewHolder(VH holder, int position) {
Job current = jobArray.get(position);
holder.jobID.setText(current.getID());
holder.jobDescription.setText(current.getDescription());
holder.jobDomain.setText(current.getDomain());
holder.jobExperience.setText(current.getExperience());
holder.jobRemarks.setText(current.getRemarks());
holder.jobLocation.setText(current.getLocation());
holder.jobPosition.setText(current.getID());
}
@Override
public int getItemCount() {
return jobArray.size();
}
public class VH extends RecyclerView.ViewHolder {
TextView jobID, jobPosition, jobExperience, jobRemarks, jobLocation, jobDescription, jobDomain;
EditText etjobID, etjobPosition, etjobExperience, etjobRemarks, etjobLocation, etjobDescription, etjobDomain;
Button bEditJob;
public VH(View v) {
super(v);
jobID = (TextView) v.findViewById(R.id.returnedJobID);
jobExperience = (TextView) v.findViewById(R.id.returnedJobExperience);
jobRemarks = (TextView) v.findViewById(R.id.returnedJobRemarks);
jobLocation = (TextView) v.findViewById(R.id.returnedJobLocation);
jobDescription = (TextView) v.findViewById(R.id.returnedJobDescription);
jobDomain = (TextView) v.findViewById(R.id.returnedJobDomain);
jobPosition = (TextView) v.findViewById(R.id.returnedJobPosition);
etjobID = (EditText) v.findViewById(R.id.etreturnedJobID);
etjobPosition = (EditText) v.findViewById(R.id.etreturnedJobPosition);
etjobExperience = (EditText) v.findViewById(R.id.etreturnedJobExperience);
etjobRemarks = (EditText) v.findViewById(R.id.etreturnedJobRemarks);
etjobLocation = (EditText) v.findViewById(R.id.etreturnedJobLocation);
etjobDescription = (EditText) v.findViewById(R.id.etreturnedJobDescription);
etjobDomain = (EditText) v.findViewById(R.id.etreturnedJobDomain);
bEditJob = (Button) v.findViewById(R.id.bEditJob);
}
}
}
}
XML 片段:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
style="@style/FloatingTextView"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:animateLayoutChanges="true"
android:elevation="6dp">
<EditText
android:id="@+id/etEditJob"
style="@style/FloatingTextView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerVertical="true"
android:layout_margin="0.5dp"
android:layout_toLeftOf="@+id/editJobDivider"
android:elevation="0dp"
android:hint="Enter a search term"
android:translationZ="0dp" />
<View
android:id="@+id/editJobDivider"
android:layout_width="1dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/editJobImage"
android:alpha="0.2"
android:background="@color/Black" />
<ImageView
android:id="@+id/editJobImage"
style="@style/FloatingTextView"
android:layout_width="47dp"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_margin="1dp"
android:alpha="0.5"
android:clickable="true"
android:elevation="0dp"
android:padding="10dp"
android:src="@drawable/ic_search_black_48dp"
android:translationZ="0dp" />
</RelativeLayout>
<TextView
android:id="@+id/noJobEdit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="No Jobs match your query"
android:visibility="gone" />
<android.support.v7.widget.RecyclerView
android:id="@+id/jobEditRecycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="2dp" />
</LinearLayout>
XML 为 activity:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/adminCoordinator"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/adminViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
<!-- <android.support.design.widget.NavigationView
android:id="@+id/nav_drawer"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/drawer_header"
app:menu="@menu/menu_drawer_admin" />
</android.support.v4.widget.DrawerLayout> -->
我已经尝试了所有方法,包括 parent.removeAllViews() 但似乎没有任何效果。我也用几乎相同的代码实现了其他 RecyclerViews,但它们都工作得很好。任何帮助将不胜感激。
尝试将其更改为...
View v = li.inflate(R.layout.job_item_list, parent,false);