0

我目前正在尝试用 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)

做什么?您的帮助将不胜感激!

4

2 回答 2

0

好吧,你永远不会在 AsyncTask 上调用实际的 execute() 方法……你只调用你自己的版本,它只存储活动的敬意,而不调用 super.execute()。实际上,在我看来,像这样重载 execute 方法是不好的设计。我建议在您的 LoadSomeStuff 类中创建一个构造函数,该构造函数接收并存储对活动的引用。这样,您在 ArrayListFragment.onActivityCreated 中的代码就可以在没有参数的情况下调用 execute()。

我还没有真正遵循你的其余代码,所以可能还有其他问题,我不知道。但先解决这个问题。

于 2013-07-22T22:31:19.217 回答
0

您的执行方法是重载方法而不是覆盖方法。调用您的执行方法除了初始化您的活动变量之外什么都不做,这并不意味着从 AsynTask 类调用执行方法:您可以将方法更改为构造函数:

     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>{

删除您的执行方法并调用:

新的 loadSomeStuff().execute(this.getActivity());

希望这有帮助:)

于 2013-07-23T08:56:35.203 回答