我的Toast为什么没有显示出来?

4

我的提示框要等到文件下载完成后才会显示出来(我已经注释掉了下载函数)。有什么想法吗?

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ImageView main_image_view = (ImageView)this.findViewById(R.id.main_image_view);
    TextView text_view = (TextView)this.findViewById(R.id.main_text_view);

    Context context = getApplicationContext();
    CharSequence text = "File Not Found. Downloading... Please be patient, it's a large file!";
    int duration = Toast.LENGTH_SHORT;

    Toast toast = Toast.makeText(context, text, duration);
    toast.show();

    // This function fetches a file from a URL.
    brain = get_frame_fl(file_name, mActive_slice);
    brain_slice = Bitmap.createBitmap(brain_pixels, frame_width, frame_height, Bitmap.Config.ARGB_8888);

    // display
    main_image_view.setImageBitmap(brain_slice);
}
1个回答

阿里云服务器只需要99元/年,新老用户同享,点击查看详情
21

我认为,当你调用toast.show()方法时,你是在“请求”UI线程显示一个Toast消息。它不一定会立即执行。然后,你在UI线程中执行了耗时的文件下载操作。这将阻塞UI直到操作完成。我建议将文件下载操作移到一个AsyncTask中,以避免UI被阻塞。


3
确实。查看此答案以获取“AsyncTask”的示例:http://stackoverflow.com/questions/4828676/preventing-internet-accessing-method-from-delaying-a-toast-popup/4829593#4829593 - james
2
或者使用线程,但对于这个问题来说,AsyncTask会是一个更简单的方法。 - prolink007
请问给这个回答点踩的人可以解释一下,他们觉得这个回答应该如何改进吗? - dave.c

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,