createChooser空指针异常

时间:2014-02-25 21:55:55

标签: android

Uri selectedUri = Uri.fromFile(selected.getAbsoluteFile());
String fileExtension = MimeTypeMap.getFileExtensionFromUrl(selectedUri.toString());
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension);

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(selectedUri, mimeType);
Intent chooser = Intent.createChooser(intent, "Open File...");
startActivity(chooser);

此代码过去常常起作用,这意味着即使没有找到任何活动来处理意图,选择器对话框仍然会出现这样的情况。现在,如果没有找到填充选择器对话框的活动,此代码将抛出NullPointerException(版本4.3)。有没有其他人经历过这个或知道我可能做错了什么?

logcat的:

02-25 17:03:24.260: E/AndroidRuntime(13502): FATAL EXCEPTION: main
02-25 17:03:24.260: E/AndroidRuntime(13502): java.lang.RuntimeException: Unable to start activity ComponentInfo{android/com.android.internal.app.ChooserActivity}: java.lang.NullPointerException
02-25 17:03:24.260: E/AndroidRuntime(13502):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2517)
02-25 17:03:24.260: E/AndroidRuntime(13502):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2574)
02-25 17:03:24.260: E/AndroidRuntime(13502):    at android.app.ActivityThread.access$600(ActivityThread.java:162)
02-25 17:03:24.260: E/AndroidRuntime(13502):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1413)
02-25 17:03:24.260: E/AndroidRuntime(13502):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-25 17:03:24.260: E/AndroidRuntime(13502):    at android.os.Looper.loop(Looper.java:158)
02-25 17:03:24.260: E/AndroidRuntime(13502):    at android.app.ActivityThread.main(ActivityThread.java:5789)
02-25 17:03:24.260: E/AndroidRuntime(13502):    at java.lang.reflect.Method.invokeNative(Native Method)
02-25 17:03:24.260: E/AndroidRuntime(13502):    at java.lang.reflect.Method.invoke(Method.java:525)
02-25 17:03:24.260: E/AndroidRuntime(13502):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
02-25 17:03:24.260: E/AndroidRuntime(13502):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:843)
02-25 17:03:24.260: E/AndroidRuntime(13502):    at dalvik.system.NativeStart.main(Native Method)
02-25 17:03:24.260: E/AndroidRuntime(13502): Caused by: java.lang.NullPointerException
02-25 17:03:24.260: E/AndroidRuntime(13502):    at com.android.internal.app.ResolverActivity.checkListHeight(ResolverActivity.java:299)
02-25 17:03:24.260: E/AndroidRuntime(13502):    at com.android.internal.app.ResolverActivity.onCreate(ResolverActivity.java:219)
02-25 17:03:24.260: E/AndroidRuntime(13502):    at com.android.internal.app.ChooserActivity.onCreate(ChooserActivity.java:53)
02-25 17:03:24.260: E/AndroidRuntime(13502):    at android.app.Activity.performCreate(Activity.java:5195)
02-25 17:03:24.260: E/AndroidRuntime(13502):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
02-25 17:03:24.260: E/AndroidRuntime(13502):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2473)
02-25 17:03:24.260: E/AndroidRuntime(13502):    ... 11 more

0 个答案:

没有答案