Search Unity

  1. Unity 2018.1 has arrived! Read about it here
    Dismiss Notice
  2. Scriptable Render Pipeline improvements, Texture Mipmap Streaming, and more! Check out what we have in store for you in the 2018.2 Beta.
    Dismiss Notice
  3. If you couldn't join the live stream, take a peek at what you missed.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  5. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  6. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Trying to create a simple AlertDialog plugin, crash occurs

Discussion in 'Android' started by fractivSammy, Jun 11, 2011.

  1. fractivSammy

    fractivSammy

    Joined:
    May 26, 2011
    Posts:
    10
    I have a simple plugin written in Java that displays an alert dialog, like this:

    Code (csharp):
    1.  
    2. public class TestAlert {
    3.    
    4.     public boolean dialogResult = false;
    5.    
    6.     public void ShowTestDialog( Activity activity ) {
    7.         AlertDialog.Builder builder = new AlertDialog.Builder( activity );
    8.         builder.setMessage( "This is a test" )
    9.                 .setCancelable(false);
    10.                 .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
    11.                     public void onClick(DialogInterface dialog, int id) {
    12.                         dialogResult = true;
    13.                         dialog.dismiss();
    14.                     }
    15.                 })
    16.                 .setNegativeButton("No", new DialogInterface.OnClickListener() {
    17.                     public void onClick(DialogInterface dialog, int id) {
    18.                         dialogResult = false;
    19.                         dialog.cancel();
    20.                     }
    21.                 });
    22.         AlertDialog alert = builder.create();
    23.         alert.show();
    24.     }
    25. }
    26.  
    I am then calling this function from Unity as follows:

    Code (csharp):
    1.  
    2.     AndroidJNI.AttachCurrentThread();      // not sure if this is necessary or not, doesn't seem to make a difference
    3.  
    4.     var activity = new AndroidJavaClass( "com.unity3d.player.UnityPlayer" );
    5.     var currentActivity = activity.GetStatic.<AndroidJavaObject>( "currentActivity" );
    6.     var test = new AndroidJavaObject( "fractiv.test.TestAlert" );
    7.     test.Call( "ShowTestDialog", currentActivity );
    8.  
    On the "ShowTestDialog" call, I get the following exception:

    Code (csharp):
    1.  
    2. java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
    3.  
    Is this error occuring because I am not calling ShowTestDialog from the main thread? How can I fix this?

    I am running Unity 3.4b3.

    Thanks,
    Sam McGrath
     
    Last edited: Jun 11, 2011
  2. fractivSammy

    fractivSammy

    Joined:
    May 26, 2011
    Posts:
    10
    I just learned about Activity.runOnUiThread and also the 'post' function of messagequeue. I'm guessing one of these will fix my problem, but I won't be able to test it until I'm back in the office on Monday.
     
  3. benni05

    benni05

    Joined:
    Mar 17, 2011
    Posts:
    58
    Yes, that should fix it, declare a

    Code (csharp):
    1. private Handler handler;
    in your activity.

    In the onCreate method of the activity:

    Code (csharp):
    1. handler = new Handler(this);
    Let your activity implement Handler.Callback.

    And then call from Unity a method in your activity that sends a message to the handler
    Code (csharp):
    1.  
    2. handler.sendEmptyMessage(0);
    In the implemented handleMessage method of your Activity you can deal with the message and finally change the UI:
    Code (csharp):
    1.  
    2. public boolean handleMessage(Message msg) {
    3.   if (msg.what == 0) {
    4.      ...
    5.   }
    6. }
    7.  
    Ben
     
  4. fractivSammy

    fractivSammy

    Joined:
    May 26, 2011
    Posts:
    10
    I got this working without having to extend the Activity at all. I just derived a Runnable class to house my code and parameters, then invoked it using runOnUiThread(). Works great!
     
  5. DanTreble

    DanTreble

    Joined:
    Aug 31, 2010
    Posts:
    585
    Same, lost about a day before I worked the runOnUiThread out. The crash you get gives no clues at all