我是 android 新手,在将图像加载到 listview 时遇到问题。我使用延迟加载进行图像加载,问题是当我们加载超过 40 个图像时,会有一个org.eclipse.jdi:timeout exception
. 我们该如何解决这个问题。
这是我的代码
private class ListViewLoaderTask extends AsyncTask<String, Void, SimpleAdapter>{
JSONObject jObject;
@Override
protected SimpleAdapter doInBackground(String... strJson) {
try{
jObject = new JSONObject(strJson[0]);
CountryJSONParser countryJsonParser = new CountryJSONParser();
countryJsonParser.parse(jObject);
}catch(Exception e){
Log.d("JSON Exception1",e.toString());
}
// Instantiating json parser class
CountryJSONParser countryJsonParser = new CountryJSONParser();
// A list object to store the parsed countries list
List<HashMap<String, Object>> countries = null;
try{
// Getting the parsed data as a List construct
countries = countryJsonParser.parse(jObject);
}catch(Exception e){
Log.d("Exception",e.toString());
}
// Keys used in Hashmap
String[] from = { "username","flag","details"};
// Ids of views in listview_layout
int[] to = { R.id.tv_country,R.id.iv_flag,R.id.tv_user};
// Instantiating an adapter to store each items
// R.layout.listview_layout defines the layout of each item
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), countries, R.layout.lv_layout, from, to);
return adapter;
}
@Override
protected void onPostExecute(SimpleAdapter adapter) {
mListView.setAdapter(adapter);
for(int i=0;i<adapter.getCount();i++){
HashMap<String, Object> hm = (HashMap<String, Object>) adapter.getItem(i);
String imgUrl = (String) hm.get("flag_path");
ImageLoaderTask imageLoaderTask = new ImageLoaderTask();
HashMap<String, Object> hmDownload = new HashMap<String, Object>();
hm.put("flag_path",imgUrl);
hm.put("position", i);
imageLoaderTask.execute(hm);
}
}
}
/** AsyncTask to download and load an image in ListView */
private class ImageLoaderTask extends AsyncTask<HashMap<String, Object>, Void, HashMap<String, Object>>{
@Override
protected HashMap<String, Object> doInBackground(HashMap<String, Object>... hm) {
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);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
iStream = urlConnection.getInputStream();
File cacheDirectory = getBaseContext().getCacheDir();
File tmpFile = new File(cacheDirectory.getPath() + "/wpta_"+position+".png");
FileOutputStream fOutStream = new FileOutputStream(tmpFile);
Bitmap b = BitmapFactory.decodeStream(iStream);
if(b!=null)
{
b.compress(Bitmap.CompressFormat.PNG,100, fOutStream);
fOutStream.flush();
fOutStream.close();
HashMap<String, Object> hmBitmap = new HashMap<String, Object>();
// Storing the path to the temporary image file
hmBitmap.put("flag",tmpFile.getPath());
hmBitmap.put("position",position);
return hmBitmap;
}
}catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
这是我的日志猫
10-15 16:14:07.573: I/dalvikvm(268): JNI: AttachCurrentThread (from ???.???)
10-15 16:14:07.573: I/AndroidRuntime(268): NOTE: attach of thread 'Binder Thread #3' failed
10-15 16:14:08.202: I/ARMAssembler(58): generated scanline__00000077:03545404_00000004_00000000 [ 47 ipp] (67 ins) at [0x34dd50:0x34de5c] in 429794 ns
10-15 16:14:08.532: D/dalvikvm(275): GC_EXTERNAL_ALLOC freed 865 objects / 62936 bytes in 70ms
10-15 16:14:09.382: I/ActivityManager(58): Displayed activity com.example./.MainActivity: 1907 ms (total 1907 ms)
10-15 16:14:13.232: I/ActivityManager(58): Starting activity: Intent { cmp=com.example./.Loginclass }
10-15 16:14:13.822: I/ActivityManager(58): Displayed activity com.example./.Loginclass: 580 ms (total 580 ms)
10-15 16:14:16.261: I/ActivityManager(58): Starting activity: Intent { cmp=com.example./.search.Search }
10-15 16:14:16.772: I/ARMAssembler(58): generated scanline__00000077:03515104_00000000_00000000 [ 33 ipp] (47 ins) at [0x356bc0:0x356c7c] in 378809 ns
10-15 16:14:17.011: D/dalvikvm(275): GC_FOR_MALLOC freed 4701 objects / 245664 bytes in 282ms
10-15 16:14:18.242: I/ActivityManager(58): Displayed activity com.example./.search.Search: 1954 ms (total 1954 ms)
10-15 16:14:26.783: I/ActivityManager(58): Starting activity: Intent { cmp=com.example./.searchresult.SearchResult (has extras) }
10-15 16:14:27.082: D/dalvikvm(275): GC_FOR_MALLOC freed 12519 objects / 739728 bytes in 101ms
10-15 16:14:27.082: I/dalvikvm-heap(275): Grow heap (frag case) to 3.747MB for 87396-byte allocation
10-15 16:14:27.242: I/ActivityManager(58): Displayed activity com.example./.searchresult.SearchResult: 421 ms (total 421 ms)
10-15 16:14:27.312: D/dalvikvm(275): GC_FOR_MALLOC freed 317 objects / 13368 bytes in 234ms
10-15 16:14:27.362: D/dalvikvm(275): GC_FOR_MALLOC freed 0 objects / 0 bytes in 52ms
10-15 16:14:27.362: I/dalvikvm-heap(275): Grow heap (frag case) to 3.817MB for 87396-byte allocation
10-15 16:14:27.422: D/dalvikvm(275): GC_FOR_MALLOC freed 0 objects / 0 bytes in 59ms
10-15 16:14:32.465: D/Exception while downloading url(275): java.net.SocketTimeoutException
10-15 16:14:32.465: D/Background Task(275): java.lang.NullPointerException
10-15 16:14:32.482: D/JSON Exception1(275): java.lang.NullPointerException
10-15 16:14:32.482: D/Exception(275): java.lang.NullPointerException
10-15 16:14:32.492: D/AndroidRuntime(275): Shutting down VM
10-15 16:14:32.492: W/dalvikvm(275): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
10-15 16:14:32.502: E/AndroidRuntime(275): FATAL EXCEPTION: main
10-15 16:14:32.502: E/AndroidRuntime(275): java.lang.NullPointerException
10-15 16:14:32.502: E/AndroidRuntime(275): at android.widget.SimpleAdapter.getCount(SimpleAdapter.java:93)
10-15 16:14:32.502: E/AndroidRuntime(275): at android.widget.ListView.setAdapter(ListView.java:436)
10-15 16:14:32.502: E/AndroidRuntime(275): at com.example..searchresult.SearchResult$ListViewLoaderTask.onPostExecute(SearchResult.java:236)
10-15 16:14:32.502: E/AndroidRuntime(275): at com.example..searchresult.SearchResult$ListViewLoaderTask.onPostExecute(SearchResult.java:1)
10-15 16:14:32.502: E/AndroidRuntime(275): at android.os.AsyncTask.finish(AsyncTask.java:417)
10-15 16:14:32.502: E/AndroidRuntime(275): at android.os.AsyncTask.access$300(AsyncTask.java:127)
10-15 16:14:32.502: E/AndroidRuntime(275): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
10-15 16:14:32.502: E/AndroidRuntime(275): at android.os.Handler.dispatchMessage(Handler.java:99)
10-15 16:14:32.502: E/AndroidRuntime(275): at android.os.Looper.loop(Looper.java:123)
10-15 16:14:32.502: E/AndroidRuntime(275): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-15 16:14:32.502: E/AndroidRuntime(275): at java.lang.reflect.Method.invokeNative(Native Method)
10-15 16:14:32.502: E/AndroidRuntime(275): at java.lang.reflect.Method.invoke(Method.java:521)
10-15 16:14:32.502: E/AndroidRuntime(275): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-15 16:14:32.502: E/AndroidRuntime(275): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-15 16:14:32.502: E/AndroidRuntime(275): at dalvik.system.NativeStart.main(Native Method)
10-15 16:14:32.522: W/ActivityManager(58): Force finishing activity com.example./.searchresult.SearchResult
10-15 16:14:33.042: W/ActivityManager(58): Activity pause timeout for HistoryRecord{46060820 com.example./.searchresult.SearchResult}
10-15 16:14:42.548: W/ActivityManager(58): Launch timeout has expired, giving up wake lock!
10-15 16:14:43.092: W/ActivityManager(58): Activity idle timeout for HistoryRecord{4601a300 com.example./.search.Search}
10-15 16:14:48.333: W/ActivityManager(58): Activity destroy timeout for HistoryRecord{46060820 com.example./.searchresult.SearchResult}
10-15 16:14:48.962: I/Process(275): Sending signal. PID: 275 SIG: 9
10-15 16:14:49.002: I/ActivityManager(58): Process com.example. (pid 275) has died.
10-15 16:14:49.042: I/WindowManager(58): WIN DEATH: Window{45fc9600 com.example./com.example..Loginclass paused=false}
10-15 16:14:49.042: I/WindowManager(58): WIN DEATH: Window{461154c0 com.example./com.example..searchresult.SearchResult paused=false}
10-15 16:14:49.042: I/WindowManager(58): WIN DEATH: Window{46113e10 com.example./com.example..search.Search paused=false}
10-15 16:14:49.072: I/ActivityManager(58): Start proc com.example. for activity com.example./.Loginclass: pid=290 uid=10037 gids={3003}
10-15 16:14:49.132: W/Process(58): Unable to open /proc/275/status
10-15 16:14:49.322: I/UsageStats(58): Unexpected resume of com.example. while already resumed in com.example.
10-15 16:14:49.552: W/InputManagerService(58): Got RemoteException sending setActive(false) notification to pid 275 uid 10037
10-15 16:14:50.022: I/ActivityManager(58): Displayed activity com.example./.Loginclass: 1009 ms (total 1009 ms)