简体   繁体   English

Toast消息不会显示

[英]Toast Messages do not show up

Hey I have a problem with toast messages which does not show up. 嘿我有一个没有出现的吐司消息的问题。 I'm creating an app which displays user's message into Morse code and I want toast message show up to inform which character is diplayed now. 我正在创建一个将用户的消息显示为摩尔斯电码的应用程序,我想要显示的消息显示,以告知现在哪个字符被显示。 But when I put it like below, toast messages do not show up. 但是,当我把它放在下面时,吐司消息不会显示出来。 It's probably because next function which is called are somehow killing the previous one, cause when I removed other commands and left only showStatus() the messages appeared. 这可能是因为被调用的下一个函数以某种方式杀死了前一个函数,因为当我删除其他命令并且只留下showStatus()消息出现时。 How can I deal with this situation? 我该如何处理这种情况?

public void dispDot()
    {
        final Parameters pM = cameraMorse.getParameters();
        pM.setFlashMode(Parameters.FLASH_MODE_TORCH);
        cameraMorse.setParameters(pM);
        MorseActivity.backgroundMorse.setBackgroundResource(R.drawable.background1on);
        waitFor(1);
        pM.setFlashMode(Parameters.FLASH_MODE_OFF);
        cameraMorse.setParameters(pM);
        MorseActivity.backgroundMorse.setBackgroundResource(R.drawable.background1a);
    }
    //function that displays the line in Morse code
    public void dispLine()
    {
        final Parameters pM = cameraMorse.getParameters();
        pM.setFlashMode(Parameters.FLASH_MODE_TORCH);
        cameraMorse.setParameters(pM);
        MorseActivity.backgroundMorse.setBackgroundResource(R.drawable.background1on);
        waitFor(3);
        pM.setFlashMode(Parameters.FLASH_MODE_OFF);
        cameraMorse.setParameters(pM);
        MorseActivity.backgroundMorse.setBackgroundResource(R.drawable.background1a);
    }

    public void showStatus(char character)
    {   
        //status1.setTextColor(Color.WHITE);
        //status1.setText("Status: Now displaying "+character);
        toast = Toast.makeText(MorseActivity.this, "Displaying "+character,Toast.LENGTH_LONG);
        toast.setGravity(Gravity.TOP, 0, 30);
        toast.show();
    }

    public void morseMessageTranslator()
    {
        morseMessage = textBox.getText().toString();
        morseMessage = morseMessage.toUpperCase();
        if(morseMessage.length()>0)
        {
            char character;
            for(int a=0;a<morseMessage.length();a++)
            {
                character=morseMessage.charAt(a);
                switch (character)
                {
                case 'A': //.-
                    showStatus('A');
                    dispDot();waitFor(1);dispLine();waitFor(1);
                    break;
                case 'B': //-...
                    showStatus('B');
                    dispLine();waitFor(1);dispDot();waitFor(1);dispDot();waitFor(1);dispDot();waitFor(1);
                    break;

UPDATE: Ok it turns out that waitFor() function is the cause. 更新:好的事实证明waitFor()函数是原因。

public void waitFor (final int time)
    {
        Thread waitthread = new Thread(new Runnable() { 
            // After call for background.start this run method call
            public void run() {


                    try {
                        Thread.sleep(time*500);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            });
    waitthread.start();
    try {
        waitthread.join();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

But still don't know how to show toast before the wait is launched. 但是在等待发布之前仍然不知道如何展示吐司。

As your comments on this question show, the reason your Toasts aren't showing is because of your waitFor() method. 正如您对此问题的评论所示,您的waitFor()未显示的原因是您的waitFor()方法。 As described, this performs a pointless calculation, which a) wastes precious CPU time (and therefore battery) and b) performs this on the UI thread. 如上所述,这执行无意义的计算,a)浪费宝贵的CPU时间(因此浪费电池),b)在UI线程上执行此操作。 This means that anything that should happen on the UI thread won't happen all the time waitFor() is running, including Toasts. 这意味着在UI线程上发生的任何事情都不会在waitFor()运行时发生,包括Toasts。

You'll have to include some sort of threading here to get over this issue (the Android Developers website has a good tutorial ). 你必须在这里包含某种线程来克服这个问题(Android开发者网站有一个很好的教程 )。 You'll want the dispDot, dispLine and waitFor calls to happen on a background thread. 您希望dispDot,dispLine和waitFor调用在后台线程上发生。 Bear in mind that if any of these three methods interact with your UI, they must do that back on the UI thread (see Communicating with the UI thread in the linked tutorial). 请记住,如果这三种方法中的任何一种与UI交互,则必须在UI线程上执行此操作(请参阅链接教程中的UI线程通信)。

Previous (wrong) answer 以前(错误的)答案

You're creating your Toast s, but not calling show() on them! 你正在创建你的Toast ,但不会在它们上面调用show() It's a very easy mistake to make. 这是一个非常容易犯的错误。 Just change the line: 只需更改行:

toast = Toast.makeText(MorseActivity.this, "Displaying "+character,Toast.LENGTH_LONG);

to

toast = Toast.makeText(MorseActivity.this, "Displaying "+character,Toast.LENGTH_LONG).show();

将.show()添加到吐司代码的末尾

toast = Toast.makeText(MorseActivity.this, "Displaying "+character,Toast.LENGTH_LONG).show();

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

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