roh*_*rma 7 android onitemselectedlistener
我有以下代码:
public class OnboardingActivity extends BaseLoggedInActivity
implements CountryPickerDialog.ICountryPickerDialogUsers, AdapterView.OnItemSelectedListener {
private Spinner _countryCodeSpinner;
.
.
.
private void setupCountrySpinner() {
List<String> sortedCountryCodeList = CountryData.getInstance().getSortedCountryCodes();
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
R.layout.country_code_spinner_item,
sortedCountryCodeList);
_countryCodeSpinner.setOnItemSelectedListener(this);
_countryCodeSpinner.setAdapter(adapter);
_countryCodeSpinner
.setOnTouchListener(getCountryCodeSpinnerTouchListener(_countryCodeSpinner));
int position = getDefaultCountryNamePosition();
if (position >= 0) {
_countryCodeSpinner.setSelection(position);
}
}
.
.
.
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
_logger.debug("Inside onItemSelected");
view.setSelected(true);
}
Run Code Online (Sandbox Code Playgroud)
我在上面的函数onItemSelected 中得到一个空指针异常。它返回 NULL 视图。我从一位用户那里收到了这条痕迹,但我自己无法重现。使用 NULL 视图调用 onItemSelected 的原因可能是什么?
谢谢
这可能是在配置更改后引起的,例如旋转设备。您的微调器已重新创建,并且您在 onItemSelected 回调中收到空参数。
您可以在实现中将视图注释为@nullable,然后
if (view != null) {view.setSelected(true);}
Run Code Online (Sandbox Code Playgroud)
如果您使用 Kotlin,请尝试以下操作:
override fun onItemSelected(parent: AdapterView<*>, view: View?, position: Int, id: Long){
view?.isSelected = true
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1343 次 |
| 最近记录: |