如果我试图在第 4 项和第 5 项上设置错误,我有一个包含 5 项的列表视图。然后它会引发空指针异常。 例外
08-22 13:34:49.523: E/AndroidRuntime(16952): FATAL EXCEPTION: main
08-22 13:34:49.523: E/AndroidRuntime(16952): java.lang.NullPointerException
08-22 13:34:49.523: E/AndroidRuntime(16952): at com.example.iweenflightbookingpage.MainActivity$1.onClick(MainActivity.java:116)
08-22 13:34:49.523: E/AndroidRuntime(16952): at android.view.View.performClick(View.java:4091)
08-22 13:34:49.523: E/AndroidRuntime(16952): at android.view.View$PerformClick.run(View.java:17072)
08-22 13:34:49.523: E/AndroidRuntime(16952): at android.os.Handler.handleCallback(Handler.java:615)
08-22 13:34:49.523: E/AndroidRuntime(16952): at android.os.Handler.dispatchMessage(Handler.java:92)
08-22 13:34:49.523: E/AndroidRuntime(16952): at android.os.Looper.loop(Looper.java:153)
08-22 13:34:49.523: E/AndroidRuntime(16952): at android.app.ActivityThread.main(ActivityThread.java:4987)
08-22 13:34:49.523: E/AndroidRuntime(16952): at java.lang.reflect.Method.invokeNative(Native Method)
08-22 13:34:49.523: E/AndroidRuntime(16952): at java.lang.reflect.Method.invoke(Method.java:511)
08-22 13:34:49.523: E/AndroidRuntime(16952): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
08-22 13:34:49.523: E/AndroidRuntime(16952): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
08-22 13:34:49.523: E/AndroidRuntime(16952): at dalvik.system.NativeStart.main(Native Method)
代码设置错误
click.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
allValues = myAdapter.getAllValues();
ArrayList<Integer> errorList = new ArrayList<Integer>();
for(int i = 0; i < allValues.length; i++){
for(int j=0;j<staticPasengerList.length;j++){
if(allValues[i] == staticPasengerList[j]){
Log.d("Lop Count", ""+allValues[i]+"="+staticPasengerList[j]);
// Add position to errorList
errorList.add(i);
}
}
}
myAdapter.setErrorList(errorList);
myAdapter.notifyDataSetChanged();
}
}
}
BaseAdapter 类
class data extends BaseAdapter {
String[] Title;
Activity activity;
public data (MainActivity mainActivity, String[] text) {
Title = text;
activity = mainActivity;
}
public String[] getAllValues() {
return Title;
}
public int getCount() {
// TODO Auto-generated method stub
return Title.length;
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = activity.getLayoutInflater();
View row ;
row = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
TextView title;
title = (TextView) row.findViewById(android.R.id.text1);
title.setText(Title[position]);
return (row);
}
在 4 和 5 将是 i 的值的情况下发生此异常。请帮我解决问题