Unable to create a handler inside a thread that did not call Looper.prepare () in the thread of the warning dialog

I had an error in my surge activity when he was checking the internet connection and there is no internet there. Perhaps this happened in my warning dialog.

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() at android.os.Handler.<init>(Handler.java:200) at android.os.Handler.<init>(Handler.java:114) at android.app.Dialog.<init>(Dialog.java:108) at android.app.Dialog.<init>(Dialog.java:148) at android.support.v7.app.AppCompatDialog.<init>(AppCompatDialog.java:43) at android.support.v7.app.AlertDialog.<init>(AlertDialog.java:95) at android.support.v7.app.AlertDialog$Builder.create(AlertDialog.java:927) at com.example.study.Splash.checking(Splash.java:66) at com.example.study.Splash$2.run(Splash.java:51) 

I tried runOnUiThread() but it still does not work. here is my spray code

 package com.example.study; import android.content.DialogInterface; import android.os.Bundle; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import com.example.study.helper.SessionManager; import com.example.study.util.ConnectionDetector; public class Splash extends AppCompatActivity { private ConnectionDetector cd; Boolean isInternetPresent = false; protected SessionManager session; private AlertDialog.Builder builder; @Override protected void onCreate (Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_splash); builder = new AlertDialog.Builder(Splash.this); session = new SessionManager(getApplicationContext()); cd = new ConnectionDetector(getApplicationContext()); builder.setTitle("No Connection"); builder.setMessage("Check Your Internet Connection."); builder.setIcon(R.drawable.fail); builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { /* TODO Auto-generated method stub */ dialog.dismiss(); } }); Thread timer = new Thread(){ public void run(){ try { sleep(2000); } catch (Exception e) { e.printStackTrace(); } finally { checking(); } } }; timer.start(); } public void checking() { isInternetPresent = cd.isConnectingToInternet(); if(isInternetPresent) { session.checkLogin(); finish(); } else { builder.create().show(); finish(); } } } 
+5
source share
2 answers

Android Looper has a queue system that starts a message loop. By default, Thread does not have a Looper associated with it when it is created. Thus, when your AlertDialog tries to use the message queue, it fails.

You can create a Looper in your new thread, but you really have to use AlertDialog in the main thread. To do this, you can use the postDelayed method for Handler :

 new Handler().postDelayed( new Runnable() { @Override public void run() { checking(); } }, 2000 ); 

in your onCreate method

+5
source

As a clarification of the previous question, I will probably do the following, since network activity in the UI thread is also a bad idea:

Create a handler in onCreate ():

 mHandler = new Handler(); 

And still run validation () on your own thread, but modify it slightly:

 public void checking() { isInternetPresent = cd.isConnectingToInternet(); if(isInternetPresent) { session.checkLogin(); finish(); } else { mHandler.post(new Runnable() { @Override public void run() { builder.create().show(); finish(); }); } } 

Thus, the user interface that we made in the thread and network of the user interface runs in another thread.

0
source

All Articles