Ошибка при прокрутке пользовательского ListView
Я создаю приложение, в котором я отображаю данные в пользовательском ListView...... данные поступают из XMLHandler. но, когда я прокручиваю список, он дает ошибку нулевого указателя.....
Есть шесть элементов, поступающих из xmlHandler одновременно, только 3 элемента отображаются на экране.... но, когда я прокручиваю, getView() должен быть вызван для оставшихся 3 элементов..... но, это вызвало еще один элемент.... следовательно, он дает нулевой указатель...... я не знаю, что делать... пожалуйста, помогите мне..... Спасибо в Adv.
* Извините, я забыл добавить getview() в мой вопрос, но он есть в моем реальном коде. я отредактировал мой код... пожалуйста, просмотрите этот код...... ***
public class SearchedRouteAdapter extends ArrayAdapter<BusGetSetters> {
private final Activity activity;
private List<BusGetSetters> listBusGS = Utils.arrayBusGetSetRoutes;
String routeID, routeName, travelsName, busType, depTime, arrTime,
seatsAvail, sellFare, ResCode;
BusGetSetters searchBus = new BusGetSetters();
public SearchedRouteAdapter(Activity activity, List<BusGetSetters> objects) {
super(activity, R.layout.route_list_item, objects);
this.activity = activity;
this.listBusGS = objects;
}
@SuppressWarnings("deprecation")
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
RouteSearchView hsView = null;
if (rowView == null) {
// Get a new instance of the row layout view
LayoutInflater inflater = activity.getLayoutInflater();
rowView = inflater.inflate(R.layout.route_list_item, null);
Log.v("Log position", "2");
// Hold the view objects in an object,
// so they don't need to be re-fetched
hsView = new RouteSearchView();
hsView.routeSVname = (TextView) rowView
.findViewById(R.id.routeName);
hsView.routeSVtravelsName = (TextView) rowView
.findViewById(R.id.travelsName);
hsView.routeSVbusType = (TextView) rowView
.findViewById(R.id.busType);
hsView.routeSVdepTime = (TextView) rowView
.findViewById(R.id.depTime);
hsView.routeSVarrTime = (TextView) rowView
.findViewById(R.id.arrTime);
hsView.routeSVseatsAvail = (TextView) rowView
.findViewById(R.id.seatsAvail);
hsView.routeSVsellFare = (TextView) rowView
.findViewById(R.id.sellFare);
hsView.routeSVcmdCheck = (Button) rowView
.findViewById(R.id.cmdCheck);
// Cache the view objects in the tag,
// so they can be re-accessed later
Log.v("Log position", "3");
rowView.setTag(hsView);
} else {
hsView = (RouteSearchView) rowView.getTag();
}
// Transfer the stock data from the data object
// to the view objects
searchBus = listBusGS.get(position);
routeID = searchBus.getRoute_id().toString();
routeName = searchBus.getService_name().toString();
travelsName = searchBus.getTravels_name().toString();
busType = searchBus.getBus_type().toString();
depTime = searchBus.getDeparture_time().toString();
arrTime = searchBus.getArival_time().toString();
seatsAvail = searchBus.getSeats_available().toString();
sellFare = searchBus.getSell_fare().toString();
hsView.routeSVname.setText(routeName);
hsView.routeSVtravelsName.setText(travelsName);
hsView.routeSVbusType.setText(busType);
hsView.routeSVdepTime.setText(depTime);
hsView.routeSVarrTime.setText(arrTime);
hsView.routeSVseatsAvail.setText(seatsAvail);
hsView.routeSVsellFare.setText(sellFare);
}
return rowView;
}
protected static class RouteSearchView {
protected TextView routeSVname;
protected TextView routeSVtravelsName;
protected TextView routeSVbusType;
protected TextView routeSVdepTime;
protected TextView routeSVarrTime;
protected TextView routeSVseatsAvail;
protected TextView routeSVsellFare;
protected Button routeSVcmdCheck;
}
}
Ниже приводится вывод Logcat:
02-01 15:59:22.852: E/AndroidRuntime(1964): FATAL EXCEPTION: main
02-01 15:59:22.852: E/AndroidRuntime(1964): java.lang.NullPointerException
02-01 15:59:22.852: E/AndroidRuntime(1964): at com.demo.busbooking.SearchedRouteAdapter.getView(SearchedRouteAdapter.java:107)
02-01 15:59:22.852: E/AndroidRuntime(1964): at android.widget.AbsListView.obtainView(AbsListView.java:1430)
02-01 15:59:22.852: E/AndroidRuntime(1964): at android.widget.ListView.makeAndAddView(ListView.java:1745)
02-01 15:59:22.852: E/AndroidRuntime(1964): at android.widget.ListView.fillDown(ListView.java:670)
02-01 15:59:22.852: E/AndroidRuntime(1964): at android.widget.ListView.fillFromTop(ListView.java:727)
02-01 15:59:22.852: E/AndroidRuntime(1964): at android.widget.ListView.layoutChildren(ListView.java:1598)
02-01 15:59:22.852: E/AndroidRuntime(1964): at android.widget.AbsListView.onLayout(AbsListView.java:1260)
02-01 15:59:22.852: E/AndroidRuntime(1964): at android.view.View.layout(View.java:7175)
02-01 15:59:22.852: E/AndroidRuntime(1964): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
02-01 15:59:22.852: E/AndroidRuntime(1964): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
02-01 15:59:22.852: E/AndroidRuntime(1964): at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
02-01 15:59:22.852: E/AndroidRuntime(1964): at android.view.View.layout(View.java:7175)
02-01 15:59:22.852: E/AndroidRuntime(1964): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
02-01 15:59:22.852: E/AndroidRuntime(1964): at android.view.View.layout(View.java:7175)
02-01 15:59:22.852: E/AndroidRuntime(1964): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
02-01 15:59:22.852: E/AndroidRuntime(1964): at android.view.View.layout(View.java:7175)
02-01 15:59:22.852: E/AndroidRuntime(1964): at android.view.ViewRoot.performTraversals(ViewRoot.java:1140)
02-01 15:59:22.852: E/AndroidRuntime(1964): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
02-01 15:59:22.852: E/AndroidRuntime(1964): at android.os.Handler.dispatchMessage(Handler.java:99)
02-01 15:59:22.852: E/AndroidRuntime(1964): at android.os.Looper.loop(Looper.java:123)
02-01 15:59:22.852: E/AndroidRuntime(1964): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-01 15:59:22.852: E/AndroidRuntime(1964): at java.lang.reflect.Method.invokeNative(Native Method)
02-01 15:59:22.852: E/AndroidRuntime(1964): at java.lang.reflect.Method.invoke(Method.java:507)
02-01 15:59:22.852: E/AndroidRuntime(1964): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-01 15:59:22.852: E/AndroidRuntime(1964): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-01 15:59:22.852: E/AndroidRuntime(1964): at dalvik.system.NativeStart.main(Native Method)
3 ответа
Я решил проблему, которая была в обработчике xml, а не в этом коде, но спасибо за ваши усилия!
ArrayAdapter
Конструктор не возвращает просмотров. Вы должны реализовать getView()
вернуть View
быть помещенным в определенную строку в ListView
,
Вы должны переопределить getView()
метод ArrayAdapter
и поместите этот код внутри.
public View getView(){
if (rowView == null) {
// Get a new instance of the row layout view
LayoutInflater inflater = activity.getLayoutInflater();
rowView = inflater.inflate(R.layout.route_list_item, null);
Log.v("Log position", "2");
// Hold the view objects in an object,
// so they don't need to be re-fetched
hsView = new RouteSearchView();
hsView.routeSVname = (TextView) rowView
.findViewById(R.id.routeName);
hsView.routeSVtravelsName = (TextView) rowView
.findViewById(R.id.travelsName);
hsView.routeSVbusType = (TextView) rowView
.findViewById(R.id.busType);
hsView.routeSVdepTime = (TextView) rowView
.findViewById(R.id.depTime);
hsView.routeSVarrTime = (TextView) rowView
.findViewById(R.id.arrTime);
hsView.routeSVseatsAvail = (TextView) rowView
.findViewById(R.id.seatsAvail);
hsView.routeSVsellFare = (TextView) rowView
.findViewById(R.id.sellFare);
hsView.routeSVcmdCheck = (Button) rowView
.findViewById(R.id.cmdCheck);
// Cache the view objects in the tag,
// so they can be re-accessed later
Log.v("Log position", "3");
rowView.setTag(hsView);
} else {
hsView = (RouteSearchView) rowView.getTag();
}
// Transfer the stock data from the data object
// to the view objects
searchBus = listBusGS.get(position);
routeID = searchBus.getRoute_id().toString();
routeName = searchBus.getService_name().toString();
travelsName = searchBus.getTravels_name().toString();
busType = searchBus.getBus_type().toString();
depTime = searchBus.getDeparture_time().toString();
arrTime = searchBus.getArival_time().toString();
seatsAvail = searchBus.getSeats_available().toString();
sellFare = searchBus.getSell_fare().toString();
hsView.routeSVname.setText(routeName);
hsView.routeSVtravelsName.setText(travelsName);
hsView.routeSVbusType.setText(busType);
hsView.routeSVdepTime.setText(depTime);
hsView.routeSVarrTime.setText(arrTime);
hsView.routeSVseatsAvail.setText(seatsAvail);
hsView.routeSVsellFare.setText(sellFare);
}
return rowView;
}
Что-то в этом роде.