首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >E/AndroidRuntime:致命异常:主java.lang.NullPointerException列表视图

E/AndroidRuntime:致命异常:主java.lang.NullPointerException列表视图
EN

Stack Overflow用户
提问于 2017-12-19 03:35:06
回答 1查看 578关注 0票数 0

我试图从JSON响应中生成列表视图,但无法做到。行处获取错误:"list_view.setAdapter(customAdapter);“@onPostExecute

NullPointerException在上线..。

完整代码:

代码语言:javascript
运行
复制
public class Screen2 extends AppCompatActivity {
    ProgressDialog pDialog;
    ArrayList<Information> record_list;
    ListView list_view;
    CustomAdapter customAdapter;

    @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) {
            if(record_list.size()>0){record_list.clear();}
            String jsonStr = makeServiceCall();
            //ssen = new ArrayList<Information>();
            try {
                //HashMap<String,String> hashMap=new HashMap<>();
                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);
                    System.out.println("id: " + id + "name:" + name + "email" + email);
                    record_list.add(information);
                }
                System.out.println(jsonStr);
                return record_list;
                //return record_list;
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(ArrayList<Information> s) {
            super.onPostExecute(s);
            for(int i=0;i<record_list.size();i++){
                System.out.println(record_list.get(i));
            }
            if(record_list!=null && !record_list.isEmpty()){
                customAdapter=new CustomAdapter(Screen2.this,record_list);
                list_view.setAdapter(customAdapter);
            }
            else {
                Toast.makeText(Screen2.this, "Array List is Empty", Toast.LENGTH_SHORT).show();
            }

            //ListAdapter listAdapter1=new ArrayAdapter<String>(Screen2.this,R.layout.custom_row,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.127: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();
        }
    }
}

请查一下上面的代码.如果有人能解决这个问题,我会很有帮助的。

代码语言:javascript
运行
复制
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.hariprasad.healthpassportnew1.Screen2$Test.onPostExecute(Screen2.java:99)
at com.example.hariprasad.healthpassportnew1.Screen2$Test.onPostExecute(Screen2.java:51)
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:4960)
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:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-19 05:09:49

也许您的record_list是空的,所以请添加

代码语言:javascript
运行
复制
 if (record_list.size > 0) {
  list_view.setAdapter(customAdapter);
 }

在添加record_list中的数据之前,请澄清

将此代码放在String jsonStr = makeServiceCall();之前

代码语言:javascript
运行
复制
  if (record_list.size > 0) {
   record_list.clear();
  }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47879671

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档