我正在尝试使用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();
}
}
}
然而,现在当我打开我的活动时。我看到一个progressdialog说你好,然后在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)
做什么?您的帮助将获得很多赞赏!
答案 0 :(得分:0)
好吧,你永远不会在AsyncTask上调用实际的execute()方法......你只调用它自己的版本,它只存储活动崇敬而不调用super.execute()。实际上,重载这样的执行方法是我心目中的糟糕设计;我建议在LoadSomeStuff类中创建一个构造函数,该构造函数接收并存储对该活动的引用。这样,您在ArrayListFragment.onActivityCreated中的代码只需调用不带参数的execute()。
我没有真正遵循其余的代码,所以可能还有其他问题,我不知道。但先解决这个问题。
答案 1 :(得分:0)
你的execute方法是一个重载方法而不是重写方法。调用你的execute方法除了init你的活动变量之外什么都不做,它并不意味着从AsynTask类调用execute方法: 您可以将方法更改为构造函数:
public LoadSomeStuff(FragmentActivity activity) {
this.activity = activity;
}
并在您的活动中:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
new loadSomeStuff().execute(this.getActivity());
}
to :
new loadSomeStuff(this.getActivity()).execute();
或者你可以上课:
LoadSomeStuff extends AsyncTask<FragmentActivity, Progress, Result>{
删除你的执行方法并致电:
new loadSomeStuff()。execute(this.getActivity());
希望这有帮助:)