douyiyang6317 2014-03-13 08:20
浏览 176
已采纳

在AsyncTask中执行doInBackground()时发生错误

Hi i am using the AsyncTask in order to get data from a php files But when i run the application i get this error !

List<Online_Messages_item> o;
public class RequestPostsTask extends AsyncTask<Void, Void, Boolean> {
        ProgressDialog pdLoading;


        @Override
        protected void onPreExecute() {
            pdLoading = new ProgressDialog(Messages_online.this);
            pdLoading.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            pdLoading.setMessage("please wait ...");
            pdLoading.setIndeterminate(false);
            pdLoading.setCancelable(false);
            pdLoading.show();
        }

        @Override
        protected Boolean doInBackground(Void... params) {
            boolean status = false; 
            HttpURLConnection urlConnection = null;

            try {
                URL url = new URL("my url");

                urlConnection = (HttpURLConnection) url.openConnection();
                Charset charset = Charset.forName("UTF8");
                int responseCode = urlConnection.getResponseCode();

                if (responseCode == HttpURLConnection.HTTP_OK) {
                    BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), charset));
                    if (in != null) {
                        StringBuilder strBuilder = new StringBuilder();
                        int ch = 0;
                        while ((ch = in.read()) != -1)
                            strBuilder.append((char) ch);
                        String response = strBuilder.toString();
                        Log.d("Server response:", response);

                        JSONObject jObject = new JSONObject(response);
                        JSONArray jsonArray = jObject.getJSONArray("posts");
                        for(int i=0; i<jsonArray.length(); i++){
                            Online_Messages_item omi = new Online_Messages_item();
                            omi.setId(jsonArray.getJSONObject(i).getLong("ID"));
                            omi.settext(jsonArray.getJSONObject(i).getString("Text"));
                            omi.setlikes(jsonArray.getJSONObject(i).getString("Likes"));
                            omi.setpt(jsonArray.getJSONObject(i).getString("PublishTime"));
                            o.add(omi);
                        }
                        if(o.size() >= 1)
                            status = true;
                    }

                    in.close();
                }
            }
            catch(MalformedURLException e){
                e.printStackTrace();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
            catch (JSONException e) {
                e.printStackTrace();
            }
            finally {
                urlConnection.disconnect();
            }

            return status;
        }

        @Override
        protected void onPostExecute(Boolean result) {
            BaseAdapter ba=new Messages_online_adapter(Messages_online.this,o);
            listView.setAdapter(ba);
            pdLoading.dismiss();

        }
    }

and here is the logcat

03-13 08:12:05.111: E/AndroidRuntime(1232): FATAL EXCEPTION: AsyncTask #1 03
-13 08:12:05.111: E/AndroidRuntime(1232): java.lang.RuntimeException: An error occured while executing doInBackground()
03-13 08:12:05.111: E/AndroidRuntime(1232):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
03-13 08:12:05.111: E/AndroidRuntime(1232):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
03-13 08:12:05.111: E/AndroidRuntime(1232):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
03-13 08:12:05.111: E/AndroidRuntime(1232):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
03-13 08:12:05.111: E/AndroidRuntime(1232):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-13 08:12:05.111: E/AndroidRuntime(1232):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
03-13 08:12:05.111: E/AndroidRuntime(1232):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
03-13 08:12:05.111: E/AndroidRuntime(1232):     at java.lang.Thread.run(Thread.java:856)
03-13 08:12:05.111: E/AndroidRuntime(1232): Caused by: java.lang.NullPointerException
03-13 08:12:05.111: E/AndroidRuntime(1232):     at mr.zereshk.online.Messages_online$RequestPostsTask.doInBackground(Messages_online.java:80)
03-13 08:12:05.111: E/AndroidRuntime(1232):     at mr.zereshk.online.Messages_online$RequestPostsTask.doInBackground(Messages_online.java:1)
03-13 08:12:05.111: E/AndroidRuntime(1232):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
03-13 08:12:05.111: E/AndroidRuntime(1232):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-13 08:12:05.111: E/AndroidRuntime(1232):     ... 4 more

Any Help ? Thanks in advance!

  • 写回答

2条回答 默认 最新

  • duan205571 2014-03-13 08:23
    关注
    List<Online_Messages_item> o;
    

    you never initialize o.

    Somewhere you should put o = new ArrayList<Online_Messages_item>();. I suggest a constructor

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 这个复选框什么作用?
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下