Toast not appearing on Android 4.4 KitKat

痞子三分冷 提交于 2019-12-24 01:28:15

问题


I have an application that uses Toast and I have tested it on a Samsung Galaxy II using Android 4.3 and it works fine.

I have recently gotten a Nexus 5 running Android 4.4 KitKat and when I try to load the Toast it doesn't appear.

Has Toast been removed or the syntax altered in KitKat?

This is the code I'm using to make and call the Toast:

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item)
{
    int itemId = item.getItemId();
    switch (itemId) {

    case R.id.action_settings:
        Log.e("TOAST","This was called");
        Toast.makeText(MainActivity.this, "This is my Toast message!",Toast.LENGTH_LONG).show();
        break;
    }
    return true;
}

I have Debugged the code and stepped through this part line by line so it is definitely being called, but nothing happens.


回答1:


Use this instead of getApplicationContext()




回答2:


Better use YourActivity.this instead of getApplicationContext()

EDIT :

Code seems valid.the issue is either nexus or kitkat.

  • give a try by rebooting your cell.
  • give a try by firing the toast from onResume(). just to make sure that it is working fine from UI thread.


来源:https://stackoverflow.com/questions/20373751/toast-not-appearing-on-android-4-4-kitkat

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!