从数据库中获取值时出错

时间:2016-03-23 10:48:09

标签: java android

我正在尝试从数据库中获取值,并且在获取时我可以看到此错误,因此,我的值未显示在我的活动中

 FATAL EXCEPTION: main
 java.lang.NullPointerException
 at java.util.Arrays$ArrayList.<init>(Arrays.java:38)
 at java.util.Arrays.asList(Arrays.java:154)
 at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:128)
 at com.weavearound.schools.weavearound.CustomList.<init>(CustomList.java:21)
 at com.weavearound.schools.weavearound.events.showJSON(events.java:61)
 at com.weavearound.schools.weavearound.events.access$000(events.java:17)
 at com.weavearound.schools.weavearound.events$1.onResponse(events.java:44)
 at com.weavearound.schools.weavearound.events$1.onResponse(events.java:41)
 at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:67)
 at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
 at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
 at android.os.Handler.handleCallback(Handler.java:605)
 at android.os.Handler.dispatchMessage(Handler.java:92)
 at android.os.Looper.loop(Looper.java:137)
 at android.app.ActivityThread.main(ActivityThread.java:4517)
 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:993)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
 at dalvik.system.NativeStart.main(Native Method)

我的自定义列表代码:

public class CustomList extends ArrayAdapter<String> {
    private String[] ids;
    private String[] names;
    //private String[] emails;
    private Activity context;

    public CustomList(Activity context, String[] ids, String[] names) {
        super(context, R.layout.list_view_layout,ids);
        this.context = context;
        this.ids = ids;
        this.names= names;
       // this.emails = emails;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View listViewItem = inflater.inflate(R.layout.list_view_layout, null, true);
        TextView textViewId = (TextView) listViewItem.findViewById(R.id.textViewId);
        TextView textViewName = (TextView) listViewItem.findViewById(R.id.textViewName);
        //TextView textViewEmail = (TextView) listViewItem.findViewById(R.id.textViewEmail);

        textViewId.setText(ids[position]);
        textViewName.setText(names[position]);
       // textViewEmail.setText(emails[position]);

        return listViewItem;
    }
}

这里取值:

private void sendRequest(){

        StringRequest stringRequest = new StringRequest(JSON_URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        showJSON(response);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(events.this,error.getMessage(),Toast.LENGTH_LONG).show();
                    }
                });

        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
    }

    private void showJSON(String json){
        ParseJSON pj = new ParseJSON(json);
        pj.parseJSON();
        CustomList cl = new CustomList(this, ParseJSON.ids, ParseJSON.names);
        listView.setAdapter(cl);
    }

ParseJSON类:

public ParseJSON(String json){
        this.json = json;
    }

    protected void parseJSON(){
        JSONObject jsonObject=null;
        try {
            jsonObject = new JSONObject(json);
            users = jsonObject.getJSONArray(JSON_ARRAY);

            ids = new String[users.length()];
            names = new String[users.length()];
            //emails = new String[users.length()];

            for(int i=0;i<users.length();i++){
                JSONObject jo = users.getJSONObject(i);
                ids[i] = jo.getString(KEY_ID);
                names[i] = jo.getString(KEY_NAME);
                //emails[i] = jo.getString(KEY_EMAIL);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

1 个答案:

答案 0 :(得分:0)

我认为你应该这样做:

 private void showJSON(String json){
        ParseJSON pj = new ParseJSON(json);
        pj.parseJSON();
        CustomList cl = new CustomList(this, pj.ids, pj.names);
        listView.setAdapter(cl);
    }

您正在创建一个新的ParseJSON对象(pj)并填充此对象的ID和名称。您不应该使用ParseJSON.ids,因为ID未初始化