我有一个使用 SimpleAdapter 的 ListView 活动。这是 onCreate:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contact_selection);
Intent intent = getIntent();
mContactsList = (ListView) findViewById(R.id.contacts_list);
mContactAdapter = new SimpleAdapter(this, getEmailContacts(), R.layout.contact_list_item,
new String[] { CONTACT_FIELDS.NAME.toString(), CONTACT_FIELDS.PHOTOURI.toString(),
CONTACT_FIELDS.SECONDARY_DATA.toString(), CONTACT_FIELDS.TYPE.toString() },
new int[] { R.id.contact_name, R.id.contact_image, R.id.contact_type_name,
R.id.contact_type_name });
mContactsList.setAdapter(mContactAdapter);
}
这很好用;列表显示并正确填写。让我强调一下——问题不在于我的 getEmailContacts() 方法为空,因为此时列表已正确呈现。
我正在尝试添加过滤,所以我在 mContactsList.setAdapter 之后添加了这一行来尝试一下:
mContactAdapter.getFilter().filter("Ca");
这会导致崩溃,并带有以下堆栈跟踪:
FATAL EXCEPTION: main
java.lang.NullPointerException
at android.widget.SimpleAdapter.getCount(SimpleAdapter.java:93)
at android.widget.AdapterView.checkFocus(AdapterView.java:712)
at android.widget.AdapterView$AdapterDataSetObserver.onInvalidated(AdapterView.java:835)
at android.widget.AbsListView$AdapterDataSetObserver.onInvalidated(AbsListView.java:5966)
at android.database.DataSetObservable.notifyInvalidated(DataSetObservable.java:50)
at android.widget.BaseAdapter.notifyDataSetInvalidated(BaseAdapter.java:59)
at android.widget.SimpleAdapter$SimpleFilter.publishResults(SimpleAdapter.java:383)
at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:282)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
有什么建议么?我错过了一些非常明显的东西吗?