2017-02-04 81 views
0

我正在嘗試在使用Microsoft Translator API的Android Studio中製作翻譯器應用程序,但由於某些原因,決定將某些文本翻譯成哪種語言的switch語句未運行。 我的代碼是:爲什麼這個switch語句沒有運行?

button.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     LayoutInflater li = LayoutInflater.from(context); 
     final View promptsView = li.inflate(R.layout.alertdialog, null); 
     AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context); 

     alertDialogBuilder.setView(promptsView); 
     final Spinner spinner = (Spinner) promptsView.findViewById(R.id.LanguagePicker); 
     final ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(Text.this, 
         R.array.language, android.R.layout.simple_spinner_item); 

     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

     spinner.setAdapter(adapter); 

     alertDialogBuilder.setCancelable(false) 
     /*The setPositiveButton determines which language I want it translated to.*/ 
         .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog,int id) { 

           //this is the main line 

           text = spinner.getSelectedItem().toString(); 

           spinner.setAdapter(adapter); 
           String r = textInput.getText().toString(); 
           allTheTranslatedText = getTranslatedText(r); 
           Log.e("Checkpoint", "Program started to execute getTranslatedText()"); 
              textOutput.setText(allTheTranslatedText); 

          } 
          }) 

         .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int id) { 
           dialog.cancel(); 
          } 
          }); 
     AlertDialog alertDialog = alertDialogBuilder.create(); 
     alertDialog.show(); 


     } 
    }); 

    } 

/* getTranslatedText() outputs text */ 
public String getTranslatedText (String text1){ 
    Translate.setClientId("CLIENT ID"); 
    Translate.setClientSecret("CLIENT SECRET"); 
    String translatedText = ""; 
    Log.e("Checkpoint", "getTranslatedText() was called"); 
    try { 
    switch (text) { 
     case "English": 
     translatedText = Translate.execute(text1, Language.AUTO_DETECT, Language.ENGLISH); // location of Unhandled exception 
     outputLanguageLabel.setText("English"); 
     Log.e("Checkpoint", "The program should work perfectly if this appears."); 

     case "French": 
     translatedText = Translate.execute(text1, Language.AUTO_DETECT, Language.FRENCH); 
     outputLanguageLabel.setText("French"); 
     // location of Unhandled exception 
     case "Spanish": 
     translatedText = Translate.execute(text1, Language.AUTO_DETECT, Language.SPANISH); 
     outputLanguageLabel.setText("Spanish"); 
// location of Unhandled exception 
     case "German": 
     translatedText = Translate.execute(text1, Language.AUTO_DETECT, Language.GERMAN); 
     outputLanguageLabel.setText("German"); 
// location of Unhandled exception 
     case "Italian": 
     translatedText = Translate.execute(text1, Language.AUTO_DETECT, Language.ITALIAN); 
     outputLanguageLabel.setText("Italian"); 
// location of Unhandled exception 
     case "Russian": 
     translatedText = Translate.execute(text1, Language.AUTO_DETECT, Language.RUSSIAN); 
     outputLanguageLabel.setText("Russian"); 
// location of Unhandled exception 
     case "Mandarin": 
     translatedText = Translate.execute(text1, Language.AUTO_DETECT, Language.CHINESE_TRADITIONAL); 
     outputLanguageLabel.setText("Mandarin"); 
// location of Unhandled exception 
     case "Korean": 
     translatedText = Translate.execute(text1, Language.AUTO_DETECT, Language.KOREAN); 
     outputLanguageLabel.setText("Korean"); 
// location of Unhandled exception 
     case "Japanese": 
     translatedText = Translate.execute(text1, Language.AUTO_DETECT, Language.JAPANESE); 
     outputLanguageLabel.setText("Japanese"); 

// location of Unhandled exception 
     default: 
     Log.e("Checkpoint", "Program didn't recognize text."); 
    } 

    } catch (Exception e) { 
      e.printStackTrace(); 
    } 
    return translatedText; 
} 

} 

我用logcat的語句來確定哪些代碼運行和代碼不是,但是當我選擇的是英語,因爲我希望我的文字被翻譯成,logcat中提交這個語言:

Checkpoint: Program started to execute getTranslatedText() 
Checkpoint: getTranslatedText() was called 

什麼,而不是我想象的還要所做的:

Checkpoint: Program started to execute getTranslatedText() 
Checkpoint: getTranslatedText() was called 
Checkpoint: The program should work perfectly if this appears 

這意味着switch語句沒有運行,因爲3號線將是在logcat上。有人可以解釋爲什麼嗎?

UPDATE:還有另一個錯誤:

ViewRootImpl: sendUserActionEvent() mView == null 
+1

不可以。變量「text」應該是Spinner所選擇的語言,而text1應該是即將翻譯的文本。 – MCoder

+1

你忘了加'break'; –

+0

呃......我在哪裏休息? – MCoder

回答

1

你忘了你的switch語句添加突破:

switch (text) { 
    case "English": 
    translatedText = Translate.execute(text1, Language.AUTO_DETECT, Language.ENGLISH); // location of Unhandled exception 
    outputLanguageLabel.setText("English"); 
    Log.e("Checkpoint", "The program should work perfectly if this appears."); 

    // Need to add break; after case 
    break; 

    ... 

    default: 
    Log.e("Checkpoint", "Program didn't recognize text."); 
} 

對於每個case,你需要添加休息。如果你不是,所有的情況下會去default:


UPDATE

看來你得到錯誤text的開關情況:

public String getTranslatedText (String text1){ 
     Translate.setClientId("CLIENT ID"); 
     Translate.setClientSecret("CLIENT SECRET"); 
     String translatedText = ""; 
     Log.e("Checkpoint", "getTranslatedText() was called"); 
     try { 

      // Should check it first 
      Log.d("TEXT", "text is = " + text); 
      switch (text) { 
       case "English": 
       ... 
       defaut: 
       ... 
      } 
     } 
     .. 
     } 

你需要檢查爲text值。

+0

問題是,缺省:log語句沒有執行。 – MCoder

+0

@MCoder:忘記給交換機的文字 –

+0

由於某些原因,它仍然沒有記錄第3條消息。 – MCoder

0

您在開關表達式中犯了一個錯誤,因爲您編寫的是文本而不是text1,以及您忘記了break語句。

public String getTranslatedText (String **text1**){ 
     Translate.setClientId("CLIENT ID"); 
     Translate.setClientSecret("CLIENT SECRET"); 
     String translatedText = ""; 
     Log.e("Checkpoint", "getTranslatedText() was called"); 
     try { 
      switch (**text1**) { 
       case "English": 
        translatedText = Translate.execute(text1, Language.AUTO_DETECT, Language.ENGLISH); // location of Unhandled exception 
        outputLanguageLabel.setText("English"); 
        Log.e("Checkpoint", "The program should work perfectly if this appears."); 
         break; 

       case "French": 
        translatedText = Translate.execute(text1, Language.AUTO_DETECT, Language.FRENCH); 
        outputLanguageLabel.setText("French"); 
         break; 
        // location of Unhandled exception 
       case "Spanish": 
        translatedText = Translate.execute(text1, Language.AUTO_DETECT, Language.SPANISH); 
        outputLanguageLabel.setText("Spanish"); 
         break; 
          ....... 
+1

請在發佈自己的答案之前,嘗試閱讀評論和其他答案。 –

+0

文本和文本1旨在保存不同的值。 – MCoder

相關問題