我的测试应用程序包含一个ListActivity,用于填充Contacts中的内容。 LogCat的错误没有解释异常的来源,因为没有涉及项目文件。
如果在ADV或设备上运行(测试了两部不同的手机),它会意外停止。
重要说明:当用户滚动列表以查看更多项目时,会发生错误:当显示两到三个项目时。 (始终在列表中的同一点崩溃,并且两者:用手指或轨迹球滚动)。
进一步调查的最佳方式是什么?
10-13 13:21:00.662: E/AndroidRuntime(8031): FATAL EXCEPTION: main
10-13 13:21:00.662: E/AndroidRuntime(8031): java.lang.NullPointerException
10-13 13:21:00.662: E/AndroidRuntime(8031): at android.widget.AbsListView.obtainView(AbsListView.java:1304)
10-13 13:21:00.662: E/AndroidRuntime(8031): at android.widget.ListView.makeAndAddView(ListView.java:1727)
10-13 13:21:00.662: E/AndroidRuntime(8031): at android.widget.ListView.fillDown(ListView.java:652)
10-13 13:21:00.662: E/AndroidRuntime(8031): at android.widget.ListView.fillGap(ListView.java:623)
10-13 13:21:00.662: E/AndroidRuntime(8031): at android.widget.AbsListView.trackMotionScroll(AbsListView.java:2944)
10-13 13:21:00.662: E/AndroidRuntime(8031): at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:2485)
10-13 13:21:00.662: E/AndroidRuntime(8031): at android.os.Handler.handleCallback(Handler.java:587)
10-13 13:21:00.662: E/AndroidRuntime(8031): at android.os.Handler.dispatchMessage(Handler.java:92)
10-13 13:21:00.662: E/AndroidRuntime(8031): at android.os.Looper.loop(Looper.java:123)
10-13 13:21:00.662: E/AndroidRuntime(8031): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-13 13:21:00.662: E/AndroidRuntime(8031): at java.lang.reflect.Method.invokeNative(Native Method)
10-13 13:21:00.662: E/AndroidRuntime(8031): at java.lang.reflect.Method.invoke(Method.java:521)
10-13 13:21:00.662: E/AndroidRuntime(8031): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
10-13 13:21:00.662: E/AndroidRuntime(8031): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
10-13 13:21:00.662: E/AndroidRuntime(8031): at dalvik.system.NativeStart.main(Native Method)
更新:适配器的代码:
package com.stripedbee.warayu;
import java.util.List;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class ContactAdapter extends ArrayAdapter<Contact> {
private final List<Contact> _contacts;
private final Activity _context;
public ContactAdapter(Activity context, List<Contact> contacts) {
super(context, R.layout.contact_list_item, contacts);
this._contacts = contacts;
this._context = context;
}
static class ViewHolder {
protected TextView display_name;
protected TextView number;
protected TextView number_type;
private Contact contact;
protected void setContact(Contact contact) {
this.display_name.setText(contact.get_display_name());
this.number.setText(contact.get_number());
this.number_type.setText(contact.get_number_type());
this.contact = contact;
}
protected Contact getContact() {
return contact;
}
}
@Override
public Contact getItem(int position) {
return this._contacts.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflater = this._context.getLayoutInflater();
view = inflater.inflate(R.layout.contact_list_item, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.display_name = (TextView) view
.findViewById(R.id.txtDisplayName);
viewHolder.number = (TextView) view.findViewById(R.id.txtNumber);
viewHolder.number_type = (TextView) view
.findViewById(R.id.txtNumberType);
viewHolder.setContact(_contacts.get(position));
view.setTag(viewHolder);
}
return view;
}
}
答案 0 :(得分:2)
当null
不是getView
时(例如,当用户开始滚动时),您会从convertView
方法返回null
视图。您的getView
方法应该是这样的:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = this._context.getLayoutInflater();
view = inflater.inflate(R.layout.contact_list_item, parent, false);
holder = new ViewHolder();
viewHolder.display_name = (TextView) view
.findViewById(R.id.txtDisplayName);
viewHolder.number = (TextView) view.findViewById(R.id.txtNumber);
viewHolder.number_type = (TextView) view
.findViewById(R.id.txtNumberType);
view.setTag(viewHolder);
} else {
view = convertView;
holder = (ViewHolder) view.getTag();
}
holder.setContact(_contacts.get(position));
return view;
}