简体   繁体   English

吐司不起作用(Android)

[英]Toast not working (Android)

When I run this method Toast.makeText(MainActivity.this, txt, Toast.LENGTH_LONG).show(); 当我运行此方法时Toast.makeText(MainActivity.this, txt, Toast.LENGTH_LONG).show(); from another thread, the application falls to be working only from the onCreate, but DebugText.setText(txt); 从另一个线程来看,该应用程序只能从onCreate运行,而DebugText.setText(txt);

works fine everywhere ... who else can help? 在任何地方都可以正常工作...还有谁可以帮助您?

public void screenMessage(final String txt) {

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
               //DebugText.setText(txt);
                Toast.makeText(MainActivity.this, txt, Toast.LENGTH_LONG).show();
            }
        });
    }

my solution as below(it works everywhere for me): 我的解决方案如下(它对我无处不在):

public static void showToast(final Context ctx, final String msg, int type) {
    if (ctx == null || TextUtils.isEmpty(msg))
        return;
    final int toastType = type == Toast.LENGTH_LONG ? Toast.LENGTH_LONG
            : Toast.LENGTH_SHORT;
    if (Looper.myLooper() == Looper.getMainLooper()) {
        Toast.makeText(ctx, msg, toastType).show();
    } else {
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(ctx, msg, toastType).show();
            }
        });
    }
}

I think you missed calling start try this 我认为您错过了致电开始的机会

public void screenMessage(final String txt) {

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
               //DebugText.setText(txt);
                Toast.makeText(MainActivity.this, txt, Toast.LENGTH_LONG).show();
            }
        }).start();    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM