Spinner问题,Android

时间:2014-03-31 16:12:32

标签: android eclipse spinner

我正在开发Android应用。我的UI的一部分有一个微调器。微调器列出了五个名字的名称。此外,在相同的布局上,有两个单选按钮。我想根据所选的选项创建一个新活动 - 一个来自地点列表和两个单选按钮。假设,从列表中选择了place1并选择了radiobutton1,它需要相应地打开一个新的意图。 我已将Adapter View合并到我的代码中,但应用程序在第二个屏幕后关闭。 LogCat(我使用Eclipse)指向与适配器相关的问题。这是代码。请建议其他替代方案或任何修改。提前致谢!

package com.example.travel;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
@SuppressWarnings("unused")
public class Mumbai extends Activity {
TextView tv;
//int result_code=1;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.three);
    tv=(TextView)findViewById(R.id.textView1);
    Toast.makeText(this, "third Screen.", Toast.LENGTH_SHORT).show();
    Button b1=(Button)findViewById(R.id.button1);
    Button b2=(Button)findViewById(R.id.button2);
    int for_spinner=0;
    int for_radio=0;
    b1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent i=new Intent(Mumbai.this,two.class);
            setResult(3,i);
            finish();


        }
    });


    RadioGroup radiogroup=(RadioGroup)findViewById(R.id.radioGroup2);
    int checkedRadioButton=radiogroup.getCheckedRadioButtonId();
    switch(checkedRadioButton) {
        case R.id.radio0 :  for_radio=1;
                            break;
        case R.id.radio1 :  for_radio=2;
                            break;

    }

    Spinner answer=(Spinner)findViewById(R.id.spinner1);
    answer.setOnItemSelectedListener(new OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> parent, View view, int   position, long id) {
            int value_spinner= (Integer) parent.getItemAtPosition(position);

            switch(position) {
            case 0: //place1
                    int for_spinner=1;
                    break;
            case 1: //place2
                    for_spinner=2;
                    break;
            case 2: //place3
                    for_spinner=3;
                    break;
            case 3: //place4
                    for_spinner=4;
                    break;
            case 4: //place5
                    for_spinner=5;
                    break;

            }

        } 

        public void onNothingSelected(AdapterView<?> parent) {

            int for_spinner=1;
        }

    });
    final int new_for_spinner=0;
    final int new_for_radio=0;
    for_spinner=new_for_spinner; 

    b2.setOnClickListener(new View.OnClickListener() {

        //@SuppressWarnings("unused")
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(new_for_radio==1 && new_for_spinner==1)
            {
                Intent i1= new Intent(Mumbai.this,food.class);
                startActivityForResult(i1,1);
                i1.putExtra("new_for_spinner","value");
            }
            if(new_for_radio==1 && new_for_spinner==2)
            {
                Intent i1= new Intent(Mumbai.this,food.class);
                startActivityForResult(i1,1);
                i1.putExtra("new_for_spinner","value");
            }
            if(new_for_radio==1 && new_for_spinner==3)
            {
                Intent i1= new Intent(Mumbai.this,food.class);
                startActivityForResult(i1,1);
                i1.putExtra("new_for_spinner","value");
            }
            if(new_for_radio==1 && new_for_spinner==4)
            {
                Intent i1= new Intent(Mumbai.this,food.class);
                startActivityForResult(i1,1);
                i1.putExtra("new_for_spinner","value");
            }
            if(new_for_radio==1 && new_for_spinner==5)
            {
                Intent i1= new Intent(Mumbai.this,food.class);
                startActivityForResult(i1,1);
                i1.putExtra("new_for_spinner","value");
            }



        }
    });
}

    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if(requestCode==1 && resultCode==0)
        {
            Toast.makeText(this, "Back from Image page", Toast.LENGTH_SHORT).show();
            super.onActivityResult(requestCode, resultCode, data);

        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

1 个答案:

答案 0 :(得分:0)

由于此行,您变量 new_for_spinner new_for_radio for_spinner

 for_spinner=new_for_spinner; 

另外我建议你阅读http://java.about.com/od/javasyntax/a/nameconventions.htm,因为命名一个类 two.class 非常糟糕....