使用 AutocompleteTextView 和 BaseAdapter 时出现空指针异常
Null Pointer Exception in working with AutocompleteTextView and BaseAdapter
我有一个 AutocompleteTextView
,我为此编写了以下适配器。我想从 Web API 获取数据,这工作正常,我可以从服务器获取数据。当我在 autocompleteTextView
上写文本时,下面的代码显示了以下错误。我调试我的代码,这个错误在调用 notifyDataSetChanged
和调用 getViewMethod
后显示。我不知道我的问题是什么。
另一件事,我从服务器得到一个对象列表(我的意思是名字、家庭、id)我只想显示其中的一部分(我的意思是我想显示名字)为此,我创建了一个自定义适配器。
我的适配器:
public class DropDownAdapter extends BaseAdapter implements Filterable {
private static final int MAX_RESULTS = 10;
private Context mContext;
private List<TestModel> resultList = new ArrayList<TestModel>();
private boolean placeResults = false;
private Object lockTwo = new Object();
private Object lock = new Object();
public DropDownAdapter(Context context) {
mContext = context;
}
@Override
public int getCount() {
return resultList.size();
}
@Override
public Object getItem(int position) {
return resultList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
View v = view;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.one_row_dropdown, viewGroup, false);
}
TextView textView = v.findViewById(R.id.textview);
textView.setText(resultList.get(position).code);
return view;
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
final FilterResults filterResults = new FilterResults();
if (constraint == null || constraint.length() == 0) {
synchronized (lock) {
filterResults.values = new ArrayList<String>();
filterResults.count = 0;
}
} else {
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("token", OfflineData.getGUID(mContext));
jsonObject.put("code", constraint.toString());
jsonObject.put("state", false);
} catch (JSONException e) {
e.printStackTrace();
}
Type type = new TypeToken<ServiceResult<List<TestModel>>>() {
}.getType();
WebService getBranchCodeListWebService = new WebService(AppConstant.URL_LISTINGDATA, "TestMethod", type);
getBranchCodeListWebService.addOnCompletedCallBack(new WebService.OnCompletedCallBack() {
@Override
public void onCompletedCallBack(boolean isSucceed, Object returnedObject) {
if (isSucceed) {
if (returnedObject != null) {
ServiceResult<List<TestModel>> response = (ServiceResult<List<TestModel>>) returnedObject;
if (response.IsSuccess) {
if (response.Result.size() > 0) {
resultList.clear();
resultList = response.Result;
filterResults.values = resultList;
filterResults.count = resultList.size();
}
}
}
}
placeResults = true;
synchronized (lockTwo) {
lockTwo.notifyAll();
}
}
});
getBranchCodeListWebService.execute(jsonObject);
while (!placeResults) {
synchronized (lockTwo) {
try {
lockTwo.wait();
} catch (InterruptedException ex) {
}
}
}
}
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results != null && results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
};
return filter;
}
错误结果:
Attempt to invoke virtual method 'int android.view.View.getImportantForAccessibility()' on a null object reference
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getImportantForAccessibility()' on a null object reference
at android.widget.AbsListView.obtainView(AbsListView.java:2382)
at android.widget.DropDownListView.obtainView(DropDownListView.java:305)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1408)
at android.widget.ListPopupWindow.buildDropDown(ListPopupWindow.java:1257)
at android.widget.ListPopupWindow.show(ListPopupWindow.java:613)
at android.widget.AutoCompleteTextView.showDropDown(AutoCompleteTextView.java:1217)
at android.widget.AutoCompleteTextView.updateDropDownForFilter(AutoCompleteTextView.java:1086)
at android.widget.AutoCompleteTextView.onFilterComplete(AutoCompleteTextView.java:1068)
at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:285)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
在 MainActivity 中创建适配器:
autoCompleteTextView = findViewById(R.id.branchCodeEdittextAutocomplete);
autoCompleteTextView.setThreshold(2);
DropDownAdapter dropDownAdapter=new DropDownAdapter(this);
autoCompleteTextView.setAdapter(dropDownAdapter);
autoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//go to next page... or anything like this
}
});
您没有返回正确的视图,请将您的 return view
更改为 return v
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
View v = view;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.one_row_dropdown, viewGroup, false);
}
TextView textView = v.findViewById(R.id.textview);
textView.setText(resultList.get(position).code);
return v;
}
并且您不必每次都可以使用 view
分配视图,它将在适配器中保存您的视图的引用,因此您可以删除行 View v = view;
并替换您的代码为
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
if (view == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.one_row_dropdown, viewGroup, false);
}
TextView textView = v.findViewById(R.id.textview);
textView.setText(resultList.get(position).code);
return view;
}
希望这对您有所帮助...
我有一个 AutocompleteTextView
,我为此编写了以下适配器。我想从 Web API 获取数据,这工作正常,我可以从服务器获取数据。当我在 autocompleteTextView
上写文本时,下面的代码显示了以下错误。我调试我的代码,这个错误在调用 notifyDataSetChanged
和调用 getViewMethod
后显示。我不知道我的问题是什么。
另一件事,我从服务器得到一个对象列表(我的意思是名字、家庭、id)我只想显示其中的一部分(我的意思是我想显示名字)为此,我创建了一个自定义适配器。
我的适配器:
public class DropDownAdapter extends BaseAdapter implements Filterable {
private static final int MAX_RESULTS = 10;
private Context mContext;
private List<TestModel> resultList = new ArrayList<TestModel>();
private boolean placeResults = false;
private Object lockTwo = new Object();
private Object lock = new Object();
public DropDownAdapter(Context context) {
mContext = context;
}
@Override
public int getCount() {
return resultList.size();
}
@Override
public Object getItem(int position) {
return resultList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
View v = view;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.one_row_dropdown, viewGroup, false);
}
TextView textView = v.findViewById(R.id.textview);
textView.setText(resultList.get(position).code);
return view;
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
final FilterResults filterResults = new FilterResults();
if (constraint == null || constraint.length() == 0) {
synchronized (lock) {
filterResults.values = new ArrayList<String>();
filterResults.count = 0;
}
} else {
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("token", OfflineData.getGUID(mContext));
jsonObject.put("code", constraint.toString());
jsonObject.put("state", false);
} catch (JSONException e) {
e.printStackTrace();
}
Type type = new TypeToken<ServiceResult<List<TestModel>>>() {
}.getType();
WebService getBranchCodeListWebService = new WebService(AppConstant.URL_LISTINGDATA, "TestMethod", type);
getBranchCodeListWebService.addOnCompletedCallBack(new WebService.OnCompletedCallBack() {
@Override
public void onCompletedCallBack(boolean isSucceed, Object returnedObject) {
if (isSucceed) {
if (returnedObject != null) {
ServiceResult<List<TestModel>> response = (ServiceResult<List<TestModel>>) returnedObject;
if (response.IsSuccess) {
if (response.Result.size() > 0) {
resultList.clear();
resultList = response.Result;
filterResults.values = resultList;
filterResults.count = resultList.size();
}
}
}
}
placeResults = true;
synchronized (lockTwo) {
lockTwo.notifyAll();
}
}
});
getBranchCodeListWebService.execute(jsonObject);
while (!placeResults) {
synchronized (lockTwo) {
try {
lockTwo.wait();
} catch (InterruptedException ex) {
}
}
}
}
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results != null && results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
};
return filter;
}
错误结果:
Attempt to invoke virtual method 'int android.view.View.getImportantForAccessibility()' on a null object reference java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getImportantForAccessibility()' on a null object reference at android.widget.AbsListView.obtainView(AbsListView.java:2382) at android.widget.DropDownListView.obtainView(DropDownListView.java:305) at android.widget.ListView.measureHeightOfChildren(ListView.java:1408) at android.widget.ListPopupWindow.buildDropDown(ListPopupWindow.java:1257) at android.widget.ListPopupWindow.show(ListPopupWindow.java:613) at android.widget.AutoCompleteTextView.showDropDown(AutoCompleteTextView.java:1217) at android.widget.AutoCompleteTextView.updateDropDownForFilter(AutoCompleteTextView.java:1086) at android.widget.AutoCompleteTextView.onFilterComplete(AutoCompleteTextView.java:1068) at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:285) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
在 MainActivity 中创建适配器:
autoCompleteTextView = findViewById(R.id.branchCodeEdittextAutocomplete); autoCompleteTextView.setThreshold(2);
DropDownAdapter dropDownAdapter=new DropDownAdapter(this);
autoCompleteTextView.setAdapter(dropDownAdapter);
autoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//go to next page... or anything like this
}
});
您没有返回正确的视图,请将您的 return view
更改为 return v
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
View v = view;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.one_row_dropdown, viewGroup, false);
}
TextView textView = v.findViewById(R.id.textview);
textView.setText(resultList.get(position).code);
return v;
}
并且您不必每次都可以使用 view
分配视图,它将在适配器中保存您的视图的引用,因此您可以删除行 View v = view;
并替换您的代码为
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
if (view == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.one_row_dropdown, viewGroup, false);
}
TextView textView = v.findViewById(R.id.textview);
textView.setText(resultList.get(position).code);
return view;
}
希望这对您有所帮助...