从导出的Unity项目调用Android Activity,NullPointerException

时间:2018-03-12 10:01:31

标签: android unity3d

我已导出Android Unity项目以创建和Android Studio项目(详见此处:Exporting and running Unity3D project to Android Studio

虽然该项目现在将通过Android Studio运行,但我无法加载导入项目后创建的Activity。

我想知道这可能吗?

我在Android中调用一个新类时检查了NullPointerException的其他解决方案,并且无法看到任何相关问题,让我怀疑我对这个问题的解决方法是否正确

Unity项目是一个简单的按钮,可以在尝试加载新活动 AndroidTestActivity 之前访问 UnityPlayerActivity 中的方法(通过Log进行验证)一个TextView。

代码如下

Unity C#:

public void Click () {

    Debug.Log("Clicked");

    AndroidJavaObject jo = new AndroidJavaObject("com.Test.AndroidTest.UnityPlayerActivity");
    jo.Call("Created");
}

UnityPlayerActivity.java

public void Created(){
    Log.e("tag", "Created");

    Intent i = new Intent(this, AndroidTestActivity.class);
    this.startActivity(i);
}

LogCat输出:

E/tag: Created
E/Unity: AndroidJavaException: java.lang.NullPointerException: Attempt to 
invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference
     java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference
         at android.app.Activity.startActivityForResult(Activity.java:4400)
         at android.app.Activity.startActivityForResult(Activity.java:4358)
         at android.app.Activity.startActivity(Activity.java:4682)
         at android.app.Activity.startActivity(Activity.java:4650)
         at com.Test.AndroidTest.UnityPlayerActivity.Created(UnityPlayerActivity.java:39)
         at com.unity3d.player.UnityPlayer.nativeRender(Native Method)
         at com.unity3d.player.UnityPlayer.c(Unknown Source)
         at com.unity3d.player.UnityPlayer$c$1.handleMessage(Unknown Source)
         at android.os.Handler.dispatchMessage(Handler.java:98)
         at android.os.Looper.loop(Looper.java:154)

我还确保在Android清单中声明了新活动,并且它已在UnityPlayerActivity中导入

0 个答案:

没有答案