2011-02-01 92 views
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); 
} 

回答

21

我覺得當你做toast.show()要求的UI線程顯示敬酒消息。它不一定會立即執行。然後通過執行文件下載,在UI線程中執行長時間運行的操作。這將阻止用戶界面,直到它完成。我會將您的文件下載文件移動到AsyncTask,以便它不會掛起用戶界面。

+3

的確如此。看到這個答案爲`AsyncTask`的例子:http://stackoverflow.com/questions/4828676/preventing-internet-accessing-method-from-delaying-a-toast-popup/4829593#4829593 – binnyb 2011-02-01 20:05:37