问题
I have a listview that have 5 item in it if am trying to set error on the 4 th and 5th item .Then it is throwing a null pointer exception. Exception
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)
Code To set Error
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
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);
}
This exception is occuring in case of 4 and 5 will be the value of i. Please help me to resolve the issue
回答1:
The following should solve your problem. Please read the comments for explanation:
class data extends BaseAdapter {
String[] Title;
Activity activity;
ArrayList<Integer> errorPositionList;
public data (MainActivity mainActivity, String[] text) {
Title = text;
activity = mainActivity;
errorPositionList = new ArrayList<Integer>();
}
// Add this method
public void setErrorList(ArrayList<Integer> eList) {
errorPositionList = eList;
}
....
....
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);
if (errorPositionList.contains(position)) {
title.setText(Title[position]);
title.setError("Please change the data");
} else {
title.setText(Title[position]);
}
return (row);
}
}
And the OnClickListener should now look like:
click.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
allValues = myAdapter.getAllValues();
Log.d("this is my array", "arr: " + Arrays.toString(allValues));
// Declare and initiate the ArrayList
ArrayList<Integer> errorList = new ArrayList<Integer>();
for(int i = 0; i < allValues.length; i++){
if(allValues[i] == passengerList[i]){
Log.d("Lop Count", ""+allValues[i]+"="+i);
// Add position to errorList
errorList.add(i);
}
}
myAdapter = new PassengerListView(MainActivity.this, allValues);
listView.setAdapter(myAdapter);
// Use setErrorList(ArrayList<Integer> eList) method from adapter
myAdapter.setErrorList(errorList);
// Update the list
// The if statement inside getView() will set the error message
myAdapter.notifyDataSetChanged();
}
}
Update:
OP will need to implement error notification. Relying on setError(CharSequence) will not work in this case.
回答2:
You should consider that getChildAt(4) may return null .
you can setError() in your adapter's method getView() where position will be 4 . . .
EDITED:
class data extends BaseAdapter {
String [] passengersList;
String[] Title;
Activity activity;
public data (MainActivity mainActivity, String[] text, String[] passengersList) {
Title = text;
activity = mainActivity;
this.passengersList = passengersList;
}
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);
if(position == 4){
checkAndSetError(title);
}
title.setText(Title[position]);
return (row);
}
private void checkAndSetError(TextView title, int position){
allValues = this.Title;
Log.d("this is my array", "arr: " + Arrays.toString(allValues));
for(int i=0;i<allValues.length;i++){
if(allValues[position] == this.passengerList[i]){
Log.d("Lop Count", ""+allValues[i]+"="+i);
title.setError("Please change the data");
}
}
}
}
It Should look something like this.
回答3:
ListView component did not render element on position 4. It is a lazy component that renders only what is on screen. So don't assume that getChildAt returns a non null view.
Tell your adapter to show error at position 4 and let ListView know that data set changed by calling notifyDataSetChanged.
- Extend your adapter with list of position, where to show error.
- On click, add position to that list.
- Call
notifyDataSetChangedfrom adapter. - Change
getViewmethod to show error if it is rendering a view with error.
来源:https://stackoverflow.com/questions/18374921/listview-item-set-error-android