我知道第一个你要这就是……为什么你会在世界上使用AsyncTask.
所以这是我的问题我正在研究一些Android应用程序(API为AndroID 2.1或更高版本),我正在测试模拟器,一切都很好,所以然后我测试HTC Sensation,它说networkonmainthreadExeption!
我正在下载一些图片,然后在地图上绘制.
所以要解决这个问题,每次(互联网连接)在这种情况下下载图片我必须把AsyncTask放到工作.
所以我需要一种方法如何知道所有图片何时完成所以我可以开始绘图..
我试了这么多,没有结果我不知道.我有一个处理程序的解决方案,但如果在较慢的网络上运行我得到nullpointer(因为图片没有下载).
所以请帮助我.
编辑:
这是一个想法:
Bitmap bubbleIcon ; onCreate(){ ...// i am making call for Asyncnew imgDown().execute(url);//and then i calling functions and classes to draw with that picture bubbleIcon !DrawOnMap(bubbleIcon);}//THIS IS ASYNC AND FOR EX. SUPPOSE I NEED TO DOWNLOAD THE PIC FirsT class imgDown extends AsyncTask<String, VoID, Bitmap> { private String url; public imgDown() { } @OverrIDe protected Bitmap doInBackground(String... params) { url = params[0]; try { return getBitmapFromURL(url); } catch (Exception err) { } return null; } @OverrIDe protected voID onPostExecute(Bitmap result) { bubbleIcon = result; bubbleIcon = Bitmap .createScaledBitmap(bubbleIcon, 70, 70, true); } public Bitmap getBitmapFromURL(String src) { try { Log.e("src", src); URL url = new URL(src); httpURLConnection connection = (httpURLConnection) url .openConnection(); connection.setDoinput(true); connection.connect(); inputStream input = connection.getinputStream(); // /tuka decode na slika vo pomalecuk kvalitet! BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 3; Bitmap myBitmap = BitmapFactory .decodeStream(new FlushedinputStream(input)); Log.e("Bitmap", "returned"); return myBitmap; } catch (IOException e) { e.printstacktrace(); Log.e("getBitmapFromURL", e.getMessage()); return null; } } class FlushedinputStream extends FilterinputStream { public FlushedinputStream(inputStream inputStream) { super(inputStream); } public long skip(long n) throws IOException { long totalBytesSkipped = 0L; while (totalBytesSkipped < n) { long bytesSkipped = in.skip(n - totalBytesSkipped); if (bytesSkipped == 0L) { int byteValue = read(); if (byteValue < 0) { break; // we reached EOF } else { bytesSkipped = 1; // we read one byte } } totalBytesSkipped += bytesSkipped; } return totalBytesSkipped; } } }
我希望现在更清楚了.
解决方法:
class OpenWorkTask extends AsyncTask { @OverrIDe protected Boolean doInBackground(String... params) { // do something return true; } @OverrIDe protected voID onPostExecute(Boolean result) { // The results of the above method // Processing the results here myHandler.sendEmptyMessage(0); }}Handler myHandler = new Handler() { @OverrIDe public voID handleMessage(Message msg) { switch (msg.what) { case 0: // calling to this function from other pleaces // The notice call method of doing things break; default: break; } }};
总结 以上是内存溢出为你收集整理的Android:如何在MainThread中等待AsyncTask完成?全部内容,希望文章能够帮你解决Android:如何在MainThread中等待AsyncTask完成?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)