Reputation: 1441
I have tried all stackoverflow forums but couldn't get the solution
Error log:
06-08 10:02:24.116 18960-18960/com.example.itachiuchiha.crimeintent E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.itachiuchiha.crimeintent, PID: 18960
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.itachiuchiha.crimeintent/com.example.itachiuchiha.crimeintent.CrimeListActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2357)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2419)
at android.app.ActivityThread.access$900(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5293)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
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:3937)
at android.view.ViewGroup.addView(ViewGroup.java:3787)
at android.view.ViewGroup.addView(ViewGroup.java:3728)
at android.view.ViewGroup.addView(ViewGroup.java:3701)
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:1236)
at android.app.Activity.performStart(Activity.java:6006)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2320)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2419)
at android.app.ActivityThread.access$900(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5293)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699) 06-08 10:02:25.528 18960-18960/com.example.itachiuchiha.crimeintent I/Process: Sending signal. PID: 18960 SIG: 9
CrimeListActivity class:
package com.example.itachiuchiha.crimeintent;
import android.support.v4.app.Fragment;
public class CrimeListActivity extends SingleFragmentActivity {
@Override
protected Fragment createFragment() {
return new CrimeListFragment();
}
}
CrimeListFragment class:
package com.example.itachiuchiha.crimeintent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
//import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
public class CrimeListFragment extends Fragment {
public RecyclerView recyclerView;
public CrimeAdapter crimeAdapter;
@Override
public View. onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view=inflater.inflate(R.layout.fragment_crime_list,container);
recyclerView=(RecyclerView)view.findViewById(R.id.crime_recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
updateUI();
return view;
}
private void updateUI(){
CrimeLab crimeLab;
crimeLab = CrimeLab.get(getActivity());
List<Crime> crimes=crimeLab.getCrimeList();
crimeAdapter=new CrimeAdapter(crimes);
recyclerView.setAdapter(crimeAdapter);
}
private class CrimeHolder extends RecyclerView.ViewHolder implements. View.OnClickListener{
public TextView titleText;
// public TextView dateText;
// public CheckBox solvedCheck;
public Crime crime;
public CrimeHolder(View itemView){
super(itemView);
itemView.setOnClickListener(this);
titleText=(TextView)itemView;//.findViewById(R.id.list_item_crime_title_text_view);
// dateText=(TextView)itemView.findViewById(R.id.list_item_crime_date_text_view);
// solvedCheck=(CheckBox) itemView.findViewById(R.id.list_item_crime_solved_check_box);
}
public void bindCrime(Crime mcrime){
crime=mcrime;
titleText.setText(crime.getCrimeTitle());
// dateText.setText(crime.getDate().toString());
// solvedCheck.setChecked(crime.isSolved());
}
@Override
public void onClick(View vo) {
Toast.makeText(getActivity(),crime.getCrimeTitle()+"clicked",Toast.LENGTH_SHORT).show();
}
}
private class CrimeAdapter extends RecyclerView.Adapter<CrimeHolder>{
private List<Crime> crimes;
public CrimeAdapter(List<Crime> crimess){
crimes=crimess;
}
@Override
public CrimeHolder onCreateViewHolder(ViewGroup parent,int viewType){
LayoutInflater layoutInflater=LayoutInflater.from(getActivity());
View vh=layoutInflater.inflate(android.R.layout.simple_list_item_1,parent,false);
return new CrimeHolder(vh);
}
@Override
public void onBindViewHolder(CrimeHolder holder, int position) {
Crime crime=crimes.get(position);
holder.bindCrime(crime);
}
@Override
public int getItemCount() {
return crimes.size();
}
}
}
SingleFragmentActivity class:
package com.example.itachiuchiha.crimeintent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
public abstract class SingleFragmentActivity extends FragmentActivity{
protected abstract Fragment createFragment();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
FragmentManager fm=getSupportFragmentManager();
Fragment fragment=fm.findFragmentById(R.id.fragmet_container);
if(fragment==null){
fragment=createFragment();
fm.beginTransaction().add(R.id.fragmet_container,fragment).commit();
}
}
}
Crime class:
package com.example.itachiuchiha.crimeintent;
import java.util.Date;
import java.util.UUID;
public class Crime {
public UUID crimeID;
public String crimeTitle;
public Date date;
public boolean isSolved;
public Crime(){
crimeID=UUID.randomUUID();
this.date=new Date();
}
public boolean isSolved() {
return isSolved;
}
public void setSolved(boolean solved) {
isSolved = solved;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public UUID getCrimeID() {
return crimeID;
}
public String getCrimeTitle() {
return crimeTitle;
}
public void setCrimeTitle(String crimeTitle) {
this.crimeTitle = crimeTitle;
}
}
CrimeLab class:
package com.example.itachiuchiha.crimeintent;
import android.content.Context;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class CrimeLab {
public static CrimeLab crimeLab;
public List<Crime> crimeList;
public static CrimeLab get(Context context){
if(crimeLab==null){
crimeLab=new CrimeLab(context);
}
return crimeLab;
}
private CrimeLab(Context context){
crimeList=new ArrayList<>();
for(int i=0;i<100;i++){
Crime crime=new Crime();
crime.setCrimeTitle("Crime #"+i);
crime.setSolved(i%2==0);
crimeList.add(crime);
}
}
public List<Crime> getCrimeList(){
return crimeList;
}
public Crime getCrimebyUUID(UUID id){
for (Crime crime:crimeList){
if(crime.getCrimeID().equals(id)){
return crime;
}
}
return null;
}
}
fragment_crime_list.XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/crime_recycler_view">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
activity_fragment.XML:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragmet_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Also it will be great if you can explain why this error is coming and how to sort it for future.
Upvotes: 0
Views: 322
Reputation: 1162
in onCreateView()
try this.
inflater.inflate(R.layout.fragment_crime_list, container, false);
According to the java docs, the third param means
Whether the inflated hierarchy should be attached to the root parameter? If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML.
Upvotes: 2