在适配器中使用布局inflater时出现IllegalStateException

时间:2012-12-19 06:49:16

标签: android android-fragments android-gridview baseadapter illegalstateexception

我有一个只包含GridView的片段,我有一个使用LayoutInflater从XML创建视图的适配器。以下是我的片段:

public class MagazineFragment extends SherlockFragment{
private GridView mGridView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    return inflater.inflate(R.layout.magazine_grid, container);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mGridView = (GridView)view.findViewById(R.id.grid_view);
    mGridView.setAdapter(new MagazineAdapter(getActivity(), savedInstanceState));
}

class MagazineAdapter extends BaseAdapter{
    int[] mImages;
    private Context mContext;
    private Bundle mSavedInstanceState;

    public MagazineAdapter(Context context, Bundle savedInstanceState) {
        mContext = context;
        mSavedInstanceState = savedInstanceState;
        mImages = getResources().getIntArray(R.array.magazines);
    }

    @Override
    public int getCount() {
        return mImages.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ImageView imageView;
        if(convertView == null){
            imageView = (ImageView)getLayoutInflater(mSavedInstanceState).inflate(R.layout.magazine_grid_item, parent, false);
        }else{
            imageView = (ImageView)convertView;
        }

        imageView.setImageResource(mImages[position]);
        return imageView;
    }

}

}

和magazine_grid.xml:

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/grid_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:columnWidth="150dip"
    android:numColumns="auto_fit"
    android:gravity="center"
    android:stretchMode="columnWidth"
    android:scrollbarStyle="outsideOverlay"
    android:padding="4dip"/>

和magazine_grid_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="150dip"
    android:layout_height="200dip"
    android:adjustViewBounds="true"
    android:scaleType="fitCenter"
    android:paddingTop="6dip"
    android:paddingBottom="6dip" 
    android:paddingLeft="2dip"
    android:paddingRight="2dip"/>

日志消息:

12-19 08:38:36.675: E/AndroidRuntime(11977): FATAL EXCEPTION: main 12-19 08:38:36.675: E/AndroidRuntime(11977): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.coderdem.android.zaytung/com.coderdem.android.zaytung.MainActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 12-19 08:38:36.675: E/AndroidRuntime(11977):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967) 12-19 08:38:36.675: E/AndroidRuntime(11977):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992) 12-19 08:38:36.675: E/AndroidRuntime(11977):   at android.app.ActivityThread.access$600(ActivityThread.java:127) 12-19 08:38:36.675: E/AndroidRuntime(11977):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158) 12-19 08:38:36.675: E/AndroidRuntime(11977):    at android.os.Handler.dispatchMessage(Handler.java:99) 12-19 08:38:36.675: E/AndroidRuntime(11977):     at android.os.Looper.loop(Looper.java:137) 12-19 08:38:36.675: E/AndroidRuntime(11977):     at android.app.ActivityThread.main(ActivityThread.java:4511) 12-19 08:38:36.675: E/AndroidRuntime(11977):   at java.lang.reflect.Method.invokeNative(Native Method) 12-19 08:38:36.675: E/AndroidRuntime(11977):    at java.lang.reflect.Method.invoke(Method.java:511) 12-19 08:38:36.675: E/AndroidRuntime(11977):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980) 12-19 08:38:36.675: E/AndroidRuntime(11977):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747) 12-19 08:38:36.675: E/AndroidRuntime(11977):    at dalvik.system.NativeStart.main(Native Method) 12-19 08:38:36.675: E/AndroidRuntime(11977): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 12-19 08:38:36.675: E/AndroidRuntime(11977):    at android.view.ViewGroup.addViewInner(ViewGroup.java:3342) 12-19 08:38:36.675: E/AndroidRuntime(11977):    at android.view.ViewGroup.addView(ViewGroup.java:3213) 12-19 08:38:36.675: E/AndroidRuntime(11977):     at android.view.ViewGroup.addView(ViewGroup.java:3170) 12-19 08:38:36.675: E/AndroidRuntime(11977):     at android.view.ViewGroup.addView(ViewGroup.java:3150) 12-19 08:38:36.675: E/AndroidRuntime(11977):     at android.support.v4.app.NoSaveStateFrameLayout.wrap(NoSaveStateFrameLayout.java:40) 12-19 08:38:36.675: E/AndroidRuntime(11977):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:915) 12-19 08:38:36.675: E/AndroidRuntime(11977):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088) 12-19 08:38:36.675: E/AndroidRuntime(11977):   at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) 12-19 08:38:36.675: E/AndroidRuntime(11977):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444) 12-19 08:38:36.675: E/AndroidRuntime(11977):    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:551) 12-19 08:38:36.675: E/AndroidRuntime(11977):  at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1136) 12-19 08:38:36.675: E/AndroidRuntime(11977):  at android.app.Activity.performStart(Activity.java:4480) 12-19 08:38:36.675: E/AndroidRuntime(11977):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1940) 12-19 08:38:36.675: E/AndroidRuntime(11977):  ... 11 more

我做错了什么?

2 个答案:

答案 0 :(得分:3)

您的onCreateView

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // why?
    container.removeAllViews();
    return inflater.inflate(R.layout.magazine_grid, container);
}

不正确。尝试像这样修改它:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
     // Android will attach the view returned by this method on its own so don't 
     // add it yourself to the container
     return inflater.inflate(R.layout.magazine_grid, container, false);
}

答案 1 :(得分:1)

MagazineFragment 类中使用此代码代替Overrided方法 public View getView()

 @Override
public View getView(int position, View convertView, ViewGroup parent) {

    //Use here parent to remove previouse view
       parent.removeAllViews();
       //this create a new view.
    final ImageView imageView;
    if(convertView == null){
        imageView = (ImageView)getLayoutInflater(mSavedInstanceState).inflate(R.layout.magazine_grid_item, parent, false);
    }else{
        imageView = (ImageView)convertView;
    }

    imageView.setImageResource(mImages[position]);
    return imageView;
}

这可能对你有帮助。