Search Unity

Error calling android static method from Unity

Discussion in 'Android' started by kalathos16, Aug 17, 2016.

  1. kalathos16

    kalathos16

    Joined:
    Aug 17, 2016
    Posts:
    5
    Hi, I have this simplified android code:

    package com.ak.shimmer;
    import com.unity3d.player.UnityPlayerActivity;
    public class MainActivity extends UnityPlayerActivity{
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    .(etc)
    .}
    // call this method from unity
    public static void calledFromUnity(){
    System.out.println("calledFromUnity method is called");
    }
    }

    This C+ code in Unity:


    using UnityEngine;
    using System.Collections;
    using UnityEngine.UI;

    public class on_onClick:MonoBehaviour{

    public void shareText(){
    //executethebelowlinesifbeingrunonaAndroid device
    #ifUNITY_ANDROID
    var androidJC=new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    var jo=androidJC.GetStatic<AndroidJavaObject>("currentActivity");
    jo.CallStatic("calledFromUnity");
    #endif
    }
    }

    And I am getting this error in the logcat:

    08-17 17:51:58.245: I/Unity(14253): AndroidJavaException: java.lang.NoSuchMethodError: no static method "Lcom/unity3d/player/UnityPlayerActivity;.calledFromUnity()V"
     
  2. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    It looks like currentActivity returns UnityPlayerActivity, and not MainActivity (the one that you have defined).

    Are you sure you have set it up properly so that it will override Unity's default activity ?
     
  3. kalathos16

    kalathos16

    Joined:
    Aug 17, 2016
    Posts:
    5
    What do you mean with set it up?

    I let the android app running in background while I push the method´s button of my Unity app.

    I have followed these steps: http://forum.unity3d.com/threads/integrating-unity-and-eclipse.71607/
     
    Last edited: Sep 1, 2016
  4. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    I meant - if you declare your own activity (a Java class derived from Activity), you have to declare that this is the main activity you use for your game in the AndroidManifest.xml. Also - your class should derive from Unity's activity.

    Check out this link for more information: https://docs.unity3d.com/Manual/PluginsForAndroid.html