Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't create handler inside thread that has not called Looper.prepare() on alert dialog thread

Tags:

java

android

i got an error on my splash activity when it checked internet connection and there's no internet .. it happened on my alert dialog, perhaps.

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 have tried runOnUiThread() but it still not works .. here's my splash 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();
        }
    }
}
like image 980
Shino Rascal Avatar asked Jul 26 '16 21:07

Shino Rascal


1 Answers

In Android the Looper is a queue system that runs a message loop. By default a Thread does not have a Looper associated with it when you create it. As such when your AlertDialog tries to use the message queue it crashes.

You could create a Looper in your new thread however you should really be using an AlertDialog on the main thread. To do this you can use postDelayed method of a Handler:

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

in your onCreate method

like image 183
Jahnold Avatar answered Nov 13 '22 15:11

Jahnold