我目前正在尝试在片段中使用 ABS(ActionBarSherlock) 制作 GridView。但我遇到了一些问题,得到 NPE(NullPointerException)。这是我的片段代码:
public class Fragment2 extends SherlockFragment {
GridView gridView;
ImageAdapter imgAdapter;
private Context context;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment2, container, false);
gridView = (GridView) rootView.findViewById(R.id.gridview);
context = getSherlockActivity();
gridView.setAdapter(new ImageAdapter(context));
return rootView;
}
还有我的 ImageAdapterClass:
public class ImageAdapter extends BaseAdapter {
// references to our images
private Integer[] mThumbIds = {
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7
};
private static Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
}
最后是我的 LogCat:
10-25 01:06:32.239: E/AndroidRuntime(12344): at com.AlexPrograms.NavDrawerSherlock.Fragment2.onCreateView(Fragment2.java:24)
10-25 01:06:32.239: E/AndroidRuntime(12344): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
10-25 01:06:32.239: E/AndroidRuntime(12344): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
10-25 01:06:32.239: E/AndroidRuntime(12344): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
10-25 01:06:32.239: E/AndroidRuntime(12344): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
10-25 01:06:32.239: E/AndroidRuntime(12344): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
10-25 01:06:32.239: E/AndroidRuntime(12344): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
10-25 01:06:32.239: E/AndroidRuntime(12344): at android.os.Handler.handleCallback(Handler.java:587)
10-25 01:06:32.239: E/AndroidRuntime(12344): at android.os.Handler.dispatchMessage(Handler.java:92)
10-25 01:06:32.239: E/AndroidRuntime(12344): at android.os.Looper.loop(Looper.java:130)
10-25 01:06:32.239: E/AndroidRuntime(12344): at android.app.ActivityThread.main(ActivityThread.java:3687)
10-25 01:06:32.239: E/AndroidRuntime(12344): at java.lang.reflect.Method.invokeNative(Native Method)
10-25 01:06:32.239: E/AndroidRuntime(12344): at java.lang.reflect.Method.invoke(Method.java:507)
10-25 01:06:32.239: E/AndroidRuntime(12344): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
10-25 01:06:32.239: E/AndroidRuntime(12344): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
10-25 01:06:32.239: E/AndroidRuntime(12344): at dalvik.system.NativeStart.main(Native Method)
如果你能找到问题所在,我会尽力而为。提前致谢