Android如何将json解析为扩展片段的类中的ListView

Android如何将json解析为扩展片段的类中的ListView,android,json,android-fragments,Android,Json,Android Fragments,我试图做一个片段,里面有一个listview,我试图使用JSON填充listview。我只有一个错误,我不知道把我的一个错误放在哪里。错误是unreachable语句,当我将getJSON()放在}下面时,它表示方法声明无效 下面是我的代码,其中包含一个片段中的listview。指向getJSON()时出错;在rootview下面。谢谢 public class News extends Fragment { private ListView lv; private String

我试图做一个片段,里面有一个listview,我试图使用JSON填充listview。我只有一个错误,我不知道把我的一个错误放在哪里。错误是unreachable语句,当我将getJSON()放在}下面时,它表示方法声明无效

下面是我的代码,其中包含一个片段中的listview。指向getJSON()时出错;在rootview下面。谢谢

public class News extends Fragment {
    private ListView lv;
    private String JSON_STRING;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(news, container, false);
        lv = (ListView) rootView.findViewById(R.id.listView3);
        return rootView;
        getJSON();
    }




    private void showResult(){
        JSONObject jsonObject = null;
        ArrayList<HashMap<String,String>> list = new ArrayList<>();
        try {
            jsonObject = new JSONObject(JSON_STRING);
            JSONArray result = jsonObject.getJSONArray(Config.TAG_JSON_ARRAY1);

            for(int i = 0; i<result.length(); i++){
                JSONObject jo = result.getJSONObject(i);
                String NID = jo.getString(Config.TAG_NID);
                String title = jo.getString(Config.TAG_title);
                String content = jo.getString(Config.TAG_content);
                String n_date = jo.getString(Config.TAG_n_date);
                HashMap<String,String> match = new HashMap<>();
                match.put(Config.TAG_NID, NID);
                match.put(Config.TAG_title,title);
                match.put(Config.TAG_content,content);
                match.put(Config.TAG_n_date,n_date);
                list.add(match);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        ListAdapter adapter = new SimpleAdapter(
                getActivity(), list, R.layout.newsadapterlayout,
                new String[]{Config.TAG_title,Config.TAG_content, Config.TAG_n_date, Config.TAG_NID},
                new int[]{ R.id.title, R.id.content, R.id.n_date});
        lv.setAdapter(adapter);
    }
    private void getJSON(){
        class GetJSON extends AsyncTask<Void,Void,String> {


            @Override
            protected void onPreExecute() {
                super.onPreExecute();

            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                JSON_STRING = s;
                showResult();
            }

            @Override
            protected String doInBackground(Void... params) {
                RequestHandler rh = new RequestHandler();
                String s = rh.sendGetRequest(Config.URL_NEWS);
                return s;
            }
        }
        GetJSON gj = new GetJSON();
        gj.execute();
    }



}
公共类新闻扩展片段{
私有ListView lv;
私有字符串JSON_字符串;
@可空
@凌驾
创建视图时的公共视图(LayoutFlater充气机、@Nullable ViewGroup容器、@Nullable Bundle savedInstanceState){
视图根视图=充气机。充气(新闻、容器、错误);
lv=(ListView)rootView.findViewById(R.id.listView3);
返回rootView;
getJSON();
}
私有void showResult(){
JSONObject JSONObject=null;
ArrayList=新建ArrayList();
试一试{
jsonObject=新的jsonObject(JSON_字符串);
JSONArray result=jsonObject.getJSONArray(Config.TAG_JSON_ARRAY1);
for(int i=0;i您的
getJSON()
调用在
return
语句之后,因此不可能执行它。这就是“unreachable statement”错误的意思


您可以移动
getJSON()
在您的
return
语句之前调用该行,它应该会解决该问题。如果不运行它,很难知道是否会出现其他问题,但至少应该解决一个问题。

Protip:将您不理解的错误放入您喜爱的搜索引擎中。在这种情况下,搜索
java不可访问语句
将产生大量结果,解释您的问题是在返回后有一条语句

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View rootView = inflater.inflate(news, container, false);
    lv = (ListView) rootView.findViewById(R.id.listView3);
    return rootView; // <- Returning from the function here
    getJSON(); // <- How is this supposed to get executed if you already returned?
}
@Nullable
@凌驾
创建视图时的公共视图(LayoutFlater充气机、@Nullable ViewGroup容器、@Nullable Bundle savedInstanceState){
视图根视图=充气机。充气(新闻、容器、错误);
lv=(ListView)rootView.findViewById(R.id.listView3);
返回rootView//