我有一个仅包含 a 的片段,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;
}
}
}
和杂志_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"/>
和杂志_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
我做错了什么?