简体   繁体   中英

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

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();
        }
    }
}

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

As a refinement to the earlier question, I would probably do the following since network activity on the UI thread is also a bad idea:

Create a Handler in the onCreate():

mHandler = new Handler();

And still run checking() in it's own thread, but change 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();
        });
    }
}

This way, UI us done on the UI thread and network is done in another thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM