Android N

时间:2017-07-28 11:19:33

标签: java android xml

问题:我在Android N上尝试安装apk时失败了。 这部分是错误信息。

  

java.lang.NullPointerException:尝试调用虚方法   “android.content.res.XmlResourceParser   android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager,   在null对象引用上的'java.lang.String)'                                                                              在   android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:583)                                                                              在   android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:557)                                                                              在
  android.support.v4.content.FileProvider.getUriForFile(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)                                                                              在   com.liulishuo.filedownloader.FileDownloadMessageStation $ UIHandlerCallback.handleMessage(FileDownloadMessageStation.java:160)                                                                              在android.os.Handler.dispatchMessage(Handler.java:98)                                                                              在android.os.Looper.loop(Looper.java:154)                                                                              在android.app.ActivityThread.main(ActivityThread.java:6077)                                                                              at java.lang.reflect.Method.invoke(Native Method)                                                                              在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:865)                                                                              在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

这部分是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);
    }

我该如何解决此错误? 我的英语不太好。认为

3 个答案:

答案 0 :(得分: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。

我希望它可以帮到你。

答案 2 :(得分:0)

您在问题中包含的代码中的问题是,您的authority字符串参数(传递到FileProvider.getUriForFile)并不匹配。在AndroidManifest.xml文件中,您指定了.fileprovider的后缀,但在installApk方法中指定了后缀.fileProvider。它区分大小写,需要匹配。