简体   繁体   English

Android Toast 消息未显示

[英]Android Toast Message is not showing

I have Button.When the user click the button, there are some condition, that condition is not satisfy then need to display Toast but not showing Toast Message...我有按钮。当用户单击按钮时,有一些条件,不满足该条件则需要显示 Toast 但不显示 Toast Message ...

Code: Edited代码:已编辑

 Button addMe = (Button)findViewById(R.id.addMe);
    addMe.setOnClickListener(new OnClickListener() {
           public void onClick(View v) {
               if(selectedReason.equals("--Select--")){
                   Log.i("TAG","-----");
                   Toast.makeText(getBaseContext(), "Reason can not be blank", Toast.LENGTH_SHORT).show();
               }else if(selectedType.equals("--Select--")){
                   Toast.makeText(getParent(), "Discount type can not be blank", Toast.LENGTH_SHORT).show();
               }else{
                   if(selectedType.equals("Value")){
                       if(spc_amount.getText().toString().equals("")){
                           Log.i("TAG","-----");
                           Toast.makeText(getBaseContext(), "Discount type can not be blank", Toast.LENGTH_SHORT).show();
                       }else{
                           if(Double.parseDouble(spc_amount.getText().toString()) > invoiceValue){
                               Toast.makeText(getBaseContext(), "Amonut can not be grater than invoice", Toast.LENGTH_SHORT).show();
                           }else{
                               Discount dis = new Discount();
                               dis.setCriteriaName(selectedReason);
                               dis.setDiscountValue(Double.parseDouble(spc_amount.getText().toString()));
                               spDisList.put(1,dis);
                               tl.removeAllViews();
                            loadTableLayout();
                           }

                       }
                   }
               }
           }
    });

I have tried context with getParent() , getApplicationContext() , SpecialDiscountActivity.this & getBaseContext() but not working....我已经尝试过使用getParent()getApplicationContext()SpecialDiscountActivity.thisgetBaseContext()上下文,但没有工作......

This Toast message coming under the Tab Activity GroupToast消息位于Tab Activity Group

Try:尝试:

Toast.makeText(getBaseContext(), "Reason can not be blank", Toast.LENGTH_SHORT).show();

It's the .show() that you've omitted everywhere that causes all your toasts to instatiate, but never execute.是 .show() 你在任何地方都省略了它导致你所有的 toasts 实例化,但永远不会执行。

Please excuse me if this isn't the solution to your problem, but I accidentally unchecked the 'Show notification' setting of the application once.如果这不是您问题的解决方案,请原谅我,但我不小心取消了应用程序的“显示通知”设置一次。 It may be an idea go into your device settings/application/manager and check this setting.进入您的设备设置/应用程序/管理器并检查此设置可能是一个想法。

I think you are missing .show();我想你错过了.show(); It should be...它应该是...

Toast.makeText(getBaseContext(), "Amount can not be grater than invoice",
                                                     Toast.LENGTH_SHORT).show();

Just restart your device!只需重新启动您的设备! It solved my problem.它解决了我的问题。

Just bumped across this issue and was wondering how a simple Toast is not appearing.刚碰到这个问题,想知道一个简单的 Toast 怎么没有出现。 After few trials it struck me to check the notification setting of the app and Voila.经过几次试验后,我突然想到检查应用程序和 Voila 的通知设置。

I switched the notification setting ON and it started showing up.我打开了通知设置,它开始出现。 I searched and came across the below link, which talks about the same:我搜索并发现了下面的链接,它谈到了相同的内容:

https://code.google.com/p/android/issues/detail?id=35013 https://code.google.com/p/android/issues/detail?id=35013

I did like this我喜欢这个

Toast.makeText(SalesActivityGroup.group.getParent(), "Amount can not be 
                                grater than invoice", Toast.LENGTH_SHORT).show();

There could be two possibilities:可能有两种可能:

1 - You have not append .show(); 1 - 你没有附加.show(); at the very end of Toast.makeText(getActivity(), "Hi", Toast.LENGTH_SHORT) .Toast.makeText(getActivity(), "Hi", Toast.LENGTH_SHORT)

2 - There could be situation you are not passing right context 2 - 可能存在您没有传递正确上下文的情况

For that try passing getActivity().getApplicationContext();为此尝试传递getActivity().getApplicationContext();

which in my case resolved the issue.在我的情况下解决了这个问题。

Good luck :)祝你好运 :)

If Toast is not showing that means either you have not called show() method or you are not on UI thread如果 Toast 没有显示,则意味着您没有调用show()方法或您不在UI 线程上

Ideally you should create a helper method to show Toast like this on UI thread理想情况下,您应该创建一个辅助方法来在 UI 线程上像这样显示 Toast

 /** Execute Toast on UI thread **/
 private fun showToast(message: String) {

        Handler().post {
            // Code here will run in UI thread
            Toast.makeText(
                this, 
                message,
                Toast.LENGTH_LONG
            ).show()
        }
    }

有时模拟器会挂起,因此重新启动模拟器可以解决问题。

You can use the context of the Activity您可以使用 Activity 的上下文

like,像,

Toast.makeText(ActivityName.this,"Reason can not be blank", Toast.LENGTH_SHORT).show()

if this is not working, please put a log.i();如果这不起作用,请输入log.i(); in your each condition may be its going to the last else and you are not getting the Toast.在您的每种情况下,可能都是最后一次,而您没有得到 Toast。

Sometimes there may be an error or nullPointerException in the message that we want to print with Toast message.有时我们想用 Toast 消息打印的消息中可能会出现错误或 nullPointerException。 If this error occured then app will simply ignore the Toast message.如果发生此错误,则应用程序将简单地忽略 Toast 消息。 I had the same issue.我遇到过同样的问题。 I printed the message in Log and found the error, after that I changed the message in Toast.我在日志中打印了消息并发现了错误,之后我在 Toast 中更改了消息。 And it worked as I wanted.它按我的意愿工作。 Try it!!尝试一下!!

Simply restart your emulator not fixed my problem.只需重新启动模拟器并不能解决我的问题。

  1. Close emulator关闭模拟器
  2. Tools -> Avd Manager工具 -> Avd 管理器
  3. In the device list click on "drop-down icon" under "Action" column.在设备列表中单击“操作”列下的“下拉图标”。
  4. Cold boot now现在冷启动

Now reinstalling the app it will work.现在重新安装应用程序它将起作用。

尝试:

Toast.makeText(v.getContext(), "Reason can not be blank", Toast.LENGTH_SHORT).show();

Complimer checks all the code and if there is a critical error, it ignores even the lines before the error section and therfore you will not see the "Toast". Complimer 检查所有代码,如果存在严重错误,它甚至会忽略错误部分之前的行,因此您将看不到“Toast”。 Simply, comment the lines which error happens in them (you can find the error lines in Logcat) Now you can see the "Toast" and can analyze your problem.简单地,注释其中发生错误的行(您可以在 Logcat 中找到错误行)现在您可以看到“Toast”并可以分析您的问题。

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

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