【问题标题】:Toast message not showing in main Looper?Toast 消息未显示在主 Looper 中?
【发布时间】:2014-09-21 23:00:45
【问题描述】:

我知道这是一个有很多答案的老问题,就像我发现的 here 一样。但我仍然看不到我的 toast 消息。

我尝试检查当前线程,例如:

Log.d("DEBUG", "Main Looper: " + Looper.getMainLooper().toString() + ", Current Looper: " + Looper.myLooper().toString());
Log.d("DEBUG", "Main Thread: " + Looper.getMainLooper().getThread().toString() + ", Current Thread: " + Thread.currentThread().toString());

//and the log is like:
D/DEBUG﹕ Main Looper: Looper{420a9e68}, Current Looper: Looper{420a9e68}
D/DEBUG﹕ Main Thread: Thread[main,5,main], Current Thread: Thread[main,5,main]

所以我认为我在主线程(ui线程)中,我还专门尝试在ui线程中吐司:

runOnUiThread(new Runnable() {
    @Override
    public void run() {
        Toast.makeText(getBaseContext(), "abc", Toast.LENGTH_LONG).show();
        Log.d("DEBUG", "ui thread abc");
    }
});

我可以看到日志“ui thread abc”,但 toast 仍然没有出现。

然后我想也许 toast 与其他视图重叠,所以我尝试指定 toast 的位置,例如: 吐司吐司 Toast.makeText(getBaseContext(), "abc", Toast.LENGTH_LONG);

Toast toast = Toast.makeText(this, "abc", Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP|Gravity.CENTER, 0, 0);
toast.show();

仍然一无所获。那么你认为我的吐司发生了什么?

-------更新

我在一个带有垂直线性布局和几个嵌入式水平线性布局的活动中。 我现在只有两个活动,我正在为主要活动干杯。

我在 onclick 事件中使用自定义图像滑块和 toasting,onClickMethod 如下:

public void onSliderClick(BaseSliderView slider) {
//        Log.d("DEBUG", "Main Looper: " + Looper.getMainLooper().toString() + ", Current Looper: " + Looper.myLooper().toString());
//        Log.d("DEBUG", "Main Thread: " + Looper.getMainLooper().getThread().toString() + ", Current Thread: " + Thread.currentThread().toString());
//        Toast.makeText(getBaseContext(), slider.getBundle().get("testMsg").toString(), Toast.LENGTH_LONG).show();

//        Toast.makeText(getApplicationContext(), slider.getBundle().get("testMsg").toString(), Toast.LENGTH_LONG).show();
//        Log.d("DEBUG", "Slider - " + slider.getBundle().get("testMsg").toString());

    Toast.makeText(getApplicationContext(), "abc", Toast.LENGTH_LONG).show();
    Log.d("DEBUG", "msg abc 123");


//        Toast toast = Toast.makeText(this, "abc", Toast.LENGTH_SHORT);
//        toast.setGravity(Gravity.TOP|Gravity.CENTER, 0, 0);
//        toast.show();
}

-------更新2

我刚刚发现操作栏 toast 也没有显示,我确实设置了标题元素,我使用的是位于屏幕底部的拆分操作栏。

其实我之前可以看到所有的 toast 消息,然后我对 UI 或其他一些东西进行了一些更改,然后所有的 toast 都消失了。问题是我无法回滚到 toast 运行良好的旧版本,我找不到它们现在消失的原因。

顺便说一下,在我添加了自定义图像滑块之后,吐司仍然很好,除了自动切换图像滑块的后台线程之外没有任何其他线程。

【问题讨论】:

  • 使用活动上下文?

标签: android toast


【解决方案1】:

使用 getApplicationContext() 或 Activity 上下文就可以了

 Toast.makeText(getApplicationContext(), "abc", Toast.LENGTH_LONG).show();

【讨论】:

  • 谢谢,但 getApplicationContext() 仍然无法正常工作,我之前尝试使用 getBaseContext() 并再次检查了它。
  • getApplicationContext() 会起作用,请检查.. 我已经测试过它对我有用
  • 是的,我正在真实设备中调试,我尝试使用 Toast 和 Log,为了以防万一,我更改了 log msg,我得到了新日志,但没有看到 toast。 ..
  • 你在哪里,我的意思是这段代码是在活动中还是在片段中?您能否发布完整代码以便更好地理解
【解决方案2】:

一个月前,当我尝试使用异步(非 UI 线程)的 doInBackground(String... params) 方法制作吐司时,我遇到了同样的问题,我使用了这个及其工作

MainActivity.this.runOnUiThread(new Runnable() {
                  public void run() {
                      Toast.makeText(getApplicationContext(), "whatever message ",
                           Toast.LENGTH_LONG).show();

希望对你有帮助

【讨论】:

    【解决方案3】:

    我今天在开发其他应用程序时再次遇到问题,并再次进行了最后一次搜索。最后发现这似乎是一个Android bug如果你禁用了应用程序的通知,toast 消息也会被禁用

    我所要做的就是去设置 -> 应用程序 -> 我的应用程序检查“显示通知”,这是我自己偶尔未选中的。

    【讨论】:

      猜你喜欢
      • 2015-02-12
      • 2011-12-09
      • 1970-01-01
      • 1970-01-01
      • 2020-07-30
      • 1970-01-01
      • 1970-01-01
      • 2014-07-04
      相关资源
      最近更新 更多