我正在创建应用程序,其中我在自定义 ListView 中显示数据......数据来自 XMLHandler。但是,当我滚动列表时,它给出了空指针错误.....
有六个项目来自 xmlHandler 一次只有 3 个项目显示在屏幕上....但是,当我滚动 getView() 时应该调用剩余的 3 个项目......但是,它要求还有一项......因此它给出了空指针......我不知道该怎么做......请帮助我......谢谢你的建议。
*抱歉,我忘记在我的问题中添加 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)