我正在创建一个Android微调器。它显示正确,但是选择一个选项没有任何反应。为什么“如果”命令不起作用?

public class Main2Activity extends AppCompatActivity {
Spinner spinner;
ArrayAdapter<CharSequence> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    spinner= (Spinner) findViewById(R.id.spinner);
    adapter=ArrayAdapter.createFromResource(this,R.array.spinner,android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {//why 'if' command is not working?
            if(adapterView.getItemAtPosition(i).toString().equals("Question 1"))
            {
                Intent intent=new Intent(Main2Activity.this,Main2Activity.class);
            }
            if(adapterView.getItemAtPosition(i).toString().equals("Question 2"))
            {
                Intent intent=new Intent(Main2Activity.this,main3Activity.class);
            }

        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });`

最佳答案

您不是故意启动活动的。做这个:

        startActivity(intent);


在意图声明和赋值之后,在if和else中都进行。

07-27 15:57