第二次,第三次加载时出现Android错误

时间:2013-11-02 18:18:36

标签: java android android-fragments

我正在制作3个片段页面应用程序,并且有2个不同的片段与http json但具有相同的代码。该应用程序工作正常,但当我去碎片和另一个然后回来我得到强制关闭

   threadid=1: thread exiting with uncaught exception (group=0x411f42a0)
   FATAL EXCEPTION: main
   java.lang.NullPointerException
   at com.example.fddaaf.Fragment3.updateList(Fragment3.java:55)
   at com.example.fddaaf.Fragment3$thirdDownloadFilesTask.onPostExecute(Fragment3.java:83)
   at com.example.fddaaf.Fragment3$thirdDownloadFilesTask.onPostExecute(Fragment3.java:1)
   at android.os.AsyncTask.finish(AsyncTask.java:631)
   at android.os.AsyncTask.access$600(AsyncTask.java:177)
   at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
   at android.os.Handler.dispatchMessage(Handler.java:99)
   at android.os.Looper.loop(Looper.java:137)
   at android.app.ActivityThread.main(ActivityThread.java:4898)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
   at dalvik.system.NativeStart.main(Native Method)

这是Fragment3类

public class Fragment3 extends Fragment  {
  private ArrayList<thirdFeedItem> thirdfeedList;;
  private ProgressBar progresssbar;
  private ListView thirdfeedListView;

  @Override  
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {  
    View thirdrootView = inflater.inflate(R.layout.third, container, false);

    progresssbar = (ProgressBar)thirdrootView.findViewById(R.id.progresssBar);
    String url = "";
    new thirdDownloadFilesTask().execute(url);
    return thirdrootView;
  } 

  public void updateList() {
    thirdfeedListView= (ListView)getActivity().findViewById(R.id.third_list);
    thirdfeedListView.setVisibility(View.VISIBLE);
    if (thirdfeedListView.isShown()) {
      progresssbar.setVisibility(View.GONE); 
    }

    thirdfeedListView.setAdapter(new thirdCustomListAdapter(getActivity(), thirdfeedList));
    thirdfeedListView.setOnItemClickListener(new OnItemClickListener() {

      @Override
      public void onItemClick(AdapterView<?> a, View v, int position, long id) {
        Object o = thirdfeedListView.getItemAtPosition(position);
        thirdFeedItem thirdData = (thirdFeedItem) o;

        Intent intent = new Intent(getActivity(), thirdFeedDetailsActivity.class);
        intent.putExtra("thirdfeed", thirdData);
        startActivity(intent);
      }
    });
  }

  public class thirdDownloadFilesTask extends AsyncTask<String, Integer, Void> {

    @Override
    protected void onPostExecute(Void result) {
      if (null != thirdfeedList) {
        updateList();
      }
    }

    @Override
    protected Void doInBackground(String... params) {
      String url = params[0];

      // getting JSON string from URL
      JSONObject json = getJSONFromUrl(url);

      //parsing json data
      parseJson(json);
      return null;
    }
  }

  public JSONObject getJSONFromUrl(String url) {
    InputStream is = null;
    JSONObject jObj = null;
    String json = null;

    // Making HTTP request
    try {
      // defaultHttpClient
      DefaultHttpClient httpClient = new DefaultHttpClient();
      HttpPost httpPost = new HttpPost(url);

      HttpResponse httpResponse = httpClient.execute(httpPost);
      HttpEntity httpEntity = httpResponse.getEntity();
      is = httpEntity.getContent();

      BufferedReader reader = new BufferedReader(new InputStreamReader(
        is, "iso-8859-1"), 8);
      StringBuilder sb = new StringBuilder();
      String line = null;
      while ((line = reader.readLine()) != null) {
        sb.append(line + "\n");
      }
      is.close();
      json = sb.toString();
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    } catch (ClientProtocolException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }

    try {
      jObj = new JSONObject(json);
    } catch (JSONException e) {
      Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;
  }

  public void parseJson(JSONObject json) {
    try {

      // parsing json object
      if (json.getString("status").equalsIgnoreCase("ok")) {
        JSONArray posts = json.getJSONArray("posts");

        thirdfeedList = new ArrayList<thirdFeedItem>();
        for (int i = 0; i < posts.length(); i++) {
          JSONObject post = (JSONObject) posts.getJSONObject(i);
          thirdFeedItem item = new thirdFeedItem();
          item.setthirdTitle(post.getString("title"));
          item.setthirdDate(post.getString("description"));
          item.setthirdId(post.getString("id"));
          item.setthirdUrl(post.getString("url"));
          item.setthirdContent(post.getString("description"));
          JSONArray attachments = post.getJSONArray("attachments");

          if (null != attachments && attachments.length() > 0) {
            JSONObject attachment = attachments.getJSONObject(0);
            if (attachment != null)
              item.setthirdAttachmentUrl(attachment.getString("url"));
          }

          thirdfeedList.add(item);
        }
      }
    } catch (JSONException e) {
      e.printStackTrace();
    }
  }
}

我检查了一切,有什么问题?谢谢

0 个答案:

没有答案