您好我有一个switch语句,我想将intent.getIntExtra("Position",0)
值保存到Position
变量,但是当我这样做时会给我提到错误,这是我的代码
if(intent.getIntExtra("HandyLevel",0)==1 && SharedPreferenceStuff.getLevel(getApplicationContext())>=1) //Preface
{
HandyLevel = intent.getIntExtra("HandyLevel",0);
switch (intent.getIntExtra("Position",0))
{
int Positions = intent.getIntExtra("Position",0);
case 2: //History
if(intent.getStringExtra("Divider").equals("Q1History" )) {
if(goToNextLevel) {
if (SharedPreferenceStuff.getSubLevel(getApplicationContext()) == 3)
SharedPreferenceStuff.setSubLevel(getApplicationContext(), 4);
localIntent = new Intent(QuestionFrame.this, LevelOne.class);
localIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(localIntent);
overridePendingTransition(R.anim.slide_start_from_button, R.anim.slide_to_up);
}
else Toast.makeText(getApplicationContext(),"FUCCCK",Toast.LENGTH_LONG).show();
}
break;
}
}
我该怎么办?感谢
答案 0 :(得分:0)
就像@Samuel Roberts在评论中所说,你需要搬家:
int Positions = intent.getIntExtra("Position",0);
位于case 2
内(因此也可能位于其他情况下)或switch
之外。您收到的错误是"您只能在case
内发出switch
声明,如果不在case
内,则不允许使用其他声明&#34 ;
请参阅此处的文档:https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
希望这有帮助!
答案 1 :(得分:0)
if(intent.getIntExtra("HandyLevel",0)==1 && SharedPreferenceStuff.getLevel(getApplicationContext())>=1) //Preface
{
HandyLevel = intent.getIntExtra("HandyLevel",0);
int Positions = intent.getIntExtra("Position",0);
switch (intent.getIntExtra("Position",0))
{
case 2: //History
if(intent.getStringExtra("Divider").equals("Q1History" )) {
if(goToNextLevel) {
if (SharedPreferenceStuff.getSubLevel(getApplicationContext()) == 3)
SharedPreferenceStuff.setSubLevel(getApplicationContext(), 4);
localIntent = new Intent(QuestionFrame.this, LevelOne.class);
localIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(localIntent);
overridePendingTransition(R.anim.slide_start_from_button, R.anim.slide_to_up);
}
else Toast.makeText(getApplicationContext(),"FUCCCK",Toast.LENGTH_LONG).show();
}
break;
}
}
试试这个