尝试优化列表视图但失败
我正在尝试采纳 Romain Guy 去年 google IO 中关于 ViewHolder 的建议,以优化列表视图,示例如下: http://www.softwarepassion.com/android-series-自定义列表视图项目和适配器/ (感谢管理员) 但是每当我尝试翻阅列表甚至稍微移动它时,应用程序就会崩溃,这是怎么回事?任何帮助将不胜感激:) 这就是我所做的>
private class OrderAdapter extends ArrayAdapter<Order> {
private ArrayList<Order> items;
LayoutInflater mInflater;
public OrderAdapter(Context context, int textViewResourceId, ArrayList<Order> items) {
super(context, textViewResourceId, items);
mInflater = LayoutInflater.from(context);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row, parent, false);
holder = new ViewHolder();
holder.tt = (TextView) convertView.findViewById(R.id.toptext);
holder.bt = (TextView) convertView.findViewById(R.id.bottomtext);
} else {
holder = (ViewHolder) convertView.getTag();
}
Order o = items.get(position);
if (o != null) {
if (holder.tt != null) {
holder.tt.setText("Name: " + o.getOrderName());
}
if (holder.bt != null) {
holder.bt.setText("Status: " + o.getOrderStatus());
}
}
return convertView;
}
}
这是我收到的错误消息,抱歉我不知道如何格式化它>
致命异常:主要 java.lang.NullPointerException 在 com.softberry.lve.SoftwarePassionView$OrderAdapter.getView(SoftwarePassionView.java:98) 在 android.widget.AbsListView.obtainView(AbsListView.java:1294) 在 android.widget.ListView.makeAndAddView(ListView.java:1727) 在 android.widget.ListView.fillDown(ListView.java:652) 在 android.widget.ListView.fillGap(ListView.java:623) 在 android.widget.AbsListView.trackMotionScroll(AbsListView.java:2944) 在 android.widget.AbsListView.onTouchEvent(AbsListView.java:2065) 在 android.widget.ListView.onTouchEvent(ListView.java:3315) 在 android.view.View.dispatchTouchEvent(View.java:3766) 在 android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:897) 在 android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936) 在 android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936) 在 android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936) 在 android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936) 在 com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1671) 在 com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107) 在 android.app.Activity.dispatchTouchEvent(Activity.java:2086) 在 com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1655) 在 android.view.ViewRoot.handleMessage(ViewRoot.java:1785) 在 android.os.Handler.dispatchMessage(Handler.java:99) 在 android.os.Looper.loop(Looper.java:123) 在 android.app.ActivityThread.main(ActivityThread.java:4627) 在 java.lang.reflect.Method.invokeNative(本机方法) 在 java.lang.reflect.Method.invoke(Method.java:521) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 在dalvik.system.NativeStart.main(本机方法)
I am trying to adopt Romain Guy's recommendation from google IO last year of a ViewHolder for a list to optimize list view for the example from:
http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/
(thanks to admin)
but whenever I try to flip through the list or even make it move a little bit, the application crashes, what is going on? any help would be appreciated :)
here is what I did >
private class OrderAdapter extends ArrayAdapter<Order> {
private ArrayList<Order> items;
LayoutInflater mInflater;
public OrderAdapter(Context context, int textViewResourceId, ArrayList<Order> items) {
super(context, textViewResourceId, items);
mInflater = LayoutInflater.from(context);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row, parent, false);
holder = new ViewHolder();
holder.tt = (TextView) convertView.findViewById(R.id.toptext);
holder.bt = (TextView) convertView.findViewById(R.id.bottomtext);
} else {
holder = (ViewHolder) convertView.getTag();
}
Order o = items.get(position);
if (o != null) {
if (holder.tt != null) {
holder.tt.setText("Name: " + o.getOrderName());
}
if (holder.bt != null) {
holder.bt.setText("Status: " + o.getOrderStatus());
}
}
return convertView;
}
}
This is the error message I got, sorry I don't know how to format it >
FATAL EXCEPTION: main
java.lang.NullPointerException
at com.softberries.lve.SoftwarePassionView$OrderAdapter.getView(SoftwarePassionView.java:98)
at android.widget.AbsListView.obtainView(AbsListView.java:1294)
at android.widget.ListView.makeAndAddView(ListView.java:1727)
at android.widget.ListView.fillDown(ListView.java:652)
at android.widget.ListView.fillGap(ListView.java:623)
at android.widget.AbsListView.trackMotionScroll(AbsListView.java:2944)
at android.widget.AbsListView.onTouchEvent(AbsListView.java:2065)
at android.widget.ListView.onTouchEvent(ListView.java:3315)
at android.view.View.dispatchTouchEvent(View.java:3766)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:897)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1671)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
at android.app.Activity.dispatchTouchEvent(Activity.java:2086)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1655)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1785)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我看到
holder = (ViewHolder) ConvertView.getTag();
。但我没有看到您在if (convertView == null) {...}
正文中为convertView
调用setTag()
。所以,我猜你忘记了这句话:
I see
holder = (ViewHolder) convertView.getTag();
. But I do not see where you invokesetTag()
forconvertView
in body ofif (convertView == null) {...}
.So, I guess you forgot the line:
您很可能会收到 ArrayIndexOutOfBound 异常。使用数组适配器的getCount()方法来声明列表的大小。
Most probably you are getting ArrayIndexOutOfBound Exception. Use getCount() method of array adapter to declare the size of the list.