我正在使用android studio,但这是命令,但是这个错误
错误:(27,12)错误:非静态方法 无法引用setOnItemSelectedListener(OnItemSelectedListener) 来自静态背景
我不知道如何解决此错误
package com.example.toshiba.mystudy;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
public class FirstActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter adapter = ArrayAdapter.createFromResource(this,R.array.semester_array,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
//spinner.setOnItemSelectedListener(this);
Spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
parent.getItemAtPosition(position);
Intent intent = null;
switch(position) {
case 1:
intent = new Intent(FirstActivity.this, SecondActivity.class);
break;
//case2:
// intent = new Intent(FirstActivity.this, TargetActivity2.class);
// break;// }
}
startActivity(intent);
}
public void onNothingSelected(AdapterView<?> parent) {
}
);
}
}
答案 0 :(得分:2)
喜欢
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
将setOnItemSelectedListener
分配给spinner(object of Spinner)
。
不是 Spinner(直接查看)
答案 1 :(得分:0)
使用此代码将setOnItemSelectedListener方法分配给微调器。
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
parent.getItemAtPosition(position);
Intent intent = null;
switch(position){
case 1:
intent = new Intent(FirstActivity.this, SecondActivity.class);
break;
//case2:
// intent = new Intent(FirstActivity.this, TargetActivity2.class);
// break;// }
} startActivity(intent);
}
public void onNothingSelected(AdapterView<?> parent){
}
});