1. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice
  2. We're looking for feedback on Unity Starter Kits! Let us know what you’d like.
    Dismiss Notice
  3. We’re giving 2017.1 beta testers a chance to win t-shirts and a Nintendo Switch. Read more on the blog.
    Dismiss Notice
  4. We want to know how you learned Unity! Help us by taking this quick survey and have a chance at a $25 gift card
    Dismiss Notice
  5. Are you an artist or level designer going to Unite Europe? Join our roundtables there to discuss artist features.
    Dismiss Notice
  6. Unity 5.6 is now released.
    Dismiss Notice
  7. Check out all the fixes for 5.6 on the patch releases 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:
    576
    Same, lost about a day before I worked the runOnUiThread out. The crash you get gives no clues at all