【发布时间】:2017-07-13 12:31:22
【问题描述】:
我收到此错误。我是 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;
}
}
上下文类 -
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>
插件包装器:
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 APK
AndroidJavaClass 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;
}
}
}
编辑
【问题讨论】:
-
在这里纠正
android:name=".ContextClass"> -
如何调用
InstallApp函数?如果来自 C#,请也发布该代码。 -
@Programmer 是的,我是从 C# 调用它...我已经添加了上面的代码
-
我认为你也应该添加 C# 标签。我很高兴您决定使用 Java 和 C# 来执行此操作。我会在不需要 Java 的那个版本上提供帮助,但我没有正确的版本。我现在看到了问题,很快就会给出我的答案。
-
@Programmer 谢谢你.. 我很感激 :)
标签: c# android unity3d android-context android-fileprovider