我收到以下错误:
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();
}
我该如何解决这个问题?
答案 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();
}
};