使用intent打开PDF后,应用程序崩溃并出现NullPointerException

时间:2013-10-25 14:05:36

标签: android pdf android-intent

我有一个应用程序,显示带有多个文档的ListView(DOC,PDF,PPT ...)。选择其中之一,我使用以下代码来显示文档:

Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
String extension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(outFile).toString());
String mimetype = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
if (extension.equalsIgnoreCase("") || mimetype == null) {
    intent.setDataAndType(Uri.fromFile(outFile), "text/*");
} else {
    intent.setDataAndType(Uri.fromFile(outFile), mimetype);            
}
startActivity(Intent.createChooser(intent, "Choose an Application:"));                           

对于所选的第一个文档,Polaris Office打开并显示文档很好。当切换回选择下一个文档时,我的应用程序不在显示的最后一个屏幕上,而是再次启动。选择文档可以在Polaris中正常工作并打开。当现在切换回来时,应用程序崩溃,无法启动活动... MainActivity ... NullPointerExpetion。

我该怎么做才能解决这个问题?

这里是LogCat:

 10-25 10:07:43.476: D/AndroidRuntime(8396): Shutting down VM
 10-25 10:07:43.476: W/dalvikvm(8396): threadid=1: thread exiting with uncaught    exception (group=0x415092a0)
 10-25 10:07:43.507: E/AndroidRuntime(8396): FATAL EXCEPTION: main
 10-25 10:07:43.507: E/AndroidRuntime(8396): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.astrex.ppastrex/com.astrex.ppastrex.MainActivity}: java.lang.NullPointerException
 10-25 10:07:43.507: E/AndroidRuntime(8396):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
 10-25 10:07:43.507: E/AndroidRuntime(8396):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2122)
 10-25 10:07:43.507: E/AndroidRuntime(8396):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
 10-25 10:07:43.507: E/AndroidRuntime(8396):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228)
 10-25 10:07:43.507: E/AndroidRuntime(8396):    at android.os.Handler.dispatchMessage(Handler.java:99)
 10-25 10:07:43.507: E/AndroidRuntime(8396):    at android.os.Looper.loop(Looper.java:137)
 10-25 10:07:43.507: E/AndroidRuntime(8396):    at android.app.ActivityThread.main(ActivityThread.java:4895)
 10-25 10:07:43.507: E/AndroidRuntime(8396):    at java.lang.reflect.Method.invokeNative(Native Method)
 10-25 10:07:43.507: E/AndroidRuntime(8396):    at java.lang.reflect.Method.invoke(Method.java:511)
 10-25 10:07:43.507: E/AndroidRuntime(8396):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
 10-25 10:07:43.507: E/AndroidRuntime(8396):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
 10-25 10:07:43.507: E/AndroidRuntime(8396):    at dalvik.system.NativeStart.main(Native Method)
 10-25 10:07:43.507: E/AndroidRuntime(8396): Caused by: java.lang.NullPointerException
 10-25 10:07:43.507: E/AndroidRuntime(8396):    at com.astrex.ppastrex.FrameDocumentos.onCreateView(FrameDocumentos.java:66)
 10-25 10:07:43.507: E/AndroidRuntime(8396):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
 10-25 10:07:43.507: E/AndroidRuntime(8396):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
 10-25 10:07:43.507: E/AndroidRuntime(8396):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
 10-25 10:07:43.507: E/AndroidRuntime(8396):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)
 10-25 10:07:43.507: E/AndroidRuntime(8396):    at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1877)
 10-25 10:07:43.507: E/AndroidRuntime(8396):    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:552)
 10-25 10:07:43.507: E/AndroidRuntime(8396):    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1178)
 10-25 10:07:43.507: E/AndroidRuntime(8396):    at android.app.Activity.performStart(Activity.java:5173)
 10-25 10:07:43.507: E/AndroidRuntime(8396):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2070)
 10-25 10:07:43.507: E/AndroidRuntime(8396):    ... 11 more

3 个答案:

答案 0 :(得分:0)

尝试这样:

 File file = new File(pdfpath);

 if (file.exists()) {
     Uri path = Uri.fromFile(file);
     Intent intent = new Intent(Intent.ACTION_VIEW);
     intent.setDataAndType(path, "application/pdf");
     intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

     try {
       startActivity(intent);
       finish();
     } 
     catch (ActivityNotFoundException e) {
     }
  }

答案 1 :(得分:0)

意图看起来它被正确调用它适用于某些人。好像你有一个空引用而不检查文件是否存在。

经验告诉我null是因为您的设备在打开pdf文件时内存不足,因此您的应用程序已关闭以释放一些内存。

解决方案是添加savedinstancestate方法,保存应用程序所需的内容,并检查oncreate方法中的savedinstancestate!= null,然后从savedinstancestate重新创建应用程序所需的内容。

答案 2 :(得分:0)

我在Android 6.0上遇到过类似的问题,但在4.4版本中没有,所以对于旧版设备,它会正常工作并且不会崩溃。

就我而言,我正在下载一个网址,保存文件并在第三方查看器中打开它们。

我发现只要我将pdf保存到下载目录,它就会根据我的情况进行修复。

File root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File dir = new File(root.getAbsolutePath());
if (dir.exists() == false) {
      dir.mkdirs();
}
String fileName = "filename.pdf";
file = new File(dir, fileName);