当调用片段暂停/删除时,异步任务会发生什么?我假设任务完成但是有人可以确认这一点很棒.我试图找到一个修复我的崩溃.
我有一个异步任务,是一个递归图像加载器.图像加载器是它为其加载图像的ListFragment的内部类.除非在完成加载所有图像之前按下后退按钮,否则图像加载器工作正常.当我在所有图像完成之前按下后退按钮加载应用程序崩溃.我觉得图像加载器失去了对适配器的访问权限,但我不确定我是否正确地攻击了这个问题.我不确定如何在异步任务中修复此问题.如果有人可以帮助我.甚至反省了一个想法让我思考的不同.
任何帮助都是极好的.谢谢你的时间,甚至看.
ImageLoader的
private class ImageLoaderTask extends AsyncTask<HashMap<String, Object>, VoID, HashMap<String, Object>> { @OverrIDe protected HashMap<String, Object> doInBackground( HashMap<String, Object>... hm) { inputStream in = null; String url = (String) hm[0].get("image"); int pos = (Integer) hm[0].get("pos"); url = "http://kzfr.org/u/img/small/" + url; URL mUrl; try { mUrl = new URL(url); URLConnection urlConnect = (URLConnection) mUrl .openConnection(); in = urlConnect.getinputStream(); file cacheDirectory = getSherlockActivity().getBaseContext() .getCacheDir(); file tmp = new file(cacheDirectory.getPath() + "/kzfr_small" + pos + ".png"); fileOutputStream fOut = new fileOutputStream(tmp); Bitmap b = BitmapFactory.decodeStream(in); b.compress(Bitmap.CompressFormat.PNG, 100, fOut); fOut.flush(); fOut.close(); HashMap<String, Object> hmBmp = new HashMap<String, Object>(); hmBmp.put("blank_start", tmp.getPath()); hmBmp.put("pos", pos); return hmBmp; } catch (Exception e) { e.printstacktrace(); } return null; } @OverrIDe protected voID onPostExecute(HashMap<String, Object> result) { // this is the img path that leads to a local image that was just // saved on the phone. String imgPath = (String) result.get("blank_start"); // the position of the image in the ListvIEw int position = (Integer) result.get("pos"); // local adapter SimpleAdapter adapter = (SimpleAdapter) getlistadapter(); // hashmap entry from the local adapter at the position of the image // that we just loaded. HashMap<String, Object> hm = (HashMap<String, Object>) adapter .getItem(position); // replace the actual blank image with the image path that we just // loaded hm.put("blank_start", imgPath); // update ListvIEw adapter.notifyDataSetChanged(); // we are done with the current position increment position and move // on. position++; // this while loop finds the next position in the adapter that does // not have false for an image path. False indicates that there is // no image and i should load a default image. while (position < adapter.getCount()) { hm = (HashMap<String, Object>) adapter.getItem(position); imgPath = (String) hm.get("image"); if (!imgPath.equalsIgnoreCase("false")) { break; } position++; } // checks to make sure that the position is not out of bounds on the // adapter. If in bounds then there might be more images to load. // Start a new ImageLoader. This is a recursive Class. if (position < adapter.getCount()) { ImageLoaderTask imageLoader = new ImageLoaderTask(); hm.put("pos", (position)); imageLoader.execute(hm); } }
堆栈跟踪
01-14 20:09:28.752: E/AndroIDRuntime(6069): FATAL EXCEPTION: main01-14 20:09:28.752: E/AndroIDRuntime(6069): java.lang.NullPointerException01-14 20:09:28.752: E/AndroIDRuntime(6069): at com.appdomum.doublea.ListFrag$ImageLoaderTask.onPostExecute(ListFrag.java:272)01-14 20:09:28.752: E/AndroIDRuntime(6069): at com.appdomum.doublea.ListFrag$ImageLoaderTask.onPostExecute(ListFrag.java:1)01-14 20:09:28.752: E/AndroIDRuntime(6069): at androID.os.AsyncTask.finish(AsyncTask.java:417)01-14 20:09:28.752: E/AndroIDRuntime(6069): at androID.os.AsyncTask.access0(AsyncTask.java:127)01-14 20:09:28.752: E/AndroIDRuntime(6069): at androID.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)01-14 20:09:28.752: E/AndroIDRuntime(6069): at androID.os.Handler.dispatchMessage(Handler.java:99)01-14 20:09:28.752: E/AndroIDRuntime(6069): at androID.os.Looper.loop(Looper.java:130)01-14 20:09:28.752: E/AndroIDRuntime(6069): at androID.app.ActivityThread.main(ActivityThread.java:3806)01-14 20:09:28.752: E/AndroIDRuntime(6069): at java.lang.reflect.Method.invokeNative(Native Method)01-14 20:09:28.752: E/AndroIDRuntime(6069): at java.lang.reflect.Method.invoke(Method.java:507)01-14 20:09:28.752: E/AndroIDRuntime(6069): at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)01-14 20:09:28.752: E/AndroIDRuntime(6069): at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:597)01-14 20:09:28.752: E/AndroIDRuntime(6069): at dalvik.system.NativeStart.main(Native Method)
解决方法:
我想你在这里走在正确的轨道上.
在编写自定义AsyncTasks时,许多开发人员忘记了,有必要处理任务取消.简单地取消任务并不意味着它的代码不会在(例如)可能在当时释放的Activity / Fragment资源上执行.这通常在以下步骤中完成:
>创建任务后,保存对它的引用.
>在活动/片段完成后取消所有正在运行的任务.这通常在Activity.onDestroy()/ Fragment.onDestroy()中完成.删除对已取消任务的所有引用.
>在任务代码中:
>在doInBackground()中定期检查它是否为isCancelled().如果是这样,整理并中止进一步执行.
>最终在onCancelled()中处理取消(在UI线程上运行).
>在onPostExecute()中执行取消任务的任何处理(如果任务被取消,则使用null结果执行).
以上是内存溢出为你收集整理的android – 按下后退按钮时异步任务崩溃.全部内容,希望文章能够帮你解决android – 按下后退按钮时异步任务崩溃.所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)