简体   繁体   English

如何使用 RunOnUIThread 方法显示 Toast

[英]How to show a Toast using a RunOnUIThread method

I have been trying to figure out why the toast is not showing.我一直在试图弄清楚为什么吐司没有出现。 from the code, the onReceive method does get executed and change the button color, but the Toast does not display.从代码中可以看出,确实执行了 onReceive 方法并更改了按钮颜色,但没有显示 Toast。 I dont know why.我不知道为什么。

 client = new Client(
                (publisher, topic, params) -> runOnUiThread(
                        () -> onReceive((Publisher) publisher, topic, params)
                ));

            client.connectToServer("192.168.3.52",5050);

  private void onReceive(Publisher publisher, String topic, Map<String, Object> params) {


        sendBtn.setBackgroundColor(getColor(R.color.white));

        Toast.makeText(MainActivity.this, "Hey", Toast.LENGTH_LONG).show();

    }

Imports进口

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

Class name Class 名称

public class MainActivity extends AppCompatActivity {

hey can you share your file name and import section........ or else try to run on clicklistener after sendbtn and try to link with your button嘿,你能分享你的文件名和导入部分........否则尝试在 sendbtn 之后在clicklistener上运行并尝试与你的按钮链接

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

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