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

Error call function from android plugin when Java class extends Activity

Discussion in 'Android' started by Hussam, May 30, 2014.

  1. Hussam

    Hussam

    Joined:
    Sep 20, 2012
    Posts:
    10
    Hello guys .

    I am programming custom android plugin and i want to call functions (static and non-static) from the plugin .
    when i call the function in c# (code bellow) from normal java class (that doesn't extends any other class) inside the JAR plugin file every thing is working perfect

    Code (csharp):
    1.  
    2. using (AndroidJavaObject cls = new AndroidJavaObject("com.BlackSowrd.androidplugin.AndroidController"))
    3.                 {
    4.                     if (cls != null)
    5.                     {
    6.                         cls.Call("ShowAlert");
    7.                     }
    8.                 }
    9.  
    But the problem occur when i make the java class extends Activity and try to call function using AndroidJavaObject , it throws exception


    Java.lang.RuntimeException : Can't create handler inside thread that has not called Looper.prepare()


    I searched for solution for that but i did not able to solve this issue , Please guys I need help :confused::confused: .
     
  2. bitter

    bitter

    Unity Technologies

    Joined:
    Jan 11, 2012
    Posts:
    530
    Hi,

    First of all, you are not really supposed to instantiate activities on your own. You normally use Intents to create/instantiate activities. Secondly, basically any UI related work has to happen on the android UI thread. No other thread is allowed to change the layout or popup a dialog or add an additional view, etc.

    You can find an example on how to schedule work on the ui thread from c# here:
    http://docs.unity3d.com/ScriptReference/AndroidJavaRunnable.html

    That said, I highly recommend reading the developer introductions at google before you start messing around with Activities - it will make things much clearer. At least "App Fundamentals", "Intents and Intent Filters" and "Activities" sections.

    http://developer.android.com/guide/index.html
     
  3. Hussam

    Hussam

    Joined:
    Sep 20, 2012
    Posts:
    10
    Thanks bittter for the fast replay i really appreciate it.

    I added new class for my android plugin which doesn't extend Activity class and it works fine I can call any function inside that class in unity but, I tried to start activity from this class and it did not worked. it gives me this exception from the unity side

    Java.lang.ClassNotFoundException : com.BlackSowrd.AndroidPlugin.MainActivity

    I also tried using the AndroidJavaRunnable and it also execute without any exceptions but it did not call the java function (From java class which extends Activity) and this is how i did it

    Code (csharp):
    1.  
    2.                 AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    3.                 AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
    4.                 activity.Call("runOnUiThread", new AndroidJavaRunnable(runOnUiThread));
    5.  
    6.  
    7.     void runOnUiThread()
    8.     {
    9.         using (AndroidJavaObject cls = new AndroidJavaObject("com.BlackSowrd.AndroidPlugin.MainActivity"))
    10.         {
    11.             if (cls != null)
    12.             {
    13.                 cls.Call("ShowAlert");
    14.             }
    15.         }
    16.     }
    17.  
    I also read the link for the android developer and it make many things clear for me :) . but i don't know how i am gonna solve it :cry:
    can you please explain in more details with code please.
     
  4. bitter

    bitter

    Unity Technologies

    Joined:
    Jan 11, 2012
    Posts:
    530
    package names are case-sensitive and it's recommend to always use lowercase for package names to avoid collisions in case insensitive filesystems