Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/181.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android在第二次、第三次加载时出错_Java_Android_Android Fragments - Fatal编程技术网

Java Android在第二次、第三次加载时出错

Java Android在第二次、第三次加载时出错,java,android,android-fragments,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.update

我正在制作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)
这是碎片3类

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();
    }
  }
}
公共类Fragment3扩展了Fragment{
private ArrayList thirdfeedList;;
私人ProgressBar progresssbar;
私有ListView-thirdfeedListView;
@凌驾
创建视图上的公共视图(布局充气机、视图组容器、捆绑包保存状态){
视图三视图=充气机。充气(右布局三视图,容器,假);
ProgressBar=(ProgressBar)thirdrootView.findViewById(R.id.ProgressBar);
字符串url=“”;
新建thirdDownloadFilesTask().execute(url);
返回第三个视图;
} 
公共void updateList(){
thirdfeedListView=(ListView)getActivity().findViewById(R.id.third_列表);
thirdfeedListView.setVisibility(View.VISIBLE);
if(thirdfeedListView.isShown()){
ProgressBar.setVisibility(View.GONE);
}
setAdapter(新的thirdCustomListAdapter(getActivity(),thirdfeedList));
thirdfeedListView.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
公共视图单击(适配器视图a、视图v、内部位置、长id){
对象o=thirdfeedListView.getItemAtPosition(位置);
第三个种子项目第三个数据=(第三个种子项目)o;
Intent Intent=new Intent(getActivity(),thirdFeedDetailsActivity.class);
意图。额外(“第三种饲料”,第三种数据);
星触觉(意向);
}
});
}
公共类thirdDownloadFilesTask扩展了AsyncTask{
@凌驾
受保护的void onPostExecute(void结果){
如果(null!=thirdfeedList){
updateList();
}
}
@凌驾
受保护的Void doInBackground(字符串…参数){
字符串url=params[0];
//从URL获取JSON字符串
JSONObject json=getJSONFromUrl(url);
//解析json数据
parseJson(json);
返回null;
}
}
公共JSONObject getJSONFromUrl(字符串url){
InputStream=null;
JSONObject jObj=null;
字符串json=null;
//发出HTTP请求
试一试{
//defaultHttpClient
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(url);
HttpResponse HttpResponse=httpClient.execute(httpPost);
HttpEntity HttpEntity=httpResponse.getEntity();
is=httpEntity.getContent();
BufferedReader reader=新的BufferedReader(新的InputStreamReader(
is,“iso-8859-1”),8);
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
is.close();
json=sb.toString();
}捕获(不支持的编码异常e){
e、 printStackTrace();
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
试一试{
jObj=新的JSONObject(json);
}捕获(JSONException e){
Log.e(“JSON解析器”,“错误解析数据”+e.toString());
}
//返回JSON字符串
返回jObj;
}
公共void parseJson(JSONObject json){
试一试{
//解析json对象
if(json.getString(“status”).equalsIgnoreCase(“ok”)){
JSONArray posts=json.getJSONArray(“posts”);
thirdfeedList=新的ArrayList();
对于(int i=0;i0){
JSONObject附件=附件。getJSONObject(0);
如果(附件!=null)
item.setthirdAttachmentUrl(附件.getString(“url”));
}
第三个进货清单。添加(项目);
}
}
}捕获(JSONException e){
e、 printStackTrace();
}
}
}

我检查了所有东西,有什么问题吗?谢谢

第55行是哪一行?在调用
onCreateView
之前是否可以调用
updateList
?thirdfeedListView=(ListView)getActivity()@我不明白,你在说什么?更新列表如下OnCreateView@SandraMladenovic:使用
thirdfeedListView=(ListView)thirdrootView.findViewById(R.id.third_列表)用于初始化ListView而不是getActivity的onCreateView内部