2011-04-13 14 views
0

我试图在显示启动画面时从Internet中读取17个图像。但是,使用以下代码,在处理过程中根本不会显示启动画面。它变黑,完成并转移到下一个活动。我认为在处理代码足够好之前调用setContentView,但显然不是。Android UI在处理其他数据时未显示

我错过了什么?

感谢您的帮助。

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.splash); 


    // read the bitmaps 
    // Open a new URL and get the InputStream to load data from it 

    // Start reading the XML and filling the arrays 
    for (int i=0; i<16; i++) { 
     try { 
      URL aURL = new URL (districtImage[i]); 
      URLConnection conn = aURL.openConnection(); 
      conn.connect(); 
      InputStream is = conn.getInputStream(); 
      // Buffered is always good for performance 
      BufferedInputStream bis = new BufferedInputStream(is); 
      districtBitmap[i] = BitmapFactory.decodeStream(bis); 
      bis.close(); 
      is.close(); 
     } 
     catch (IOException e){ 
      Log.e ("DEBUGTAG","Cannot load remote image", e); 
     } 
    } 
    ... some more code to transit to next activity... 
} 

回答

4

当您在onCreate中执行网络操作时,您将阻止UI线程,直到它们完成。在onCreate返回之前屏幕上不会显示任何内容。有关避免此问题的各种方法,请参见文章Painless Threading

+0

感谢您的快速回复。让我看看并回来。 – MikeC 2011-04-13 15:01:43

+0

通读AsyncTask。这是要走的路。谢谢 – MikeC 2011-04-14 02:29:20

0

您是否尝试在您的活动的onResume()中开始阅读XML并填充数组,然后转到下一个活动?在线程/异步任务中从网络读取数据。

+0

为什么在onCreate()中运行事件时会参与onResume()? – MikeC 2011-04-13 15:09:51

+0

在调用onResume()之前,用户界面不会显示给用户。因此,在onResume()被调用后,保持onCreate()'light'并做一些沉重的事情。 – Abhilash 2011-04-13 15:16:20

+0

其实,你的UI线程很重要。 – 2011-04-13 16:40:57