以下是我的代码:
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); cursor.movetoFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); cursor.close(); }
这是我的logcat错误:
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.access00(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解决方法@H_404_24@ 你如何开始你的活动,从哪里开始?
如果您通过MainActivity传递结果,可以尝试在DialogFragment中创建一个新函数,如下所示:
public voID onMyActivityResult(Context main,int resultCode... an so on){ main.getContentResolver.......}
编辑:
我这样做了:
获取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); 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; } }总结
以上是内存溢出为你收集整理的android – Nullpointerexcepiton在光标上从对话框片段的画廊中选择照片全部内容,希望文章能够帮你解决android – Nullpointerexcepiton在光标上从对话框片段的画廊中选择照片所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)