"未处理的异常:Java.lang.exception"在代码中

时间:2017-01-22 01:18:12

标签: java android

我正在使用Microsoft Translator API在Android Studio中制作翻译应用程序。但是,当我尝试将一些来自Microsoft Translator API的翻译文本存储在变量中时,我得到错误"未处理的异常:Java.lang.exception"。

此错误意味着什么,我该如何解决?

我的代码是:

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

}

2 个答案:

答案 0 :(得分:1)

IDE会向您显示此错误消息,以便警告您,如果您尝试编译,java编译器将在以后生成相同的错误。

错误意味着您的onClick()方法正在调用另一个已使用throws Exception声明的方法,并且java要求在执行此操作时您必须拥有捕获异常的代码,或者转而使用throws Exception声明调用函数。

由于onClick()是覆盖,因此可能不允许您向其添加throws Exception

因此,您可以在try { ... } catch( Exception e ) { ... }方法中添加onClick(),也可以在没有 {{getTranslatedText()的情况下声明您的throws Exception方法 1}}。

答案 1 :(得分:-2)

尝试将您的代码放入try / catch中 示例

try{
yourcodegoeshere..
}
catch(Exception e){
e.printStackTrace();
}

然后在你的编译器中我们会看到你的错误,我们会告诉你它为什么会发生