jeudi 2 octobre 2014

Android - java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first


Vote count:

-1




I'm bad at English so i explain my problem briefly. Sorry ;) I have a GridView with an Adapter that extends BaseAdapter... This is my Adapter code:



public class CustomGrid extends BaseAdapter{
private Context mContext;

public CustomGrid(Context c) {
mContext = c;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return capTitle.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View grid;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = new View(mContext);
grid = inflater.inflate(R.layout.capability, null);
LinearLayout carcap = (LinearLayout) grid.findViewById(R.id.carCap);
TextView textView = (TextView) grid.findViewById(R.id.capText);
ImageView imageView = (ImageView)grid.findViewById(R.id.capImg);
textView.setText(capTitle[position]);
imageView.setImageResource(capImgId[position]);
Bundle args = new Bundle();
args.putString("capcontent", capContent[position]);
final CapViewer capviewer = new CapViewer();
capviewer.setArguments(args);
carcap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
showCapContent();
}
});
}
else {
grid = (View) convertView;
}
return grid;
}
}


Each view has a click listener that call a method to replace a layout with a new fragment:



public void showCapContent(){
FrameLayout capLayout = (FrameLayout) getView().getParent();
capLayout.removeView(getView().findViewById(R.id.caprep));
view.setVisibility(View.GONE);
getChildFragmentManager().beginTransaction().replace(R.id.caprep, new CapViewer()).commit();
}


When I click on one of views, it throws Exepctions:



10-02 06:45:40.966: E/AndroidRuntime(1569): FATAL EXCEPTION: main
10-02 06:45:40.966: E/AndroidRuntime(1569): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
10-02 06:45:40.966: E/AndroidRuntime(1569): at android.view.ViewGroup.addViewInner(ViewGroup.java:3509)
10-02 06:45:40.966: E/AndroidRuntime(1569): at android.view.ViewGroup.addView(ViewGroup.java:3380)
10-02 06:45:40.966: E/AndroidRuntime(1569): at android.view.ViewGroup.addView(ViewGroup.java:3325)
10-02 06:45:40.966: E/AndroidRuntime(1569): at android.view.ViewGroup.addView(ViewGroup.java:3301)
10-02 06:45:40.966: E/AndroidRuntime(1569): at android.support.v4.app.NoSaveStateFrameLayout.wrap(NoSaveStateFrameLayout.java:40)
10-02 06:45:40.966: E/AndroidRuntime(1569): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:942)
10-02 06:45:40.966: E/AndroidRuntime(1569): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
10-02 06:45:40.966: E/AndroidRuntime(1569): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
10-02 06:45:40.966: E/AndroidRuntime(1569): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
10-02 06:45:40.966: E/AndroidRuntime(1569): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:446)
10-02 06:45:40.966: E/AndroidRuntime(1569): at android.os.Handler.handleCallback(Handler.java:730)
10-02 06:45:40.966: E/AndroidRuntime(1569): at android.os.Handler.dispatchMessage(Handler.java:92)
10-02 06:45:40.966: E/AndroidRuntime(1569): at android.os.Looper.loop(Looper.java:137)
10-02 06:45:40.966: E/AndroidRuntime(1569): at android.app.ActivityThread.main(ActivityThread.java:5103)
10-02 06:45:40.966: E/AndroidRuntime(1569): at java.lang.reflect.Method.invokeNative(Native Method)
10-02 06:45:40.966: E/AndroidRuntime(1569): at java.lang.reflect.Method.invoke(Method.java:525)
10-02 06:45:40.966: E/AndroidRuntime(1569): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-02 06:45:40.966: E/AndroidRuntime(1569): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-02 06:45:40.966: E/AndroidRuntime(1569): at dalvik.system.NativeStart.main(Native Method)


And this i my xml file:



<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/capabilitySelect">

<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="@+id/caprep"
android:orientation="horizontal"></LinearLayout>
</LinearLayout>

<GridView
android:id="@+id/capSelect"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/categorySelected"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:horizontalSpacing="10dp"
android:verticalSpacing="20dp"
android:padding="20dp"
android:clipToPadding="false"/>

</FrameLayout>


Any suggestion?



asked 53 secs ago







Android - java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first

Aucun commentaire:

Enregistrer un commentaire