为什么我的Toast不出现?

时间:2011-02-01 19:55:18

标签: java android toast

我的吐司直到文件下载完成后才显示(我评论了下载功能)。有什么想法吗?

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 个答案:

答案 0 :(得分:21)

我认为当您执行toast.show()时,您请求 UI线程显示Toast消息。它不一定执行immediatley。然后,您通过执行文件下载在UI线程中执行长时间运行的操作。这将阻止UI直到完成。我会将您的文件下载移动到AsyncTask,以便它不会挂起UI。