2016-02-27 70 views
0

我的RecyclerView在協調器佈局中的一個片段內膨脹時拋出上述錯誤。是否有與父佈局的任何連接?我查看了與此錯誤有關的所有答案,但沒有一個可以解決我的問題。下面是片段+適配器RecyclerView拋出java.lang.IllegalStateException:指定的子項已經有一個父

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的活動:

<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以及幾乎相同的代碼,但他們都工作得很好。任何幫助,將不勝感激。

+1

嘗試改變它...... '視圖V = li.inflate(R.layout.job_item_list,父母,假);' –

+0

謝謝!現在它工作得很好! –

+0

m將其作爲答案張貼..接受它plz .. –

回答

1

試圖去改變它......

View v = li.inflate(R.layout.job_item_list, parent,false); 
相關問題