使用CustomAdapter的ListView的NullPointerException [duplicate]

时间:2021-10-27 17:05:18

This question already has an answer here:

这个问题在这里已有答案:

CustomAdapter File included...

包含CustomAdapter文件...

1.Initializing ArrayList.

2.Initializing ListView.

3.Grapping the Data from URL and storing in ArrayList

3.从URL中包装数据并存储在ArrayList中

4.Setting ListView with CustomAdapter

4.使用CustomAdapter设置ListView

5.Please check the CustomAdapter code which below the MainActivity..

5.请检查MainActivity下面的CustomAdapter代码。

public class Screen2 extends AppCompatActivity {     

ProgressDialog pDialog;
        ArrayList<Information> record_list;
        ListView list_view;
        CustomAdapter listAdapter;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.screen2);
            record_list=new ArrayList<Information>();
            list_view=(ListView) findViewById(R.id.list_view);
            new Test().execute();
        }

        public class Test extends AsyncTask<String, Void, ArrayList<Information>> {
            @Override
            protected void onPreExecute() {

                super.onPreExecute();

                // Showing progress dialog
                pDialog = new ProgressDialog(Screen2.this);
                pDialog.setMessage("Please wait...");
                pDialog.setCancelable(false);
                pDialog.show();
            }

            @Override
            protected ArrayList<Information> doInBackground(String... params) {

                String jsonStr = makeServiceCall();
                try {
                    JSONArray jsonArray=new JSONArray(jsonStr);
                    for(int i=0;i<jsonArray.length();i++){
                        JSONObject jsonObject=jsonArray.getJSONObject(i);
                        String id=jsonObject.getString("id");
                        String name=jsonObject.getString("label");
                        String email=jsonObject.getString("email");
                        Information information=new Information(id,name,email);
                        record_list.add(information);
                        return record_list;
                    }
                    System.out.println(jsonStr);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                return null;
            }

            @Override
            protected void onPostExecute(ArrayList<Information> s) {
                super.onPostExecute(s);
                // Dismiss the progress dialog
                //listAdapter=new CustomAdapter(Screen2.this,record_list);
                listAdapter=new CustomAdapter(Screen2.this,record_list);
                list_view.setAdapter(listAdapter);
                if (pDialog.isShowing())
                    pDialog.dismiss();

                /**
                 * Updating parsed JSON data into ListView
                 * */
            }

            public String makeServiceCall(){
                String response = null;
                try {
                    URL url = new URL("http://192.168.1.109:9000/tasks2");
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setRequestMethod("GET");
                    // read the response
                    InputStream in = new BufferedInputStream(conn.getInputStream());
                    response = convertStreamToString(in);
                } catch (MalformedURLException e) {
                    System.out.println("MalformedURLException: " + e.getMessage());
                } catch (ProtocolException e) {
                    System.out.println("MalformedURLException: " + e.getMessage());
                } catch (IOException e) {
                    System.out.println("MalformedURLException: " + e.getMessage());
                } catch (Exception e) {
                    System.out.println("MalformedURLException: " + e.getMessage());
                }
                return response;
            }

            private String convertStreamToString(InputStream is) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                StringBuilder sb = new StringBuilder();

                String line;
                try {
                    while ((line = reader.readLine()) != null) {
                        sb.append(line).append('\n');
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println(sb);
                return sb.toString();
            }
        }

}

CustomAdapter.java:(Here is my CustomAdapter File) I don't see any error in this file too...

CustomAdapter.java:(这是我的CustomAdapter文件)我在这个文件中也没有看到任何错误...

public class CustomAdapter extends ArrayAdapter<Information> {

        public CustomAdapter(Context c, ArrayList<Information> record) {
            super(c, R.layout.custom_row, record);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            // Get the data item for this position

            Information information = getItem(position);

            //LayoutInflater inflater=LayoutInflater.from(getContext());

            // Check if an existing view is being reused, otherwise inflate the view

            convertView = LayoutInflater.from(getContext()).inflate(R.layout.custom_row, parent, false);

            // Lookup view for data population

            TextView record_id = (TextView) convertView.findViewById(R.id.record_id);

            TextView record_name = (TextView) convertView.findViewById(R.id.record_name);

            TextView record_email = (TextView) convertView.findViewById(R.id.record_email);

            // Populate the data into the template view using the data object

            record_id.setText(information.id);
            record_name.setText(information.name);
            record_email.setText(information.email);

            return convertView;
        }
}

1 个解决方案

#1


0  

There are logical errors in code:

代码中存在逻辑错误:

  1. The for loop has a return statement inside it while you have to iterate so do this for a good design define another variable for Arraylist lets say s and the code would look like this:

    for循环在其中有一个return语句,而你必须迭代所以这样做一个好的设计为Arraylist定义另一个变量让我们说s,代码看起来像这样:

    @Override
    protected ArrayList<Information> doInBackground(String... params) {
    
        String jsonStr = makeServiceCall();
        ArrayList<Information> s = new ArrayList<Information>();
        try {
            JSONArray jsonArray=new JSONArray(jsonStr);
            for(int i=0;i<jsonArray.length();i++){
                JSONObject jsonObject=jsonArray.getJSONObject(i);
                String id=jsonObject.getString("id");
                String name=jsonObject.getString("label");
                String email=jsonObject.getString("email");
                Information information=new Information(id,name,email);
                s.add(information); 
            }
            System.out.println(jsonStr);
            return s;
        } catch (JSONException e) {
            e.printStackTrace();
        }
    
        return null;
    }
    
  2. Then on your onPostExecute make record_list be s like this:

    然后在你的onPostExecute make record_list是这样的:

    @Override
    protected void onPostExecute(ArrayList<Information> s) {
        super.onPostExecute(s);
        // Dismiss the progress dialog
        //listAdapter=new CustomAdapter(Screen2.this,record_list);
        record_list = s;
        listAdapter=new CustomAdapter(Screen2.this,record_list);
        list_view.setAdapter(listAdapter);
        if (pDialog.isShowing())
            pDialog.dismiss();
    
        /**
         * Updating parsed JSON data into ListView
         * */
    }
    

#1


0  

There are logical errors in code:

代码中存在逻辑错误:

  1. The for loop has a return statement inside it while you have to iterate so do this for a good design define another variable for Arraylist lets say s and the code would look like this:

    for循环在其中有一个return语句,而你必须迭代所以这样做一个好的设计为Arraylist定义另一个变量让我们说s,代码看起来像这样:

    @Override
    protected ArrayList<Information> doInBackground(String... params) {
    
        String jsonStr = makeServiceCall();
        ArrayList<Information> s = new ArrayList<Information>();
        try {
            JSONArray jsonArray=new JSONArray(jsonStr);
            for(int i=0;i<jsonArray.length();i++){
                JSONObject jsonObject=jsonArray.getJSONObject(i);
                String id=jsonObject.getString("id");
                String name=jsonObject.getString("label");
                String email=jsonObject.getString("email");
                Information information=new Information(id,name,email);
                s.add(information); 
            }
            System.out.println(jsonStr);
            return s;
        } catch (JSONException e) {
            e.printStackTrace();
        }
    
        return null;
    }
    
  2. Then on your onPostExecute make record_list be s like this:

    然后在你的onPostExecute make record_list是这样的:

    @Override
    protected void onPostExecute(ArrayList<Information> s) {
        super.onPostExecute(s);
        // Dismiss the progress dialog
        //listAdapter=new CustomAdapter(Screen2.this,record_list);
        record_list = s;
        listAdapter=new CustomAdapter(Screen2.this,record_list);
        list_view.setAdapter(listAdapter);
        if (pDialog.isShowing())
            pDialog.dismiss();
    
        /**
         * Updating parsed JSON data into ListView
         * */
    }