rohit sharma
rohit sharma

Reputation: 191

AdapterView.OnItemSelectedListener is returning NULL view

I have the following code :

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

I am getting a null pointer exception in the above function onItemSelected . Its returning NULL view. This trace i am receiving from one of the user but i am unable to reproduce it myself. What could be reason that onItemSelected is being called with a NULL view ?

Thanks

Upvotes: 9

Views: 2000

Answers (4)

Ivan
Ivan

Reputation: 503

Try to call spinner.setAdapter(adapter); with delay after view created.

Solution

Upvotes: 1

ErfanDP
ErfanDP

Reputation: 181

pretty late Answer I still don't know how it's happennig but you need to change view :View in

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    _logger.debug("Inside onItemSelected");
    view.setSelected(true);
}

with view:View? it can be null when the view isn't avalable

Upvotes: 4

林果皞
林果皞

Reputation: 7813

You can block default selection which possible null view by rotation, then perform setSelection() explicitly:

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

Note that simply catch null will causes issue if you change UI inside onItemSelected() because layout already refresh.

Upvotes: 0

Pitos
Pitos

Reputation: 867

It may be caused after the configuration change, e.g rotating the device. Your spinner is recreated and you are receiving a null parameter in the onItemSelected callback.

You can annotate the view in your implementation to be @nullable and then

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

If you are using Kotlin try this:

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

Upvotes: 5

Related Questions