
我收到此错误.我是android studio的新手,我需要创建此插件以统一在运行时安装apk
错误-尝试在空对象引用上调用虚拟方法’androID.content.Context Android.content.Context.getApplicationContext()’
插件类-
package com.example.unitypluginappinstall;import androID.app.Activity;import androID.content.Context;import androID.content.Intent;import androID.net.Uri;import androID.support.v4.content.fileProvIDer;import java.io.file;public class PluginClass extends Activity {static String errMessage;public static String InstallApp(String ApkPath){ try { errMessage = "test"; file toInstall = new file(ApkPath); Uri apkUri = fileProvIDer.getUriForfile(ContextClass.context(), ContextClass.context().getApplicationContext().getPackagename() + ".fileprovIDer", toInstall); Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE); intent.setData(apkUri); intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); ContextClass.context().startActivity(intent); } catch (Exception e){ errMessage = e.getMessage(); } return errMessage;}}ContextClass-
package com.example.unitypluginappinstall;import androID.app.Application;import androID.content.Context;public class ContextClass extends Application {private static ContextClass instance;public ContextClass(){ instance = this;}public static ContextClass instance(){ return instance;}public static Context context() { return instance.getApplicationContext();}}清单文件-
<manifest xmlns:androID="http://schemas.androID.com/apk/res/androID"package="com.example.unitypluginappinstall"><application androID:allowBackup="true" androID:label="@string/app_name" androID:supportsRtl="true"> <provIDer androID:name="androID.support.v4.content.fileProvIDer" androID:authoritIEs="${applicationID}.fileprovIDer" androID:exported="false" androID:grantUriPermissions="true"> <Meta-data androID:name="androID.support.file_PROVIDER_PATHS" androID:resource="@xml/filepaths"></Meta-data> </provIDer> androID:name=".ContextClass"></application>PluginWrapper:
public class PluginWrapper : MonoBehavIoUr {string savePath;voID Start () { savePath = Path.Combine(Application.persistentDataPath, "data"); savePath = Path.Combine(savePath, "applaunchtest.apk"); Install();}voID Install(){try { //Install APK GameObject.Find("TextDeBUG").GetComponent<Text().text"Installing..."; var plugin = new AndroIDJavaClass("com.example.unitypluginappinstall.PluginClass"); GameObject.Find("TextDeBUG").GetComponent<Text>().text = plugin.CallStatic<string>("InstallApp", savePath); } catch(Exception e) { GameObject.Find("TextDeBUG").GetComponent<Text>().text = e.Message; }}}编辑-
插件类
package com.example.unitypluginappinstall;import androID.app.Activity;import androID.content.Context;import androID.content.Intent;import androID.net.Uri;import androID.support.v4.content.fileProvIDer;import java.io.file;public class PluginClass extends Activity {static String errMessage;public static String InstallApp(Context context, String ApkPath){ try { errMessage = "test"; file toInstall = new file(ApkPath); Uri apkUri = fileProvIDer.getUriForfile(context, context.getApplicationContext().getPackagename() + ".fileprovIDer", toInstall); Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE); intent.setData(apkUri); intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); context.startActivity(intent); } catch (Exception e){ errMessage = e.getMessage(); } return errMessage;}}插件包装器-
public class PluginWrapper : MonoBehavIoUr {string savePath;voID Start () { savePath = Path.Combine(Application.persistentDataPath, "data"); savePath = Path.Combine(savePath, "applaunchtest.apk"); Install();}voID Install(){try { //Install APKAndroIDJavaClass unityClass = new AndroIDJavaClass("com.unity3d.player.UnityPlayer"); AndroIDJavaObject unityActivity = unityClass.GetStatic<AndroIDJavaObject>("currentActivity"); AndroIDJavaObject unityContext = unityActivity.Call<AndroIDJavaObject>("getApplicationContext"); GameObject.Find("TextDeBUG").GetComponent<Text().text"Installing..."; var plugin = new AndroIDJavaClass("com.example.unitypluginappinstall.PluginClass"); GameObject.Find("TextDeBUG").GetComponent<Text>().text = plugin.CallStatic<string>("InstallApp",unityContext, savePath); } catch(Exception e) { GameObject.Find("TextDeBUG").GetComponent<Text>().text = e.Message; }}}编辑
解决方法:
来自Java的Contect插件为null.如果需要,您必须将Unity的上下文发送到Java插件.
添加另一个接收上下文的参数,然后使用该参数中的该上下文而不是AndroID.content.Context.getApplicationContext()中的那个.
Java的:
public static String InstallApp(Context context, String ApkPath){ try { errMessage = "test"; file toInstall = new file(ApkPath); Uri apkUri = fileProvIDer.getUriForfile(context, context.getPackagename() + ".fileprovIDer", toInstall); Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE); intent.setData(apkUri); intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); context.startActivity(intent); } catch (Exception e){ errMessage = e.getMessage(); } return errMessage;}C#:
发送Unity上下文和要传递给函数的路径.
voID Install(){ try { GameObject.Find("TextDeBUG").GetComponent<Text>().text = "Installing..."; //Get Unity Context AndroIDJavaClass unityClass = new AndroIDJavaClass("com.unity3d.player.UnityPlayer"); AndroIDJavaObject unityActivity = unityClass.GetStatic<AndroIDJavaObject>("currentActivity"); AndroIDJavaObject unityContext = unityActivity.Call<AndroIDJavaObject>("getApplicationContext"); AndroIDJavaClass plugin = new AndroIDJavaClass("com.example.unitypluginappinstall.PluginClass"); string result = plugin.CallStatic<string>("InstallApp", unityContext, savePath); } catch (Exception e) { GameObject.Find("TextDeBUG").GetComponent<Text>().text = e.Message; }}您可能有其他不相关的错误,但是Context null问题应该消失了.
编辑:
如果收到异常:
Attempt to invoke virtual method
‘androID.content.res.XmlResourceParser
androID.content.pm.packageItemInfo.loadXmlMetaData(androID.content.pm.PackageManager.java.lang.String)’
您只需要做几件事.
1.将“ .jar”插件插入UnityProject / Assets / Plugins / AndroID目录.
2.从“ AndroIDSDK / extras / androID / support / v4 / androID-support-v4.jar”中复制“ androID-support-v4.jar”
目录到您的“ UnityProject / Assets / Plugins / AndroID”目录.
3.在UnityProject / Assets / Plugins / AndroID目录中创建一个名为“ AndroIDManifest.xml”的文件,并将下面的代码放入其中.
确保用您自己的包名称替换“ com.company.product”.有2种情况出现.您必须同时替换它们:
这些可以在package =“ com.company.product”和androID:authoritIEs =“ com.company.product.fileprovIDer”中找到.不要更改或删除“文件提供程序”,也不要更改其他任何内容.
这是“ AndroIDManifest.xml”文件:
<?xml version="1.0" enCoding="utf-8"?><manifest xmlns:androID="http://schemas.androID.com/apk/res/androID" package="com.company.product" xmlns:tools="http://schemas.androID.com/tools" androID:installLocation="preferExternal" androID:versionname="1.0" androID:versionCode="1"> <supports-screens androID:smallScreens="true" androID:normalScreens="true" androID:largeScreens="true" androID:xlargeScreens="true" androID:anyDensity="true" /> <application androID:theme="@style/UnitythemeSelector" androID:icon="@drawable/app_icon" androID:label="@string/app_name" androID:deBUGgable="true"> <activity androID:name="com.unity3d.player.UnityPlayerActivity" androID:label="@string/app_name"> <intent-filter> <action androID:name="androID.intent.action.MAIN" /> <category androID:name="androID.intent.category.LAUNCHER" /> </intent-filter> <Meta-data androID:name="unityplayer.UnityActivity" androID:value="true" /> </activity> <provIDer androID:name="androID.support.v4.content.fileProvIDer" androID:authoritIEs="com.company.product.fileprovIDer" androID:exported="false" androID:grantUriPermissions="true"> <Meta-data androID:name="androID.support.file_PROVIDER_PATHS" androID:resource="@xml/provIDer_paths"/> </provIDer> </application> <uses-sdk androID:minSdkVersion="16" androID:targetSdkVersion="23" /></manifest>4.在“ UnityProject / Assets / Plugins / AndroID / res / xml”目录中创建一个名为“ provIDer_paths.xml”的新文件,并将代码放在下面.如您所见,您必须创建一个res,然后创建一个xml文件夹.
确保用您自己的包名称替换“ com.company.product”.
它只出现一次.
这是您应该放入此“ provIDer_paths.xml”文件的内容:
<?xml version="1.0" enCoding="utf-8"?><paths xmlns:androID="http://schemas.androID.com/apk/res/androID"> <!--<external-path name="external_files" path="."/>--> <external-path path="AndroID/data/com.company.product" name="files_root" /> <external-path path="." name="external_storage_root" /></paths>而已.
总结以上是内存溢出为你收集整理的错误:java.lang.NullPointerException:尝试调用虚拟方法’android.content.res.XmlResourceParser全部内容,希望文章能够帮你解决错误:java.lang.NullPointerException:尝试调用虚拟方法’android.content.res.XmlResourceParser所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)