【问题标题】:Toast not appearing on Android 4.4 KitKatToast 没有出现在 Android 4.4 KitKat 上
【发布时间】:2013-12-04 11:10:41
【问题描述】:

我有一个使用 Toast 的应用程序,我已经在使用 Android 4.3 的三星 Galaxy II 上对其进行了测试,它运行良好。

我最近得到了一个运行 Android 4.4 KitKat 的 Nexus 5,当我尝试加载 Toast 时它没有出现。

Toast 是否已被移除或 KitKat 中的语法已更改?

这是我用来制作和调用 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;
}

我已经调试了代码并逐行遍历了这部分,所以它肯定被调用了,但是没有任何反应。

【问题讨论】:

标签: android toast


【解决方案1】:

使用this 而不是getApplicationContext()

【讨论】:

  • 他应该使用 Activity.this 而不是 getApplicationContext() 的任何具体原因?
  • @PareshMayani:这里只需要current activitycontext(参考)而不是application 的上下文
  • @MehulJoisar ApplicationContext 和 ActivityContext 都可以与 Toast 一起使用,因为 Toast 不绑定到窗口,并且可以从应用程序的任何位置显示。见Toast.makeText
  • 使用 this 或 YourActivity.this 没有区别。它仍然没有出现,但在运行 Android 4.3 的设备上运行良好
  • @PriestVallon:这很奇怪!你能把你发射toast的代码贴出来吗?
【解决方案2】:

最好使用YourActivity.this 而不是getApplicationContext()

编辑:

代码似乎有效。问题是nexuskitkat

  • 重启你的手机试试看。
  • 尝试从onResume() 发射吐司。只是为了确保它在 UI thread 上运行良好。

【讨论】:

  • 他应该使用 Activity.this 而不是 getApplicationContext() 的任何具体原因?
  • @PareshMayani 检查此链接:stackoverflow.com/questions/1026973/…
  • 使用 this 或 YourActivity.this 没有区别。它仍然没有出现,但在运行 Android 4.3 的设备上运行良好
  • @MehulJoisar 都试过了,都没有解决。 MainActivity 中显示的片段是否重要?
  • @PriestVallon:不,没关系。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
  • 1970-01-01
  • 2016-11-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多