"Unhandled exception: Java.lang.exception" in code
Matthew Martinez
I am making a translator app in Android Studio using Microsoft Translator API. However, when I am trying to store some translated text from Microsoft Translator API in a variable, I get the error "Unhandled exception: Java.lang.exception".
What does this error mean, and how can I fix it?
My code is:
public class Text extends AppCompatActivity { Context context; Button button; public static String text; TextView textOutput = (TextView) findViewById(R.id.Result); EditText textInput = (EditText) findViewById(R.id.TextBar); public static String allTheTranslatedText; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.text_wall); Spinner spinner = (Spinner) findViewById(R.id.LanguagePicker); button = (Button) findViewById(R.id.ConfirmationButton); context = this; ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.language, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { LayoutInflater li = LayoutInflater.from(context); View promptsView = li.inflate(R.layout.alertdialog, null); AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder( context); alertDialogBuilder.setView(promptsView); alertDialogBuilder .setCancelable(false) .setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int id) { Spinner spinner = (Spinner) findViewById(R.id.LanguagePicker); text = spinner.getSelectedItem().toString(); String r = textInput.getText().toString(); allTheTranslatedText = getTranslatedText(r); // this is the location of error. } }) .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); AlertDialog alertDialog = alertDialogBuilder.create(); alertDialog.show(); } }); } public String getTranslatedText (String text1) throws Exception{ Translate.setClientId("nnn333"); Translate.setClientSecret("shoppingfor1gbatmymicrosoftdatamarket"); String translatedText = ""; switch (text) { case "English": translatedText = Translate.execute(text1, Language.ENGLISH); case "French": translatedText = Translate.execute(text1, Language.FRENCH); case "Spanish": translatedText = Translate.execute(text1, Language.SPANISH); case "German": translatedText = Translate.execute(text1, Language.GERMAN); case "Italian": translatedText = Translate.execute(text1, Language.ITALIAN); case "Russian": translatedText = Translate.execute(text1, Language.RUSSIAN); case "Mandarin": translatedText = Translate.execute(text1, Language.CHINESE_TRADITIONAL); case "Korean": translatedText = Translate.execute(text1, Language.KOREAN); case "Japanese": translatedText = Translate.execute(text1, Language.JAPANESE); } return translatedText; }
} 9 2 Answers
This error message is being shown to you by your IDE in order to warn you that the java compiler will generate the same error later if you try to compile.
The error means that your onClick() method is invoking another method which has been declared with throws Exception, and java mandates that when you do this you have to either have code which catches the exception, or in turn declare the calling function also with throws Exception.
And since onClick() is an override, you are probably not allowed to add throws Exception to it.
So, you can either add a try { ... } catch( Exception e ) { ... } in your onClick() method, or declare your getTranslatedText() method without throws Exception.
try to put your code in a try/ catch example
try{
yourcodegoeshere..
}
catch(Exception e){
e.printStackTrace();
}then in your compiler we will see your error and we will tell you why it happens
4