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

Lan
Lan
2020-09-24 / 0 评论 / 830 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2020年09月24日,已超过1271天没有更新,若内容或图片失效,请留言反馈。

新建一个类toastHelp

package ink.cik.logininfoapp.eneity;

import android.content.Context;
import android.os.Looper;
import android.widget.Toast;

public class toastHelp {
    static Toast toast = null;

    public static void show(Context context, String text) {
        try {
            if (toast != null) {
                toast.setText(text);
            } else {
                toast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
            }
            toast.show();
        } catch (Exception e) {
            //解决在子线程中调用Toast的异常情况处理
            Looper.prepare();
            Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
            Looper.loop();
        }
    }
}

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

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

image.png

0

评论 (0)

取消