问题
Android 中在线程中使用Toast的方法,如何在线程中使用Toast,使用Toast报错 toast on a thread that has not called 如何解决?
解决
在线程中使用Toast主要有两种方法
方法一:强制让其在主线程中使用。(这种方法可以,但是它阻塞了主线程UI)
MainActivity.xxx.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(ctx, msg, 0).show();
}
});
方法二、直接在线程中使用
new Thread(new Runnable() {
@Override
public void run() {
Looper.prepare();
Toast.makeText(ctx, msg, 0).show();
Looper.loop();
}
}).start();