我目前正在尝试用 AsyncTask 填充我的列表视图。
更新: 我更新了我的代码如下:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
new loadSomeStuff(this.getActivity()).execute();
}
public static class loadSomeStuff extends
AsyncTask<FragmentActivity, Integer, String> {
ProgressDialog dialog;
ArrayList<BeanClass4> beanClass4 = new ArrayList<BeanClass4>();
SharedPreferences prefs;
public static final String prefName = "SecretFile";
FragmentActivity activity;
public loadSomeStuff(FragmentActivity activity2) {
// TODO Auto-generated constructor stub
this.activity = activity2;
}
protected void onPreExecute() {
dialog = new ProgressDialog(activity);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.setCancelable(false);
dialog.setIndeterminate(false);
dialog.setMessage("hi");
dialog.show();
}
protected String doInBackground(FragmentActivity... params) {
// TODO Auto-generated method stub
beanClass4.clear();
String result = "";
InputStream isr = null;
try {
HttpClient httpclient = new DefaultHttpClient();
String url = "";
HttpPost httpost = new HttpPost(url);
HttpResponse resposne = httpclient.execute(httpost);
HttpEntity entity = resposne.getEntity();
isr = entity.getContent();
} catch (Exception e) {
Log.e("log_httpconnection",
"error in http connection" + e.toString());
}
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(isr, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
isr.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_json", "Error converting Result " + e.toString());
}
try {
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json = jArray.getJSONObject(i);
beanClass4.add(new BeanClass4(json.getString("PhoneName"),
json.getString("ModelNumber"), json
.getString("PhonePrice"), json
.getString("imageurl"), json
.getString("moneyunit"), json
.getString("newphone")));
}
dialog.dismiss();
} catch (Exception e) {
Log.e("lag_tag", "ERROR PARSING DATA" + e.toString());
beanClass4.clear();
}
return null;
}
protected void onProgressUpdate(Integer... progress) {
}
protected void onPostExecute(String result) {
listview.setAdapter(new MyListAdapter4(activity, beanClass4));
if (beanClass4.isEmpty()) {
Toast.makeText(activity, "No Connection", Toast.LENGTH_LONG)
.show();
}
}
}
但是,现在当我打开我的活动时。我看到一个进度对话框打招呼,然后在 2~3 秒后我在 logcat 上得到了这个:
07-23 13:18:55.236: E/AndroidRuntime(1230): FATAL EXCEPTION: main
07-23 13:18:55.236: E/AndroidRuntime(1230): java.lang.NullPointerException
07-23 13:18:55.236: E/AndroidRuntime(1230): at com.androidarabia.utils.Taby$loadSomeStuff.onPostExecute(Taby.java:268)
07-23 13:18:55.236: E/AndroidRuntime(1230): at com.androidarabia.utils.Taby$loadSomeStuff.onPostExecute(Taby.java:1)
07-23 13:18:55.236: E/AndroidRuntime(1230): at android.os.AsyncTask.finish(AsyncTask.java:631)
07-23 13:18:55.236: E/AndroidRuntime(1230): at android.os.AsyncTask.access$600(AsyncTask.java:177)
07-23 13:18:55.236: E/AndroidRuntime(1230): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
07-23 13:18:55.236: E/AndroidRuntime(1230): at android.os.Handler.dispatchMessage(Handler.java:99)
07-23 13:18:55.236: E/AndroidRuntime(1230): at android.os.Looper.loop(Looper.java:137)
07-23 13:18:55.236: E/AndroidRuntime(1230): at android.app.ActivityThread.main(ActivityThread.java:5293)
07-23 13:18:55.236: E/AndroidRuntime(1230): at java.lang.reflect.Method.invokeNative(Native Method)
07-23 13:18:55.236: E/AndroidRuntime(1230): at java.lang.reflect.Method.invoke(Method.java:511)
07-23 13:18:55.236: E/AndroidRuntime(1230): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
07-23 13:18:55.236: E/AndroidRuntime(1230): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
07-23 13:18:55.236: E/AndroidRuntime(1230): at dalvik.system.NativeStart.main(Native Method)
做什么?您的帮助将不胜感激!