【问题标题】:AdapterView.OnItemSelectedListener is returning NULL viewAdapterView.OnItemSelectedListener 返回 NULL 视图
【发布时间】:2015-01-07 13:49:42
【问题描述】:

我有以下代码:

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);
}

我在上述函数 onItemSelected 中遇到空指针异常。它返回 NULL 视图。我从其中一位用户那里收到了这条跟踪信息,但我自己无法重现它。使用 NULL 视图调用 onItemSelected 的原因可能是什么?

谢谢

【问题讨论】:

  • 这是因为_countryCodeSpinner.setSelection(position); ... listpopupwindow 不可见,所以没有视图... 您能提供此设备上的 android 版本吗? (也许在 API 11 之前微调器 listpopupwindow 更像是一个对话框)
  • 安卓系统版本为4.0.4
  • 从后栈获取片段时遇到同样的问题。我会努力的。
  • API24 (Android7) 中的同样问题

标签: android onitemselectedlistener


【解决方案1】:

这可能是在配置更改后引起的,例如旋转设备。您的微调器已重新创建,并且您在 onItemSelected 回调中收到一个 null 参数。

您可以在实现中将视图注释为@nullable,然后

if (view != null) {view.setSelected(true);}

如果你使用 Kotlin,试试这个:

override fun onItemSelected(parent: AdapterView<*>, view: View?, position: Int, id: Long){
    view?.isSelected = true
}

【讨论】:

    【解决方案2】:

    很晚回答我仍然不知道它是怎么发生的,但你需要更改view :View in

    @Override public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position, long id) { _logger.debug("Inside onItemSelected"); view.setSelected(true); } whit view:View? 当视图不可用时它可以为空

    【讨论】:

      【解决方案3】:

      您可以通过旋转阻止默认选择可能的空视图,然后显式执行setSelection()

      int position = getDefaultCountryNamePosition();
      if (position < 0) {
          position = 0;
      }
      // Block default selection to custom onItemSelected() from listener initialization
      //, see https://stackoverflow.com/a/37561529/1074998
      // + 1 as fake value because onItemSelected() only triggered by setSelection() if different value. 
      _countryCodeSpinner.setSelection(position + 1, false); 
      _countryCodeSpinner.setOnItemSelectedListener(this);
      
      // position(without + 1) is our real target.
      // set selection explicitly should not null now
      _countryCodeSpinner.setSelection(position, false); 
      

      请注意,如果您在 onItemSelected() 中更改 UI,则简单地捕获 null 会导致问题,因为布局已经刷新。

      【讨论】:

        猜你喜欢
        • 2012-09-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多