当前位置:网站首页 > 更多 > 编程开发 > 正文

[安卓] Android在子线程显示Toast闪退,Can"t toast on a thread that has not called Loop

作者:CC下载站 日期:2020-09-24 00:00:00 浏览:57 分类:编程开发

新建一个类toastHelp

packageink.cik.logininfoapp.eneity;

importandroid.content.Context;
importandroid.os.Looper;
importandroid.widget.Toast;

publicclasstoastHelp{
staticToasttoast=null;

publicstaticvoidshow(Contextcontext,Stringtext){
try{
if(toast!=null){
toast.setText(text);
}else{
toast=Toast.makeText(context,text,Toast.LENGTH_SHORT);
}
toast.show();
}catch(Exceptione){
//解决在子线程中调用Toast的异常情况处理
Looper.prepare();
Toast.makeText(context,text,Toast.LENGTH_SHORT).show();
Looper.loop();
}
}
}

然后需要用的时候直接调用

toastHelp.show(LoginActivity.this,responseInfo.getInfo());

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯