Spinner's onItemSelected callback called twice after a rotation if non-zero position is selected

后端 未结 8 1042
执念已碎
执念已碎 2021-02-03 20:52

When I create my activity, I setup a Spinner, assigning it a listener and an initial value. I know that the onItemSelected callback is called automatically during a

8条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-03 21:25

    The first time the onItemSelected runs, the view is not yet inflated. The second time it is already inflated. The solution is to wrap methods inside onItemSelected with if (view != null).

    @Override
    public void onItemSelected(AdapterView parent, View view, int pos, long id) {
        if (view != null) { 
            //do things here
    
        }
    }
    

提交回复
热议问题