如何解决处理其他数据时未显示android UI
我正在尝试在显示初始屏幕的同时从Internet读取17张图像。但是,使用以下代码,启动屏幕在处理期间根本不会显示。它变黑,结束并过渡到下一个活动。我以为在处理代码之前调用setContentView足够好,但显然不行。 我想念什么? 谢谢你的帮助。@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...
}
解决方法
在ѭ1中进行联网操作时,您将阻塞UI线程直到完成。 ѭ1返回之前,屏幕上将不会显示任何内容。有关避免此问题的各种方法,请参见文章无痛线程。
,您是否尝试过在活动的onResume()中执行“开始读取XML并填充数组”,然后过渡到下一个活动?并在线程/异步任务中进行网络读取。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。