当前位置: 首页> 房产> 市场 > 重庆网站优化公司_中国住建网的官网_百度一下网址是多少_网站快速优化排名方法

重庆网站优化公司_中国住建网的官网_百度一下网址是多少_网站快速优化排名方法

时间:2025/7/16 8:19:52来源:https://blog.csdn.net/weixin_41119184/article/details/142179103 浏览次数:0次
重庆网站优化公司_中国住建网的官网_百度一下网址是多少_网站快速优化排名方法

问题描述:
  在安卓中,ui更新必须在主线执行,Toast是一种用于在屏幕上短暂显示消息的机制,它依赖主线程来进行创建和显示消息。当我们在子线程中直接调用Toast时,就会触发Can‘t create handler inside thread that has not called Looper报错。这个错误时由于Toast内部使用了Handler来处理消息队列,并在主线程中显示Toast,而在子线程中没有默认的Looper对象可供Handler使用。
  当我们调用Toast的show方法时,会通过enqueueToast方法将toast插入消息队列中。

public void show() {if (Compatibility.isChangeEnabled(CHANGE_TEXT_TOASTS_IN_THE_SYSTEM)) {checkState(mNextView != null || mText != null, "You must either set a text or a view");} else {if (mNextView == null) {throw new RuntimeException("setView must have been called");}}INotificationManager service = getService();String pkg = mContext.getOpPackageName();TN tn = mTN;tn.mNextView = mNextView;final int displayId = mContext.getDisplayId();try {if (Compatibility.isChangeEnabled(CHANGE_TEXT_TOASTS_IN_THE_SYSTEM)) {if (mNextView != null) {// It's a custom toastservice.enqueueToast(pkg, mToken, tn, mDuration, displayId);} else {// It's a text toastITransientNotificationCallback callback =new CallbackBinder(mCallbacks, mHandler);service.enqueueTextToast(pkg, mToken, mText, mDuration, displayId, callback);}} else {service.enqueueToast(pkg, mToken, tn, mDuration, displayId);}} catch (RemoteException e) {// Empty}}

解决方案
新建一个Handler,用于子线程中使用Toast。

public class HandlerUtil {private static Handler mHandler;public static void init() {mHandler = new Handler();}public static void post(MyFunction func) {if (mHandler != null) {mHandler.removeCallbacksAndMessages(null);mHandler.post(new Runnable() {@Overridepublic void run() {func.apply();}});}}@FunctionalInterfacepublic interface MyFunction {void apply();}
}

在application中调用初始化

	HandlerUtil.init();

在子线程中实现Toast。

HandlerUtil.post(()-> ToastUtil.showToast("test", 2000));

ToastUtil类可参考:安卓Toast避免重复显示

关键字:重庆网站优化公司_中国住建网的官网_百度一下网址是多少_网站快速优化排名方法

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: