Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to call a Java method with a parameter?

Discussion in 'Android' started by TOES2, Nov 17, 2015.

  1. TOES2

    TOES2

    Joined:
    May 20, 2013
    Posts:
    135
    My Unity code:

    Code (CSharp):
    1. // to get the activity
    2. AndroidJavaClass androidJC = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    3. AndroidJavaObject jo = androidJC.GetStatic<AndroidJavaObject>("currentActivity");
    4. // Accessing the class to call a static method on it
    5. AndroidJavaClass jc = new AndroidJavaClass("com.lf.clientsampleapp.MainActivity");
    6. // Calling a Call method to which the current activity is passed
    7. jc.CallStatic("Call", jo);
    My plugins Java code:

    Code (Java):
    1. public class MainActivity extends UnityPlayerActivity
    2. {
    3.     public static void Call(Activity activity)
    4.     {
    5.         // Creating an intent with the current activity and the activity we wish to start
    6.         Intent myIntent = new Intent(activity, MainActivity.class);
    7.         activity.startActivity(myIntent);
    8.     }
    9.  
    10.     public static void Call()
    11.     {
    12.         //Calling this method without a parameter works fine...
    13.     }
    14.  
    15.  
    This produces a NoSuchMethod exception. But if I call the same method without a parameter from Unity using

    Code (CSharp):
    1. jc.CallStatic("Call")
    it works fine. So, there is some mapping issue with the parameter.

    I tried other variants, like

    Code (CSharp):
    1. jc.CallStatic("Call", new object[] { jo });
    But still no go... How do I do this?
     
  2. TOES2

    TOES2

    Joined:
    May 20, 2013
    Posts:
    135
    Is this really impossible..?
     
  3. bitter

    bitter

    Unity Technologies

    Joined:
    Jan 11, 2012
    Posts:
    530
    That looks correct. Is there any information in the log?
     
  4. TOES2

    TOES2

    Joined:
    May 20, 2013
    Posts:
    135
    Just a NoSuchMethod exception... I have tried to call other methods with string parameters, and then it works. But the activity object won't map..
     
  5. bitter

    bitter

    Unity Technologies

    Joined:
    Jan 11, 2012
    Posts:
    530
    Odd! Could you please file a bug and post the case number here so I can investigate the issue? And so I don't forget ;-)
     
  6. Privateer

    Privateer

    Joined:
    Jun 5, 2016
    Posts:
    23
    I have a similar problem.
    I'm making a Unity wrapper for an Android library and I need to take an instance of a class that is defined inside the library, save it, and later, send it back as a parameter for a library method. I have no choice but to save it as an AndroidJavaObject. Could this be a problem when I try to pass it as a parameter? How can I solve this?