0

我收到以下错误:

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

}

我怎样才能解决这个问题?

4

1 回答 1

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();
    }
};
于 2013-08-27T15:01:27.307 回答