这里NPE的原因是什么?

时间:2016-05-03 07:31:24

标签: android json fragment warnings

我已经提到了我的运行时错误行27,118,62和134的行号.Logcat也在那里。我在30-35秒后收到这些错误。

为什么我要在第27行获得

  

第134行:ListView listView =(ListView)   。getView()findViewById(R.id.list4);

警告显示可能导致NPE

  public class Quizze extends android.support.v4.app.Fragment {  //line 27


JSONObject jsonobject;
JSONArray jsonarray;
String idno = " ";



ArrayList<Details> details;
LinearLayout layout;
ArrayList<String> nameList;
TextView textView;

public Quizze() {
}



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    SharedPreferences pref = getActivity().getPreferences(0);
    pref.getString("url-address", "empty");

    new DownloadJSON().execute();
    return inflater.inflate(R.layout.quiz_result, container, false);





}


class DownloadJSON extends AsyncTask<Void, Void, Void> {  //line 62

    @Override
    protected Void doInBackground(Void... params) {


        SharedPreferences pref = getActivity().getPreferences(0);
        String url = pref.getString("url-address", "empty");

        details = new ArrayList<>();

        jsonobject = JSONfunctions
                .getJSONfromURL(url);

        try {
            // Locate the NodeList name
            jsonarray = jsonobject.getJSONArray("results");
            for (int i = 0; i < jsonarray.length(); i++) {
                jsonobject = jsonarray.getJSONObject(i);
                Collections.reverse(details);
                if (jsonobject.getString("resType").contains("Multiple Choice Questions")) {
                    {
                        Details detailAll = new Details();

                        detailAll.setResType(jsonobject.optString("resType"));
                        detailAll.setName(jsonobject.optString("resName"));
                        detailAll.setUrl(jsonobject.optString("resLink"));
                        detailAll.setId(jsonobject.optInt("id"));


                        details.add(detailAll);
                        Collections.reverse(details);
                    }
                }

            }


        } catch (JSONException e) {
            Log.e("Error", "" + e.getMessage());
            e.printStackTrace();
        }
        return null;


}

    @Override
    protected void onPostExecute(Void args) {


        nameList = new ArrayList<>();
        for (Details bean : details) {
            nameList.add(bean.getResType());


            aero(details);  //line 118
        }
    }
}

private void aero(List<Details> mList) {


    for (Details bean : details) {
        final String urlChar = bean.getUrl();

        idno = String.valueOf(bean.getId());

        if (bean.getResType().equals("Multiple Choice Questions"))

        {
            ListView listView = (ListView) getView().findViewById(R.id.list4);       //Line 134

            CustomListViewAdapterMindMaps adapter = new CustomListViewAdapterMindMaps(getContext(),
                    R.layout.quiz_result, mList);
            listView.setAdapter(adapter);


            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {


                    Toast.makeText(getActivity(),
                            "Click ListItem Number " + idno, Toast.LENGTH_LONG);


                    Intent intent = new Intent(getContext(), Main_Activity_For_Quiz.class);
                    //add data to the Intent object
                    intent.putExtra("idquiz", idno);
                    intent.putExtra("urlchar", urlChar);
                    Toast.makeText(getActivity(),
                            "Click ListItem Number " +idno+ urlChar, Toast.LENGTH_LONG)
                            .show();
                    //start the second activity
                     startActivity(intent);

                }


            });
        }
    }

   }
}




 FATAL EXCEPTION: main
                                                                        Process: com.example.wonder5.player, PID: 21792
                                                                        java.lang.NullPointerException
                                                                            at com.example.wonder5.player.Quizze.aero(Quizze.java:134)
                                                                            at com.example.wonder5.player.Quizze.access$000(Quizze.java:27)
                                                                            at com.example.wonder5.player.Quizze$DownloadJSON.onPostExecute(Quizze.java:118)
                                                                            at com.example.wonder5.player.Quizze$DownloadJSON.onPostExecute(Quizze.java:62)

1 个答案:

答案 0 :(得分:1)

IDE警告您getView()不是NULL safe 如果你改变:

ListView listView = (ListView) getView().findViewById(R.id.list4);

为:

ListView listView;
View view = getView();
if(view != null){
listView = (ListView) view .findViewById(R.id.list4);
}

你不会再得到它了