gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 14:11:51 27 4
gpt4 key购买 nike

我收到以下错误:

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

}

我该如何解决这个问题?

最佳答案

这样的事情应该有效:

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

关于java - 无法在未调用 Looper 的线程内创建处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18468942/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com