android - Spinner.setSelected(false) 和 Spinner.setSelection(0,true) 正在努力解决除手机外的空点异常:三星 sm-G31
问题描述
在我的 android 应用程序中,我遇到了以下错误
尝试在空对象引用 onItemSelected 上调用虚拟方法“java.lang.String java.lang.Object.toString()”
因此我使用location.setSelected(false)
andlocation.setSelection(0,true)
来避免空字符串。我的代码在除三星 sm-g31h 之外的所有手机上都能完美运行,它显示相同的错误。需要一个可能的解决方案。我的代码如下
MainActivity.java
location = (Spinner) findViewById(R.id.location);
ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(this, R.array.location, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
location.setPrompt("Select Location");
location.setAdapter(
new NothingSelectedSpinnerAdapter(
adapter1, R.layout.location_spinner_row_nothing_selected,
// R.layout.contact_spinner_nothing_selected_dropdown, // Optional
this));
// location.setSelected(false); // must
// location.setSelection(0,true); //must
location.setOnItemSelectedListener(this);
location.setVisibility(View.VISIBLE);
location.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Log.w("location----", "> " + location.getSelectedItem().toString());
location_code = location.getSelectedItem().toString();
loc_code = location_code.substring(0, 1).toString();
Log.e("loccode", "> " + loc_code);
final String check =String.valueOf(loc_code);
Log.e("checkloccode", "> " + check);
if(loc_code.equals("S")){
Log.e("loccodeif", "> " + check);
location.setFocusable(true);
location.setFocusableInTouchMode(true);
location.requestFocus();
}
else{
Log.e("loccodeelse", "> " + check);
shift_spinner.setFocusable(true);
shift_spinner.setFocusableInTouchMode(true);
shift_spinner.requestFocus();
}
}
public void onNothingSelected(AdapterView<?> adapterView) {
return;
}
});
NothingSelectedSpinnerAdapter
public class NothingSelectedSpinnerAdapter implements SpinnerAdapter, ListAdapter {
protected static final int EXTRA = 1;
protected SpinnerAdapter adapter;
protected Context context;
protected int nothingSelectedLayout;
protected int nothingSelectedDropdownLayout;
protected LayoutInflater layoutInflater;
/**
* Use this constructor to have NO 'Select One...' item, instead use
* the standard prompt or nothing at all.
* @param spinnerAdapter wrapped Adapter.
* @param nothingSelectedLayout layout for nothing selected, perhaps
* you want text grayed out like a prompt...
* @param context
*/
public NothingSelectedSpinnerAdapter(
SpinnerAdapter spinnerAdapter,
int nothingSelectedLayout, Context context) {
this(spinnerAdapter, nothingSelectedLayout, -1, context);
}
/**
* Use this constructor to Define your 'Select One...' layout as the first
* row in the returned choices.
* If you do this, you probably don't want a prompt on your spinner or it'll
* have two 'Select' rows.
* @param spinnerAdapter wrapped Adapter. Should probably return false for isEnabled(0)
* @param nothingSelectedLayout layout for nothing selected, perhaps you want
* text grayed out like a prompt...
* @param nothingSelectedDropdownLayout layout for your 'Select an Item...' in
* the dropdown.
* @param context
*/
public NothingSelectedSpinnerAdapter(SpinnerAdapter spinnerAdapter,
int nothingSelectedLayout, int nothingSelectedDropdownLayout, Context context) {
this.adapter = spinnerAdapter;
this.context = context;
this.nothingSelectedLayout = nothingSelectedLayout;
this.nothingSelectedDropdownLayout = nothingSelectedDropdownLayout;
layoutInflater = LayoutInflater.from(context);
}
@Override
public final View getView(int position, View convertView, ViewGroup parent) {
// This provides the View for the Selected Item in the Spinner, not
// the dropdown (unless dropdownView is not set).
if (position == 0) {
return getNothingSelectedView(parent);
}
return adapter.getView(position - EXTRA, null, parent); // Could re-use
// the convertView if possible.
}
/**
* View to show in Spinner with Nothing Selected
* Override this to do something dynamic... e.g. "37 Options Found"
* @param parent
* @return
*/
protected View getNothingSelectedView(ViewGroup parent) {
return layoutInflater.inflate(nothingSelectedLayout, parent, false);
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
// Android BUG! http://code.google.com/p/android/issues/detail?id=17128 -
// Spinner does not support multiple view types
if (position == 0) {
return nothingSelectedDropdownLayout == -1 ?
new View(context) :
getNothingSelectedDropdownView(parent);
}
// Could re-use the convertView if possible, use setTag...
return adapter.getDropDownView(position - EXTRA, null, parent);
}
/**
* Override this to do something dynamic... For example, "Pick your favorite
* of these 37".
* @param parent
* @return
*/
protected View getNothingSelectedDropdownView(ViewGroup parent) {
return layoutInflater.inflate(nothingSelectedDropdownLayout, parent, false);
}
@Override
public int getCount() {
int count = adapter.getCount();
return count == 0 ? 0 : count + EXTRA;
}
@Override
public Object getItem(int position) {
return position == 0 ? null : adapter.getItem(position - EXTRA);
}
@Override
public int getItemViewType(int position) {
return 0;
}
@Override
public int getViewTypeCount() {
return 1;
}
@Override
public long getItemId(int position) {
return position >= EXTRA ? adapter.getItemId(position - EXTRA) : position - EXTRA;
}
@Override
public boolean hasStableIds() {
return adapter.hasStableIds();
}
@Override
public boolean isEmpty() {
return adapter.isEmpty();
}
@Override
public void registerDataSetObserver(DataSetObserver observer) {
adapter.registerDataSetObserver(observer);
}
@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
adapter.unregisterDataSetObserver(observer);
}
@Override
public boolean areAllItemsEnabled() {
return false;
}
@Override
public boolean isEnabled(int position) {
return position != 0; // Don't allow the 'nothing selected'
// item to be picked.
}
}
解决方案
推荐阅读
- spring-boot - 部署在 glassfish 中时的 FileOutputStream NPE
- android - 如何在 Android 中将 RecyclerView 用于另一个 RecyclerView 的适配器?
- html - 如何居中对齐下拉列表
- powershell - 如何在 Excel 中循环以将 GPO 列表应用于 Powershell 中的 OU
- reactjs - 如何在componentDidMount之后初始化React Materialize下拉
- javascript - 在 openlayers 上添加标记会给出“未捕获的类型错误:无法读取未定义的属性 'addLayer'”
- java - 同一个实例的同步块和同步方法
- javascript - 转义对象的属性值动态引用
- kubernetes - Kubernetes仪表板安装给出x509:证书由未知权限错误签名
- python - 调整 Keras 中的隐藏层数