标记“{”上的语法错误,此标记后应有 SwitchLabels

发布于 2024-10-31 10:02:21 字数 1389 浏览 0 评论 0原文

当我从微调器类中选择选项时我想打开新表单

我尝试这个但我有语法错误

标记“{”上的语法错误, 此后预计会有 SwitchLabels 令牌

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.
        }
    }
}

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.
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

场罚期间 2024-11-07 10:02:21

您尝试在第一个 case 块之前声明一个变量。你不能这样做。将变量声明移至开关上方。

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

花心好男孩 2024-11-07 10:02:21

我的朋友,你不能在 switch 语句中声明变量:

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;
         }
     }

相反,你必须移到 Spinner 声明之上:

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;
         }
     }

顺便说一句,在你的代码中,你没有初始化 Spinner...,你也应该这样做:

Spinner spinner2 = new Spinner(); 

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(); 
一世旳自豪 2024-11-07 10:02:21

我假设 view.isClickable() 返回一个布尔值,在这种情况下,您应该使用 if 而不是 switch

也就是说,什么是 case spinner2.setSelection(0):

case 标签不能调用代码,也不能是动态的。
case 标签应该是常量,可以是整数或枚举值。

您还声明了 Spinner spinner2; (并在错误的位置声明了它,正如 Oli 指出的那样),但它没有设置为任何内容,因此您的 spinner2.setSelection(x)即使你可以让它执行,也会抛出 NullPointerException 。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文