2017-07-28 77 views
0

问题:我尝试在Android上安装apks时出现故障。 这部分是错误信息。Android安装apk在Android上的错误N

显示java.lang.NullPointerException:尝试调用虚拟方法 “android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang中。字符串)'空对象引用 在 android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:583) at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:557) at
android.support.v4.content.FileProvider.getUr iForFile(FileProvider.java:399) 在 me.hades.androidsafer.utils.SystemUtils.installApk(SystemUtils.java:59) 在 me.hades.androidsafer.utils.SystemUtils.installApk(SystemUtils.java:51) 在 me.hades.androidsafer.activity.SplashActivity $ 2.completed(SplashActivity.java:154) 在 com.liulishuo.filedownloader.FileDownloadMessenger.handoverMessage(FileDownloadMessenger.java:341) 在 com.liulishuo.filedownloader。 FileDownloadMessageStation $ UIHandlerCallback.dispose(FileDownloadMessageStation.java:169) at com.liu lishuo.filedownloader.FileDownloadMessageStation $ UIHandlerCallback.handleMessage(FileDownloadMessageStation.java:160) at android.os.Handler.dispatchMessage(Handler.java:98) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java: 865) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

该p艺术的Manifest.xml

<provider 
      android:name="android.support.v4.content.FileProvider" 
      android:authorities="me.hades.androidsafer.fileprovider" 
      android:exported="false" 
      android:grantUriPermissions="true"> 

      <meta-data 
       android:name="android.support.FILE_PROVIDER_PATHS" 
       android:resource="@xml/file_paths"/> 

    </provider> 

这部分是@ XML/file_paths.xml

<?xml version="1.0" encoding="utf-8"?> 
<paths> 

    <external-path path="Download/" name="Download" /> 
    <external-files-path name="Download" path="Download/" /> 
</paths> 

installApk()函数:

public static void installApk(Context context,File file) { 
     Intent intent = new Intent(Intent.ACTION_VIEW); 

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { 
      intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
      Uri contentUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileProvider", file); 
      intent.setDataAndType(contentUri, "application/vnd.android.package-archive"); 
     } else { 
      intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive"); 
      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     } 
     context.startActivity(intent); 
    } 

我怎么做才能解决这个问题? 我的英文不好。认为

+0

它看起来像'context'为空。 –

回答

0

检查这个Question可能会对你有所帮助。问题出在您的文件提供程序中。

+1

谢谢,我试了一下 –

0

最近我有这个问题,并最终我得到了它与下面的代码工作:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { 
        Uri apkUri = FileProvider.getUriForFile(activity, BuildConfig.APPLICATION_ID + ".provider", file); 
        Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE); 
        intent.setData(apkUri); 
        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
        activity.startActivity(intent); 
       } 

尝试使用:Intent.ACTION_INSTALL_PACKAGE。

我希望它能帮助你。