我正在嘗試在使用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
不可以。變量「text」應該是Spinner所選擇的語言,而text1應該是即將翻譯的文本。 – MCoder
你忘了加'break'; –
呃......我在哪裏休息? – MCoder