Java 从数据库中获取值时出错
我正在尝试从数据库获取值,在获取时,我可以看到由于此错误而导致的此错误,我的值未显示在我的活动中Java 从数据库中获取值时出错,java,android,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&
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)
致命异常:main
java.lang.NullPointerException
位于java.util.Arrays$ArrayList。(Arrays.java:38)
位于java.util.Arrays.asList(Arrays.java:154)
位于android.widget.ArrayAdapter.(ArrayAdapter.java:128)
在com.weavearound.schools.weavearound.CustomList.(CustomList.java:21)
位于com.weavearound.schools.weavearound.events.showJSON(events.java:61)
访问com.weavearound.schools.weavearound.events.access$000(events.java:17)
位于com.weavearound.schools.weavearound.events$1.onResponse(events.java:44)
位于com.weavearound.schools.weavearound.events$1.onResponse(events.java:41)
位于com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:67)
位于com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
位于com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
位于android.os.Handler.handleCallback(Handler.java:605)
位于android.os.Handler.dispatchMessage(Handler.java:92)
位于android.os.Looper.loop(Looper.java:137)
位于android.app.ActivityThread.main(ActivityThread.java:4517)
位于java.lang.reflect.Method.Invokenactive(本机方法)
位于java.lang.reflect.Method.invoke(Method.java:511)
在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)上
位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
在dalvik.system.NativeStart.main(本机方法)
我的客户列表代码:
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;
}
}
公共类CustomList扩展了ArrayAdapter{
私有字符串[]id;
私有字符串[]名称;
//私人字符串[]电子邮件;
私人活动语境;
公共自定义列表(活动上下文、字符串[]ID、字符串[]名称){
超级(上下文、右布局、列表、视图、布局、ID);
this.context=上下文;
this.ids=ids;
这个名称=名称;
//这个。电子邮件=电子邮件;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
LayoutInflater充气器=上下文。getLayoutInflater();
View listViewItem=充气机。充气(R.layout.list\u View\u 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(id[position]);
textViewName.setText(名称[位置]);
//textViewEmail.setText(电子邮件[位置]);
返回listViewItem;
}
}
在这里获取值:
私有void sendRequest(){
StringRequest-StringRequest=新的StringRequest(JSON\u URL,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
showJSON(响应);
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
Toast.makeText(events.this、error.getMessage()、Toast.LENGTH_LONG.show();
}
});
RequestQueue RequestQueue=Volley.newRequestQueue(this);
添加(stringRequest);
}
私有void showJSON(字符串json){
ParseJSON pj=新的ParseJSON(json);
pj.parseJSON();
CustomList cl=new CustomList(这个,ParseJSON.ids,ParseJSON.names);
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();
}
}
公共解析json(字符串json){
this.json=json;
}
受保护的void parseJSON(){
JSONObject JSONObject=null;
试一试{
jsonObject=新的jsonObject(json);
users=jsonObject.getJSONArray(JSON_数组);
ids=新字符串[users.length()];
名称=新字符串[users.length()];
//emails=新字符串[users.length()];
对于(int i=0;i我认为您应该:
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未初始化。您可以发布代码片段吗?如果您不提供代码片段,任何人都可以处理此错误吗?我不知道JSON,但不应该是CustomList cl=new CustomList(这是,pj.ids,pj.names)如果ParseJSON是您的模型类,那么getter setter方法可能会有问题class@PiyushGupta:我添加了ParseJSON代码段,我尝试使用此解决方案,但得到相同的错误
private void showJSON(String json){
ParseJSON pj = new ParseJSON(json);
pj.parseJSON();
CustomList cl = new CustomList(this, pj.ids, pj.names);
listView.setAdapter(cl);
}