Nullpointerexcepiton在光标上,同时从对话框片段上的图库中选择照片 [英] Nullpointerexcepiton on cursor while selecting photo from gallery on dialog fragment
本文介绍了Nullpointerexcepiton在光标上,同时从对话框片段上的图库中选择照片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试通过 DialogFragment
从图库中选择照片。但是在初始化 cursor
时,我得到 nullpointerexception
。有什么想法为什么会得到这个错误?
I'm trying to select photo from gallery through DialogFragment
. But I'm getting nullpointerexception
while initializing cursor
. Any ideas why getting this error?
以下是我的代码:
if (resultCode == Activity.RESULT_OK) {
Uri selectedImage = imageReturnedIntent.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
// Nullpointerexcepiton on this line
Cursor cursor = getActivity().getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
cursor.close();
}
这是我的logcat错误:
Here is my logcat error :
03-24 12:34:37.645: E/AndroidRuntime(21479): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65538, result=-1, data=Intent { dat=content://media/external/images/media/3890 flg=0x1 }} to activity {com.example/com.example.MainActivity}: java.lang.NullPointerException
03-24 12:34:37.645: E/AndroidRuntime(21479): at android.app.ActivityThread.deliverResults(ActivityThread.java:3462)
03-24 12:34:37.645: E/AndroidRuntime(21479): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3505)
03-24 12:34:37.645: E/AndroidRuntime(21479): at android.app.ActivityThread.access$1100(ActivityThread.java:150)
03-24 12:34:37.645: E/AndroidRuntime(21479): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1346)
03-24 12:34:37.645: E/AndroidRuntime(21479): at android.os.Handler.dispatchMessage(Handler.java:99)
03-24 12:34:37.645: E/AndroidRuntime(21479): at android.os.Looper.loop(Looper.java:213)
03-24 12:34:37.645: E/AndroidRuntime(21479): at android.app.ActivityThread.main(ActivityThread.java:5225)
03-24 12:34:37.645: E/AndroidRuntime(21479): at java.lang.reflect.Method.invokeNative(Native Method)
03-24 12:34:37.645: E/AndroidRuntime(21479): at java.lang.reflect.Method.invoke(Method.java:525)
03-24 12:34:37.645: E/AndroidRuntime(21479): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
03-24 12:34:37.645: E/AndroidRuntime(21479): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
03-24 12:34:37.645: E/AndroidRuntime(21479): at dalvik.system.NativeStart.main(Native Method)
03-24 12:34:37.645: E/AndroidRuntime(21479): Caused by: java.lang.NullPointerException
03-24 12:34:37.645: E/AndroidRuntime(21479): at com.example.MainDialogFragment.onActivityResult(MainDialogFragment.java:226)
03-24 12:34:37.645: E/AndroidRuntime(21479): at com.example.MainActivity.onActivityResult(DelictActivity.java:85)
03-24 12:34:37.645: E/AndroidRuntime(21479): at android.app.Activity.dispatchActivityResult(Activity.java:5322)
03-24 12:34:37.645: E/AndroidRuntime(21479): at android.app.ActivityThread.deliverResults(ActivityThread.java:3458)
03-24 12:34:37.645: E/AndroidRuntime(21479): ... 11 more
推荐答案
如何开始你的活动,从哪里开始?
如果你通过MainActivity传递你的结果,你可以尝试在你的DialogFragment中创建一个新的函数,如下所示:
How do you start your Activity and from where? If you pass your result trough your MainActivity, you can try to make a new function in your DialogFragment like this:
public void onMyActivityResult(Context main, int resultCode... an so on){
main.getContentResolver.......
}
编辑:
i这样做:
i have done it in this way:
在onCreate中获取活动:
Get the Activity in onCreate:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mActivity = (MainActivity) this.getActivity();
}
那么:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_GALLERY && resultCode == Activity.RESULT_OK) {
...
Uri selectedImage = data.getData();
String path = getRealPathFromURI(selectedImage);
...
reloadImages();
}
super.onActivityResult(requestCode, resultCode, data);
}
和:
private String getRealPathFromURI(Uri contentURI) {
Cursor cursor = mActivity.getContentResolver().query(contentURI, null, null, null, null);
if (cursor == null) { // Source is Dropbox or other similar local file
// path
return contentURI.getPath();
} else {
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
String path = cursor.getString(idx);
cursor.close();
return path;
}
}
这篇关于Nullpointerexcepiton在光标上,同时从对话框片段上的图库中选择照片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文