在Android中获取微调的位置

我正在尝试获取微调器的位置(数字),以便在另一个活动中使用该活动,每次根据所选项目显示不同的地图。 当我运行应用程序崩溃。 这是第一个活动代码:

public class TestProjectActivity extends Activity { public Spinner spinner1; public Integer number; private Button valideButton; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); MySpinner(); valide_button(); } public void MySpinner() { final Spinner spinner1 = (Spinner) findViewById(R.id.spinner1); ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( this, R.array.num, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner1.setAdapter(adapter); spinner1.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) { // Object item = parentView.getItemAtPosition(position); TestProjectActivity.this.number = spinner1 .getSelectedItemPosition() + 1; } public void onNothingSelected(AdapterView<?> arg0) {// do nothing } }); } public void valide_button() { valideButton = (Button) findViewById(R.id.valide_button); valideButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent intent = new Intent(TestProjectActivity.this, MetroMapActivity.class); startActivity(intent); } }); } } 

Solutions Collecting From Web of "在Android中获取微调的位置"

获得微调select的方法是:

  spinner1.getSelectedItemPosition(); 

文档参考: http : //developer.android.com/reference/android/widget/AdapterView.html#getSelectedItemPosition()

但是,在你的代码中,你引用它的地方是你的setOnItemSelectedListener() 。 没有必要轮询微调,因为onItemSelected方法获取作为“位置”variables的位置。

所以你可以改变这一行:

 TestProjectActivity.this.number = position + 1; 

如果这不能解决问题,请发布应用程序崩溃时生成的错误消息。

  final int[] positions=new int[2]; Spinner sp=findViewByID(R.id.spinner); sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub Toast.makeText( arg2....); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); 
 if (position ==0) { if (rYes.isChecked()) { Toast.makeText(SportActivity.this, "yes ur answer is right", Toast.LENGTH_LONG).show(); } else if (rNo.isChecked()) { Toast.makeText(SportActivity.this, "no.ur answer is wrong", Toast.LENGTH_LONG).show(); } } 

这个代码应该select这两个checkbox。
有问题吗?