标记“{”上的语法错误,此标记后应有 SwitchLabels
当我从微调器类中选择选项时我想打开新表单
我尝试这个但我有语法错误
标记“{”上的语法错误, 此后预计会有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您尝试在第一个
case
块之前声明一个变量。你不能这样做。将变量声明移至开关
上方。You have tried to declare a variable before your first
case
block. You cannot do this. Move the variable declaration to above theswitch
.我的朋友,你不能在 switch 语句中声明变量:
相反,你必须移到 Spinner 声明之上:
顺便说一句,在你的代码中,你没有初始化 Spinner...,你也应该这样做:
My friend you can't declare a variable within a switch statement:
instead you have to move above your Spinner declaration:
BTW, in your code you aren't initializing your Spinner..., you should also do it like:
我假设 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 anif
not aswitch
.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 yourspinner2.setSelection(x)
would throw a NullPointerException even if you could get this to execute.