Posting Toast message from a Thread

前端 未结 4 2142
温柔的废话
温柔的废话 2020-12-03 22:09

My application launches a thread to query the web for some data. I want to display a Toast message when nothing is found, but my application always crashes.

I\'ve t

4条回答
  •  囚心锁ツ
    2020-12-03 22:11

    If you're running the Handler from your Activity class, you can set the context by just referencing the .this of your Activity like so:

    final Runnable showToastMessage = new Runnable() {
        public void run() {
            Toast.makeText(YourActivity.this, "Message", Toast.LENGTH_SHORT).show();
        }
    };
    

提交回复
热议问题