2017-10-20 18 views
-3

Bonjour j'ai une instruction switch, je veux sauver la variable valeur intent.getIntExtra("Position",0)-Position mais quand je le fais me donner l'erreur mentionnée, voici mon codeinstruction switch erreur: Déclaration doit être préfixé avec étui étiquette

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

Que dois-je faire? Merci

+0

Pourquoi utilisez-vous l'instruction switch? Un changement avec un seul cas n'est-il pas juste une déclaration trop compliquée? –

+3

mettre 'int Positions = intention.getIntExtra (" Position ", 0);' sous la déclaration 'case 2' –

+0

oui c'est, mais ce n'est pas tout de mon code, je viens de copier un morceau de celui-ci :) @Ryry –

Répondre

0

Comme @Samuel Roberts dit dans les commentaires, vous devez déplacer:

int Positions = intent.getIntExtra("Position",0); 

soit à l'intérieur de votre case 2 (et donc peut-être dans vos autres cas aussi) ou à l'extérieur de votre switch. L'erreur que vous obtenez dit "vous ne pouvez avoir qu'une instruction case à l'intérieur d'un switch, d'autres instructions ne sont pas autorisées sans être à l'intérieur d'un case".

S'il vous plaît voir la documentation ici: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

Hope this helps!

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

Essayez cette