我的问题:我有一个带有OnItemClickListener的AutoCompleteTextView。这已经工作了18个月了,但我现在已经注意到当我在HTC Desire S上以横向模式选择项目时它会抛出NullPointerException。(纵向模式或任何其他手机或模拟器上没有错误我已经测试了它。)
AdapterView<?> av
参数为null。为什么会这样,我怎么能绕过它呢?
代码:
myAutoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.myAutoCompleteTextView);
myAutoCompleteTextView.setSingleLine();
myAutoCompleteTextView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> av, View v, int index, long arg) {
String selectedItem = (String)av.getItemAtPosition(index);
//Do stuff with selected item ...
}
}
错误:
java.lang.NullPointerException
at uk.co.myCompany.mobile.android.myCompanymobile.pages.groups.AbstractGroupSelectionPage$3.onItemClick(AbstractGroupSelectionPage.java:228)
at android.widget.AutoCompleteTextView.onCommitCompletion(AutoCompleteTextView.java:993)
at com.android.internal.widget.EditableInputConnection.commitCompletion(EditableInputConnection.java:76)
at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:368)
at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:86)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:4385)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
at dalvik.system.NativeStart.main(Native Method)
额外代码 - 我的自定义适配器内部类:
/**
* An inner class to simply make a custom adapter in which we can alter the on-screen look of selected groups.
*/
private class SelectedGroupAdapter extends ArrayAdapter<Group> {
private ArrayList<Group> items;
private int layout;
public SelectedGroupAdapter(Context context, int layout, ArrayList<Group> items) {
super(context, layout, items);
this.items = items;
this.layout = layout;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(layout, null);
}
Group o = items.get(position);
//Display the group name and number of contacts
if (o != null) {
TextView groupName = (TextView) v.findViewById(R.id.groupName);
TextView noOfContacts = (TextView) v.findViewById(R.id.noOfContacts);
if (groupName != null) {
groupName.setText(o.getGroupName());
}
if(noOfContacts != null) {
if (o.isDynamic())
noOfContacts.setText(getString(R.string.dynamic));
else {
int contactsCount = o.getGroupSize();
if(contactsCount == 1) noOfContacts.setText(contactsCount + " " + getString(R.string.contact));
else noOfContacts.setText(contactsCount + " " + getString(R.string.contacts));
}
}
}
return v;
}
}
答案 0 :(得分:0)
我的预感是,由于您在清单中声明了android:configChanges="orientation"
,因此当您旋转旧版OnItemClickListener
时仍然存在,并且由于您在技术上有新的布局,{{1}在方向更改之前使用的元素不再存在,因此当您单击某个项目时它为null。
如果是这种情况,我认为有两件事可以解决这个问题:
删除清单中的AdapterView
选项。您放在orientation
中的任何事件都告诉Android“我正在处理此配置更改,所以让我来处理它”,而不是让Android处理它。如果方向发生变化,Android的正常操作是销毁并重新创建configChanges
(它会自动重新填充某些Activity
个数据。
如果您决定需要处理方向更改,请覆盖View
并将onConfigurationChanged()
设置为新的OnItemClickListener
对象(AdapterView
,{{1应该在ListView
方法中重新创建的,无论你使用哪个。