我发现很多像这样的问题,但没有一个问题,我找到了导致问题的原因,但我找不到解决办法,我们的应用程序有一个Asynctask下载列表的图像,如下所示:
private class ImageLoaderTask extends AsyncTask<HashMap<String, Object>, Void, HashMap<String, Object>>{
@Override
protected void onCancelled() {
urlConnection.disconnect();
}
@Override
protected HashMap<String, Object> doInBackground(HashMap<String, Object>... hm) {
if(!isCancelled()) {
InputStream iStream=null;
String imgUrl = (String) hm[0].get("flag_path");
int position = (Integer) hm[0].get("position");
URL url;
try {
url = new URL(imgUrl);
// Creating an http connection to communicate with url
urlConnection = (HttpURLConnection) url.openConnection();
if(!isCancelled())
{
// Connecting to url
urlConnection.connect();
//urlConnection.getOutputStream().flush();
// Reading data from url
iStream = urlConnection.getInputStream();
// Getting Caching directory
File cacheDirectory = mContext.getCacheDir();
// Temporary file to store the downloaded image
File tmpFile = new File(cacheDirectory.getPath() + "/wpta_"+position+".png");
// The FileOutputStream to the temporary file
FileOutputStream fOutStream = new FileOutputStream(tmpFile);
BufferedInputStream bis = new BufferedInputStream(iStream, 8 * 1024);
// Creating a bitmap from the downloaded inputstream
Bitmap b = BitmapFactory.decodeStream(bis);
// Writing the bitmap to the temporary file as png file
b.compress(Bitmap.CompressFormat.PNG,100, fOutStream);
// Flush the FileOutputStream
fOutStream.flush();
//Close the FileOutputStream
fOutStream.close();
// Create a hashmap object to store image path and its position in the listview
HashMap<String, Object> hmBitmap = new HashMap<String, Object>();
// Storing the path to the temporary image file
hmBitmap.put("flag",tmpFile.getPath());
// Storing the position of the image in the listview
hmBitmap.put("position",position);
// Returning the HashMap object containing the image path and position
return hmBitmap;
}
return null;
}catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
.
.
.
}
当用户根据行urlConnection.connect();
更改片段时,它会崩溃,如果我对它进行注释(以及依赖它的代码),则asynctask不会崩溃。
我尝试了很多东西来解决它,但没有什么对我有用我试图检查任务是否被取消我还使用以下代码取消片段销毁时的asyncTask:
@Override
public void onDestroy() {
urlConnection.disconnect();
if(imageLoaderTask != null && imageLoaderTask.getStatus() == Status.RUNNING) {
imageLoaderTask.cancel(true);
}
super.onDestroy();
}
这是我的LogCat:
07-23 23:05:48.930: D/AbsListView(15157): onVisibilityChanged() is called, visibility : 0
07-23 23:05:48.930: D/AbsListView(15157): unregisterIRListener() is called
07-23 23:05:50.035: D/AbsListView(15157): onVisibilityChanged() is called, visibility : 4
07-23 23:05:50.035: D/AbsListView(15157): unregisterIRListener() is called
07-23 23:05:53.150: D/AbsListView(15157): onDetachedFromWindow
07-23 23:05:53.225: D/AbsListView(15157): Get MotionRecognitionManager
07-23 23:05:53.235: D/ProgressBar(15157): setProgress = 0
07-23 23:05:53.235: D/ProgressBar(15157): setProgress = 0, fromUser = false
07-23 23:05:53.235: D/ProgressBar(15157): mProgress = 0mIndeterminate = false, mMin = 0, mMax = 100
07-23 23:05:53.255: D/ProgressBar(15157): setProgress = 0
07-23 23:05:53.255: D/ProgressBar(15157): setProgress = 0, fromUser = false
07-23 23:05:53.260: D/ProgressBar(15157): mProgress = 0mIndeterminate = false, mMin = 0, mMax = 10000
07-23 23:05:53.395: D/AbsListView(15157): unregisterIRListener() is called
07-23 23:05:53.495: D/ProgressBar(15157): updateDrawableBounds: left = 0
07-23 23:05:53.495: D/ProgressBar(15157): updateDrawableBounds: top = 0
07-23 23:05:53.495: D/ProgressBar(15157): updateDrawableBounds: right = 96
07-23 23:05:53.495: D/ProgressBar(15157): updateDrawableBounds: bottom = 96
07-23 23:05:53.590: D/AbsListView(15157): unregisterIRListener() is called
07-23 23:05:53.595: D/AbsListView(15157): unregisterIRListener() is called
07-23 23:05:53.595: D/AbsListView(15157): unregisterIRListener() is called
07-23 23:05:54.005: D/AbsListView(15157): unregisterIRListener() is called
07-23 23:05:54.020: D/AbsListView(15157): unregisterIRListener() is called
07-23 23:05:54.020: D/AbsListView(15157): unregisterIRListener() is called
07-23 23:05:54.030: E/ViewRootImpl(15157): sendUserActionEvent() mView == null
07-23 23:05:54.390: D/ProgressBar(15157): updateDrawableBounds: left = 0
07-23 23:05:54.390: D/ProgressBar(15157): updateDrawableBounds: top = 0
07-23 23:05:54.390: D/ProgressBar(15157): updateDrawableBounds: right = 96
07-23 23:05:54.390: D/ProgressBar(15157): updateDrawableBounds: bottom = 96
07-23 23:05:54.395: D/AbsListView(15157): unregisterIRListener() is called
07-23 23:05:55.020: D/AbsListView(15157): unregisterIRListener() is called
07-23 23:05:55.500: D/AbsListView(15157): onVisibilityChanged() is called, visibility : 0
07-23 23:05:55.500: D/AbsListView(15157): unregisterIRListener() is called
07-23 23:05:55.660: D/dalvikvm(15157): GC_FOR_ALLOC freed 3700K, 27% free 16424K/22420K, paused 15ms, total 16ms
07-23 23:05:55.710: D/AbsListView(15157): [unregisterDoubleTapMotionListener]
07-23 23:05:55.710: I/MotionRecognitionManager(15157): .unregisterListener : / listener count = 0->0,
07-23 23:05:55.710: D/AbsListView(15157): unregisterIRListener() is called
07-23 23:05:55.725: D/AbsListView(15157): [unregisterDoubleTapMotionListener]
07-23 23:05:55.725: I/MotionRecognitionManager(15157): .unregisterListener : / listener count = 0->0,
07-23 23:05:55.725: D/AbsListView(15157): unregisterIRListener() is called
07-23 23:05:55.905: D/AbsListView(15157): unregisterIRListener() is called
07-23 23:05:56.535: D/dalvikvm(15157): GC_FOR_ALLOC freed 2002K, 27% free 16436K/22420K, paused 15ms, total 19ms
07-23 23:05:56.570: D/AbsListView(15157): unregisterIRListener() is called
07-23 23:05:56.735: D/AbsListView(15157): unregisterIRListener() is called
07-23 23:05:56.795: D/AbsListView(15157): onDetachedFromWindow
07-23 23:05:56.840: D/AbsListView(15157): Get MotionRecognitionManager
07-23 23:05:56.925: D/AbsListView(15157): unregisterIRListener() is called
07-23 23:05:57.095: D/AbsListView(15157): onVisibilityChanged() is called, visibility : 4
07-23 23:05:57.095: D/AbsListView(15157): unregisterIRListener() is called
07-23 23:05:58.530: D/dalvikvm(15157): threadid=11: still suspended after undo (sc=1 dc=1)
07-23 23:06:03.415: W/dalvikvm(15157): threadid=11: thread exiting with uncaught exception (group=0x41930700)
07-23 23:06:03.465: E/AndroidRuntime(15157): FATAL EXCEPTION: AsyncTask #1
07-23 23:06:03.465: E/AndroidRuntime(15157): java.lang.RuntimeException: An error occured while executing doInBackground()
07-23 23:06:03.465: E/AndroidRuntime(15157): at android.os.AsyncTask$3.done(AsyncTask.java:299)
07-23 23:06:03.465: E/AndroidRuntime(15157): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
07-23 23:06:03.465: E/AndroidRuntime(15157): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
07-23 23:06:03.465: E/AndroidRuntime(15157): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
07-23 23:06:03.465: E/AndroidRuntime(15157): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
07-23 23:06:03.465: E/AndroidRuntime(15157): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
07-23 23:06:03.465: E/AndroidRuntime(15157): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
07-23 23:06:03.465: E/AndroidRuntime(15157): at java.lang.Thread.run(Thread.java:841)
07-23 23:06:03.465: E/AndroidRuntime(15157): Caused by: java.lang.NullPointerException
07-23 23:06:03.465: E/AndroidRuntime(15157): at com.Terabyte.myApp.OrganizationFragment$ListViewLoaderTask.doInBackground(OrganizationFragment.java:521)
07-23 23:06:03.465: E/AndroidRuntime(15157): at com.Terabyte.myApp.OrganizationFragment$ListViewLoaderTask.doInBackground(OrganizationFragment.java:1)
07-23 23:06:03.465: E/AndroidRuntime(15157): at android.os.AsyncTask$2.call(AsyncTask.java:287)
07-23 23:06:03.465: E/AndroidRuntime(15157): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
07-23 23:06:03.465: E/AndroidRuntime(15157): ... 4 more
07-23 23:06:04.550: D/AbsListView(15157): unregisterIRListener() is called
07-23 23:06:04.550: D/AbsListView(15157): unregisterIRListener() is called
07-23 23:06:04.555: D/AbsListView(15157): unregisterIRListener() is called
07-23 23:06:04.600: D/AbsListView(15157): onDetachedFromWindow
07-23 23:06:04.620: D/AbsListView(15157): onDetachedFromWindow
07-23 23:06:04.675: D/AbsListView(15157): onDetachedFromWindow