2 qq 33753801 qq_33753801 于 2016.01.20 15:11 提问

求指教访问网络数据后解析总出现空指针的问题

我在主类中用了一个listview绑定访问网络的数据出来效果后,点击item后,跳转到另外的一个activity里再次访问一个网络数据还是listview的布局,两张页面的数据没有必然联系,只是重复两次访问而已,用的适配器都一样,访问的方法用的都是HttpClient,第一个能够出来,而第二个就出不来?请大神指教

3个回答

devmiao
devmiao   Ds   Rxr 2016.01.20 17:52
qq_33689414
qq_33689414   2016.01.20 23:02

没代码怎么看,我也是醉了

qq_33753801
qq_33753801   2016.01.21 10:37

这是其中出问题的那段代码,还请诸位大牛给出意见建议啊
package com.hhh.text_frag;

import java.io.IOException;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import com.google.gson.Gson;
import com.hhh.text_base.TwoBase;
import com.hhh.text_daotwo.One;
import com.hhh.text_daotwo.Three_two;
import com.hhh.text_xiaoshuo.R;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

public class FragTwo extends Fragment {
private String twoactivity_listview_url = "http://apis.juhe.cn/cook/category?key=327bea0541441681997b707f5063b252";
private ListView twoac_listview;
private String strstr;
private List list;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view=View.inflate(getActivity(), R.layout.frag2,null);

        twoac_listview=(ListView)view.findViewById(R.id.twoac_listview);            

        new Thread(new Runnable(){
                @Override
                public void run() {
                    //你请求网络的操作
                    work();
                }
            }).start();

        return view;
    }

    private void work() {
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(twoactivity_listview_url);
        try {
            HttpResponse hr = client.execute(post);
            int ss=hr.getStatusLine().getStatusCode();
            if (ss==200) {
                HttpEntity httpentity = hr.getEntity();
                strstr = EntityUtils.toString(httpentity);

                Log.i("CNM", "cccccccccccccccccccccccccCcccccccccccc" + strstr);
            }

            Gson gs = new Gson();
            One one=gs.fromJson(strstr, One.class);
            list=one.getResult().getList();
            Log.i("AAA",list.size()+"========================");

            getActivity().runOnUiThread(new Runnable() {    
                @Override
                public void run() {
                    twoac_listview.setAdapter(new TwoBase(getActivity(), list));
                }
            });

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block              e.printStackTrace();
        }

    }

}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!