0

I'm trying to launch an Intent from the render method in libgdx but I am getting a "Can't create handler inside thread that has not called Looper.prepare()" error.

I have implemented the Interface from here http://code.google.com/p/libgdx-users/wiki/IntegratingAndroidNativeUiElements3TierProjectSetup

I have used the Toast implementaion and that works okay.

This is my Android implementation

 @Override
public void launchPlayerRoom() {
    Intent intent = new Intent(appContext, RoomViewActivity.class);
    intent.putExtras(selectPlayerRoom());
    startActivity(intent);
}

and calling from Libgdx render

if (health_amount <= 0){
            actionResolver.launchPlayerRoom();
        }

The Intent needs to be called from render as it depends on a value that is decremented in render. I understand that the problem is calling a UI thread from a render thread(I think!) but I don't know how to solve it. I have tried from this post Can't create handler inside thread that has not called Looper.prepare()

 @Override
public void launchPlayerRoom() {
    final Intent intent = new Intent(appContext, RoomViewActivity.class);
    intent.putExtras(selectPlayerRoom());
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            startActivity(intent);
        }
    });

But this makes no difference.

Any help would be most appreciated.

Community
  • 1
  • 1
jonnypotwash
  • 43
  • 1
  • 4

2 Answers2

2

The Libgdx "render" thread is not the Android "UI" thread, so when you invoke code in the Android back-end that requires the Android UI thread context you have to jump through some hoops.

In general, the solution is to create a Handler in the context of the UI thread and then post Runnables to that object. This is what the wiki page on integrating android UI elements is doing.

If you have the Toast implementation working correctly, then the Intent code should also work (both have the same requirement to be run on the Android UI thread context).

Perhaps there is some other problem with the Handler you're creating (is it not being created during the Libgdx create callback? (Its implicitly associated with the thread that created it.) Or are you invoking this code too early in your setup? A full backtrace might provide more details.

P.T.
  • 24,557
  • 7
  • 64
  • 95
  • Thanks,Tried '@Override public void launchPlayerRoom() { final Bundle extras = selectPlayerRoom(); uiThread.post(new Runnable() { @Override public void run() { Intent intent = new Intent(appContext, RoomViewActivity.class); intent.putExtras(extras); appContext.startActivity(intent); } }); }' but still no joy. Tried to launch actionResolver.launchPlayerRoom() from create, that also crashed with same error.The Toast works from render method, so I think the interface is ok. – jonnypotwash Sep 18 '13 at 10:05
0

I have now got it sorted. It turned out I needed to interrupt the render thread then I could launch the intent. I changed implementaion slightly as I call an AlertDialog and launch the intent from there. I have accepted the answer as it got me thinking along the right lines.

jonnypotwash
  • 43
  • 1
  • 4
  • 1
    Hey Hi @jonnypotwash, How you are displaying dialog inside Render method and how you r starting activity, Can you please share your code here, as I have the same requirement to start activity from Render method. – Salman Khan Jun 16 '14 at 12:44