Spinner显示此错误:android.view.WindowManager $ BadTokenException:无法添加窗口 - 令牌null无效;你的活动在运行吗?

时间:2018-04-01 13:30:10

标签: android android-layout android-spinner

我希望微调器在我运行活动时自行打开,而用户不必在它下降之前点击它,所以我使用performClick()方法,但是它显示了这个错误:

引起:android.view.WindowManager $ BadTokenException:无法添加窗口 - 令牌null无效;你的活动在运行吗?

我该怎么办?这是我的代码

public class FacilityComplaint extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
private Spinner spinner1;

private static final String[] suggestions = {"Select from suggestions", "Switch not working",
        "Switch faulty", "Switch light not working", "Switch handle faulty"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_facility_complaint);

    spinner1 = (Spinner) findViewById(R.id.spinner1);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(FacilityComplaint.this,
            android.R.layout.simple_spinner_item, suggestions);

    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner1.setAdapter(adapter);
    spinner1.setOnItemSelectedListener(this);
    spinner1.performClick();
}


public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {

    switch (position) {
        case 0:
            //do something
            break;

        case 1:
            //do something
            break;

        case 2:
            //do something
            break;

        case 3:
            //do something
            break;

        case 4:
            //do something
            break;
    }
}

public void onNothingSelected(AdapterView<?> parent) {

}

}

0 个答案:

没有答案