AndroidJAVAClass无法执行我的非静态功能

时间:2012-05-22 11:31:46

标签: android java-native-interface unity3d

我从我的统一代码中调用java函数。 我有静电和放大器我的java类中的非静态函数类型 要访问第一个静态功能,我使用了:

AndroidJavaClass plugin = new AndroidJavaClass("com.android.test.TestActivity");
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");        
plugin.CallStatic("InstantiateMe", qwe);

上面的代码运行正常,但在此之后,当我试图执行我的非静态函数时,它实际上没有执行..甚至没有给出任何异常或错误..

我调用非静态函数的代码

AndroidJavaClass plugin = new AndroidJavaClass("com.android.test.TestActivity");
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");        
plugin.Call("Enable", qwe);

帮帮我吧。我坚持到这里。

1 个答案:

答案 0 :(得分:6)

将“activity”变量用于非静态调用:activity.Call("Enable", qwe); 有静态和非静态函数调用的例子:

public class Caller {
  private readonly AndroidJavaClass _ActivityClass;
  private readonly AndroidJavaObject _ActivityObject;
  private readonly AndroidJavaClass _MyActivityClass; 

  public Caller() {
    _ActivityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    _MyActivityClass = new AndroidJavaClass("com.example.u2aTest.MyMainActivity"); 

    _ActivityObject = _ActivityClass.GetStatic("currentActivity");
    // AndroidJavaObject my_activity_object = _MyActivityClass.GetStatic("currentActivity"); //doesn't work
  }

  public void MakeTestCalls() {
    _ActivityObject.Call("testVoid"); // works fine
    //_ActivityClass.CallStatic("testVoidStatic"); // doesn't works
   _MyActivityClass.CallStatic("testVoidStatic"); // works fine
    //_ActivityObject.CallStatic("testVoid"); // doesn't work on some divices

   //public int testInt(int intValue)
   int iresult = _ActivityObject.Call<int>("testInt", 999);

   //public String testStringString(String paramValue)
   String sresult = _ActivityObject.Call<String>("testStringString", "s2");      

   //public static int testIntStatic(int intValue) 
   iresult = _MyActivityClass.CallStatic<int>("testIntStatic", 999);

   //public static String testStringStringStatic(String paramValue)
   sresult =_MyActivityClass.CallStatic<String>("testStringStringStatic", "s1");
  }
}

查看example of Android plugin for Unity3D的详细信息。