호출하는 프래그먼트가 일시 중지/제거 될 때 비동기 작업은 어떻게됩니까? 나는 그 일이 끝났다고 가정하고 있지만 그것은 누군가를 잘 확인할 수있을 것이다. 내 충돌에 대한 해결책을 찾고 있습니다.비동기 Backbutton에서 충돌이 발생했습니다.
재귀 이미지 로더 인 비동기 작업이 있습니다. 이미지 로더는 이미지를로드하는 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: main
01-14 20:09:28.752: E/AndroidRuntime(6069): java.lang.NullPointerException
01-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.access$300(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)
어떤 줄이 272입니까? 활동이 끝났기 때문에 더 이상 존재하지 않는 것을 참조하고 싶다. –
라인 272는'string imgPath = (String) result.get ("blank_start");'out of onPostExecute이다. 그것은 DoIn 백그라운드에서 전달되기 때문에 Image Loader 클래스 내부에 있어야하는 것을 참조합니다. – doubleA
먼저 result.hasKey ("blank_start")를 확인해야합니다. 해당 키에 대한 항목이 없어서 null이 반환되는 것처럼 보입니다. –