开发者

Syntax error on token "{", SwitchLabels expected after this token

i want to open new form when i select option from spinner class

i try this but i have syntax error

Syntax error on token "{", SwitchLabels expected after this token

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tf);
   开发者_运维知识库 Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
    ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(
        this, R.array.tfoptions,android.R.layout.simple_spinner_item);
    adapter2.setDropDownViewResource(
        android.R.layout.simple_spinner_dropdown_item);
    spinner2.setAdapter(adapter2);

    spinner2.setOnItemSelectedListener(new MyOnItemSelectedListener());

    public class MyOnItemSelectedListener implements OnItemSelectedListener {

        public void onItemSelected(AdapterView<?> parent,
        View view, int pos, long id) {
            switch (view.isClickable()) {       <---------------- syntax error 
                Spinner spinner2;
            case spinner2.setSelection(0):
                startActivity(new Intent(this,To.class));
                break;
            case spinner2.setSelection(1):
                startActivity(new Intent(this,out.class));
            default:
                break;
            }
        }
        public void onNothingSelected(AdapterView parent) {
            // Do nothing.
        }
    }
}


You have tried to declare a variable before your first case block. You cannot do this. Move the variable declaration to above the switch.


My friend you can't declare a variable within a switch statement:

View view, int pos, long id) {
     switch (view.isClickable()) {       
         Spinner spinner2;   <---------------- here is your syntax error 
         case spinner2.setSelection(0):
         startActivity(new Intent(this,To.class));
         break;
         case spinner2.setSelection(1):
         startActivity(new Intent(this,out.class));
         default:
         break;
         }
     }

instead you have to move above your Spinner declaration:

View view, int pos, long id) {
     Spinner spinner2;  // <---------------- now it's ok
     switch (view.isClickable()) {       

         case spinner2.setSelection(0):
         startActivity(new Intent(this,To.class));
         break;
         case spinner2.setSelection(1):
         startActivity(new Intent(this,out.class));
         default:
         break;
         }
     }

BTW, in your code you aren't initializing your Spinner..., you should also do it like:

Spinner spinner2 = new Spinner(); 


I assume that view.isClickable() returns a boolean, in which case you should use an if not a switch.

That said, what is case spinner2.setSelection(0):??

A case label can't invoke code, and it can't be dynamic. case labels should be constants, either integers or enum values.

You're also declaring Spinner spinner2; (and declaring it in the wrong place, as Oli points out) but it's not set to anything so your spinner2.setSelection(x) would throw a NullPointerException even if you could get this to execute.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜