[Solved]Unity Call Java non static method error,Search for google infomation but does not solve

Hello,everyone.The problem is this, I can call the static function but can not call non-static function.
My Java code:

public class Main extends UnityPlayerActivity {

    public static Context mContext;
   @override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext=this;
   
        Log.d("Unity", "OnCreate...........");
    }
    public int nonIntFunc(int a,int b)
    {
        Log.d("Unity", "Call non IntFunc...........");
        return a+b;
    }
   }

My Unity Code:

void Start () {

        Debug.Log("1->start.....");
 
        jc = new AndroidJavaClass("com.panda.easyvr.Main");

        jo = jc.GetStatic<AndroidJavaObject>("mContext");

        int m = jo.Call<int>("nonIntFunc", new object[] { 3, 2 });
   
        Debug.Log("Call end! ->"+m);
   
    }

debug log message is always show m value is 0.
I have spent several hours, but the problem is still not resolved. Any available information very grateful!

Use code tags or no one’s going to bother even trying to read that.

Thank you!:wink:

int m=jo.Call(“nonIntFunc”,2,3);

Thank you JFo, I will try!

Hello,JFo. when jo = jc.GetStatic(“mContext”); find logcat error message"
Exception: JNI: Init’d AndroidJavaObject with null ptr! ", can you tell me how to modify ? Thank you .

It looks like you’re trying to call nonIntFunc on jo, which is the mContext field, which is a Context object. So that function doesn’t exist. You should be calling it on jc, which is the Main class.

Thanks makeshiftwings. change code to:

public class Test : MonoBehaviour {

    AndroidJavaClass jc;
    AndroidJavaObject jo;

    void Start () {

        Debug.Log("1->start.....");
    
        jc = new AndroidJavaClass("com.panda.easyvr.Main");

        //jo = jc.GetStatic<AndroidJavaObject>("mContext");

        int m = jc.Call<int>("nonIntFunc", 2, 3);
       
        Debug.Log("Call end! ->"+m);
      
    }
}

always return m is 0,the logcat message show “I/Unity (32166): Call end! ->0”; :frowning:

Oh, I think you also want to use AndroidJavaObject rather than AndroidJavaClass for “Main” as well:

AndroidJavaObject jc;
//.........stuff.....
jc = new AndroidJavaObject("com.panda.easyvr.Main");
//...more stuff...

th

thanks,but logcat show error message“I/Unity ( 1436): AndroidJavaException: java.lang.RuntimeException: Can’t create handler inside thread that has not called Looper.prepare()”