由于我是Android的新手,我无法弄清楚为什么ListView不从ArrayList获取值,因此显示NullPointerException。
我正在尝试使用自定义对话框从TextView点击的json链接查看ListView中的内容。
logcat的:
02-26 05:42:22.905: E/AndroidRuntime(1441): FATAL EXCEPTION: main
02-26 05:42:22.905: E/AndroidRuntime(1441): java.lang.NullPointerException
02-26 05:42:22.905: E/AndroidRuntime(1441): at com.example.customalertdialoglistview.MainActivity$1.onClick(MainActivity.java:106)
02-26 05:42:22.905: E/AndroidRuntime(1441): at android.view.View.performClick(View.java:4204)
02-26 05:42:22.905: E/AndroidRuntime(1441): at android.view.View$PerformClick.run(View.java:17355)
02-26 05:42:22.905: E/AndroidRuntime(1441): at android.os.Handler.handleCallback(Handler.java:725)
02-26 05:42:22.905: E/AndroidRuntime(1441): at android.os.Handler.dispatchMessage(Handler.java:92)
02-26 05:42:22.905: E/AndroidRuntime(1441): at android.os.Looper.loop(Looper.java:137)
02-26 05:42:22.905: E/AndroidRuntime(1441): at android.app.ActivityThread.main(ActivityThread.java:5041)
02-26 05:42:22.905: E/AndroidRuntime(1441): at java.lang.reflect.Method.invokeNative(Native Method)
02-26 05:42:22.905: E/AndroidRuntime(1441): at java.lang.reflect.Method.invoke(Method.java:511)
02-26 05:42:22.905: E/AndroidRuntime(1441): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-26 05:42:22.905: E/AndroidRuntime(1441): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-26 05:42:22.905: E/AndroidRuntime(1441): at dalvik.system.NativeStart.main(Native Method)
MainActivity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vehino = (TextView) findViewById(R.id.textView1);
// add button listener
vehino.setOnClickListener(new OnClickListener() {
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Override
public void onClick(View arg0) {
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Select Id...");
ThreadPolicy tp = ThreadPolicy.LAX;
StrictMode.setThreadPolicy(tp);
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String,String>> state_cons=new ArrayList<HashMap<String,String>>();
try {
json2=JSONfunctions
.getJSONfromURL("http://192.168.1.63/inba/mtc/mtc_enduser/vehicle_id_pop.php");
jsonarray = json2.getJSONArray("vehicle");
// looping through All Contacts
for (int i=0;i<jsonarray.length();i++){
JSONObject c=jsonarray.getJSONObject(i);
HashMap<String,String> valuePairs=new HashMap<String,String>();
valuePairs.put("vehicle_id",c.getString(TAG_id));
valuePairs.put("vehicle_no", c.getString(TAG_no));
String state1 = c.getString(TAG_id);
String state2 = c.getString(TAG_no);
Log.d("states",state1 + state2);
state_cons.add(valuePairs);
state_cons.addAll(state_cons);
shippingid=state1;
cids=state2;
listview = (ListView) findViewById(R.id.listView1);
adapter = new ListViewAdapter(MainActivity.this, arraylist);
listview.setAdapter(adapter);
}
// set the custom dialog components - text, image and button
dialog.show();
}catch(JSONException e){
e.printStackTrace();
}
}
});}}
ListViewAdapter类:
public class ListViewAdapter extends BaseAdapter {
Context context;
LayoutInflater inflater;
ArrayList<HashMap<String, String>> data;
HashMap<String, String> resultp = new HashMap<String, String>();
public ListViewAdapter(Context context,
ArrayList<HashMap<String, String>> arraylist) {
this.context = context;
data = arraylist;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
TextView vehicle_id;
TextView vehicle_no;
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.listview_item, null);
}
/*inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);*/
/*View itemView = inflater.inflate(R.layout.listview_item, parent, false);*/
resultp = data.get(position);
vehicle_id = (TextView) v.findViewById(R.id.vid);
Log.d("vehicle_id", "name" + vehicle_id);
vehicle_no = (TextView) v.findViewById(R.id.vno);
Log.d("vehicle_no", "name" + vehicle_no);
vehicle_id.setText(resultp.get(MainActivity.TAG_id));
vehicle_no.setText(resultp.get(MainActivity.TAG_no));
v.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
resultp = data.get(position);
/*
* intent.putExtra("name", resultp.get(Update.NAME));
* intent.putExtra("age", resultp.get(Update.AGE));
*
*
* context.startActivity(intent);
*/
}
});
return v;
}}
谢谢。
答案 0 :(得分:0)
adapter = new ListViewAdapter(MainActivity.this, arraylist);
需要指定“arrayList”,如果将其声明为变量,则为空
答案 1 :(得分:0)
adapter = new ListViewAdapter(MainActivity.this, arraylist);
我认为arraylist值是Null .....
答案 2 :(得分:0)
我发现你可能正在主线程上运行网络相关的操作,这是不可能的。
为此目的,您应该使用Thread
或AsyncTask
。
这部分必须在后台线程中
json2=JSONfunctions.getJSONfromURL("http://192.168.1.63/inba/mtc/mtc_enduser/vehicle_id_pop.php");
如果您说第106行是listview.setAdapter(adapter);
,则listview为空。检查oyu是否在listView1
activity_main.xml
的列表视图