我正在尝试开发一个应用程序,该应用程序在 SDCard 的文件夹中创建图像并显示它们(“MyCreatedImages”文件夹现在显示在图库中),现在我正在尝试从 gridview 布局中的给定文件夹加载所有图像。我正在使用以下代码,但是由于我无法理解的原因,我不断获得 NPE。
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.activity_my_image_fragment,
container, false);
// Set up an array of the Thumbnail Image ID column we want
String[] projection = { MediaStore.Images.Thumbnails._ID };
// Create the cursor pointing to the SDCard
cursor = getActivity().getContentResolver().query(
Uri.parse("file://" + Environment.getExternalStorageDirectory()+ File.separator + "MyCreatedImages"), projection,
null, null, MediaStore.Images.Thumbnails._ID);
// Get the column index of the Thumbnails Image ID
columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
GridView gridview = (GridView) root.findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this.getActivity()));
//this.registerForContextMenu(gridview);
return root;
}
还有我的 Gridview 适配器:
private class ImageAdapter extends BaseAdapter {
private Context context;
public ImageAdapter(Context localContext) {
context = localContext;
}
public int getCount() {
return cursor.getCount();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView picturesView;
if (convertView == null) {
picturesView = new ImageView(context);
// Move cursor to current position
cursor.moveToPosition(position);
// Get the current value for the requested column
int imageID = cursor.getInt(columnIndex);
// Set the content of the image based on the provided URI
picturesView.setImageURI(Uri.withAppendedPath(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
"" + imageID));
picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER);
picturesView.setPadding(8, 8, 8, 8);
picturesView
.setLayoutParams(new GridView.LayoutParams(100, 100));
} else {
picturesView = (ImageView) convertView;
}
return picturesView;
}
}
打开此片段后,我收到以下 LogCat 错误:
09-23 16:25:06.286: E/AndroidRuntime(28497): FATAL EXCEPTION: main
09-23 16:25:06.286: E/AndroidRuntime(28497): java.lang.NullPointerException
09-23 16:25:06.286: E/AndroidRuntime(28497): at com.meme.hdmeme.MyMemeFragment.onCreateView(MyMemeFragment.java:51)
09-23 16:25:06.286: E/AndroidRuntime(28497): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
09-23 16:25:06.286: E/AndroidRuntime(28497): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
09-23 16:25:06.286: E/AndroidRuntime(28497): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
09-23 16:25:06.286: E/AndroidRuntime(28497): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
09-23 16:25:06.286: E/AndroidRuntime(28497): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
09-23 16:25:06.286: E/AndroidRuntime(28497): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
09-23 16:25:06.286: E/AndroidRuntime(28497): at android.os.Handler.handleCallback(Handler.java:615)
09-23 16:25:06.286: E/AndroidRuntime(28497): at android.os.Handler.dispatchMessage(Handler.java:92)
09-23 16:25:06.286: E/AndroidRuntime(28497): at android.os.Looper.loop(Looper.java:137)
09-23 16:25:06.286: E/AndroidRuntime(28497): at android.app.ActivityThread.main(ActivityThread.java:4921)
09-23 16:25:06.286: E/AndroidRuntime(28497): at java.lang.reflect.Method.invokeNative(Native Method)
09-23 16:25:06.286: E/AndroidRuntime(28497): at java.lang.reflect.Method.invoke(Method.java:511)
09-23 16:25:06.286: E/AndroidRuntime(28497): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
09-23 16:25:06.286: E/AndroidRuntime(28497): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
09-23 16:25:06.286: E/AndroidRuntime(28497): at dalvik.system.NativeStart.main(Native Method)
第 51 行是:
columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);