无法在未调用Looper的线程内创建处理程序

时间:2013-08-27 14:56:37

标签: java android toast

我收到以下错误:

08-27 17:49:17.995: E/AndroidRuntime(10085): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
08-27 17:49:17.995: E/AndroidRuntime(10085):    at android.os.Handler.<init>(Handler.java:121)
08-27 17:49:17.995: E/AndroidRuntime(10085):    at android.widget.Toast$TN.<init>(Toast.java:361)
08-27 17:49:17.995: E/AndroidRuntime(10085):    at android.widget.Toast.<init>(Toast.java:97)
08-27 17:49:17.995: E/AndroidRuntime(10085):    at android.widget.Toast.makeText(Toast.java:254)

运行第一行时:

public static void alertUser(String str) {
    Toast toast = Toast.makeText(mInstance.getApplicationContext(), str,
            Toast.LENGTH_LONG);
    toast.show();

}

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

这样的事情应该有效:

public static void alertUser(String str) {

runOnUiThread(setErrorMessage); 

}


private Runnable setErrorMessage = new Runnable() {
    @Override
    public void run() {

        Toast toast = Toast.makeText(mInstance.getApplicationContext(), str,
        Toast.LENGTH_LONG);
toast.show();
    }
};