0

With the following code:

private void showDialog(String message) {
    try
    {
    Looper.prepare();
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            try {
                Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                Ringtone r = RingtoneManager.getRingtone(context.getApplicationContext(), notification);
                r.play();
            } catch (Exception e) {
                e.printStackTrace();
                UserPrefs.setLogerForException(Log.getStackTraceString(e).toString(),
                        GlobalContext.Myglobalcontext,ApiConstants.Excption_Log_Message);
            }
            CustomAlert alertDialog = new CustomAlert();
            alertDialog.setTitle(context.getResources().getString(R.string.notification_tite));
            alertDialog.setMessage(msg.obj.toString());
            alertDialog.setAlertId(Events.MORE_INFO);
            alertDialog.setTextGravity(Gravity.LEFT);
            ReplicaPrefs.showAlert(alertDialog);
        }
    };

    Message msgObj = handler.obtainMessage();
    msgObj.obj = message    ;
    handler.handleMessage(msgObj);
    }
    catch(Exception ex)
    {
        UserPrefs.setLogerForException(Log.getStackTraceString(ex).toString(),
                GlobalContext.Myglobalcontext,ApiConstants.Excption_Log_Message);
    }

}

After some time (not the exact scenario) I have the following error:

java.lang.RuntimeException: Only one Looper may be created per thread at android.os.Looper.prepare

Does anyone have any idea where I went wrong?

Don't Panic
  • 41,125
  • 10
  • 61
  • 80
IGT
  • 9
  • 2

2 Answers2

0

I think that you should take a look on this to understand about the lopper: What is the purpose of Looper and how to use it?

Then, take a look on this: java.lang.RuntimeException: Only one Looper may be created per thread for finding the answer for your question.

Hope that help!

Community
  • 1
  • 1
Lạng Hoàng
  • 1,790
  • 3
  • 17
  • 32
0

Take note: Every Handler is bundle with one Looper, which only one Looper may be created per thread. So, you can create an Handler instance by constructor of Handler(Looper). When using Handler() to create one instance, the default Looper is from the Main UI Thread on which current Activitys run.

As for your codes above, you defined showDialog(String message) in an Activity, right? So, that means this method will be called on the Main UI Thread. Consequently, when you create a Handler instance by constructor of Handler() inside showDialog(String message), the Looper from the Main UI Thread will be bundled with the Handler instance by default. That means Looper.prepare(); is redundant and then removing Looper.prepare(); will solve your problem.

SilentKnight
  • 13,761
  • 19
  • 49
  • 78
  • Not exactly. `Handler()` uses the current thread and throws an exception if the current thread is not a `Looper`. That's normally, but not necessarily the main thread. – Kevin Krumwiede Dec 15 '15 at 03:00
  • Yes, `Handler()` will bundled with the `Looper` created by the `Thread` on which `showDialog(String message)` run. In this case, `showDialog(String message)` run on the **Main UI Thread**. – SilentKnight Dec 15 '15 at 03:31