我的应用程序在调试模式下运行良好,但在发布模式下崩溃,所以当我将可调试设置为true时,我意识到错误来自开关情况和一种方法。
由以下原因引起:java.lang.IllegalStateException:到目前为止没有成功匹配
在com.example.specakingtranslator.MainActivity.p(:3334(
的com.example.speakingtranslator.MMainActivity$e.onClick(:228(
android.view.view.performClick(View.java:7016(
在android.View.View.access$3200(View.java:783(
android.vview.View$PerformClick.run(View.jjava:26595(
1android.os.Handler.handleCallback(Handler.java:873(
2android.os.Handler.dispatchMessage voke(本机方法(
在com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:497(
com.android.nternal.os.ZygoteInit.main(ZygoteInit.java:912(
由java.lang.IllegalStateException引起:到目前为止没有成功匹配
这是我的代码
default:
if (checkInternetConnection()) {
getTranslateService();
result = translate();
} else {
result = getResources().getString(R.string.no_connection);
}
break;
editTranslate.setText(result);
以下是的方法
public void getTranslateService() {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
try (InputStream is = getResources().openRawResource(R.raw.credentials)) {
//Get credentials:
final GoogleCredentials myCredentials = GoogleCredentials.fromStream(is);
//Set credentials and get translate service:
TranslateOptions translateOptions = TranslateOptions.newBuilder().setCredentials(myCredentials).build();
translate = translateOptions.getService();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
public String translate() {
originalText = editText.getText().toString();
Translation translation = translate.translate(originalText, Translate.TranslateOption.targetLanguage("yo"), Translate.TranslateOption.model("nmt"));
translatedText = translation.getTranslatedText();
//Translated text and original text are set to TextViews:
return translatedText;
}
这是翻译按钮的OnClickListener的完整代码,我有一组单词,我不想麻烦把它们发送到服务器,这就是为什么我使用switch case来测试它们,所以如果输入的值不在列表中,那么它应该把单词发送到api进行翻译。
trans.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
String t = editText.getText().toString().toLowerCase().trim();
if (t.length() == 0) {
Toast.makeText(getApplicationContext(), "Enter Text Value", Toast.LENGTH_LONG).show();
editText.requestFocus();
return;
}
String result;
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("Please wait while loading..."); // Setting Message
progressDialog.setTitle("Translating"); // Setting Title
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); // Progress Dialog Style Spinner
progressDialog.setCancelable(true);
progressDialog.show();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
progressDialog.dismiss();
}}, 2000);
switch (t) {
case "i love you":
result = "Mo nif̣ẹrẹ";
break;
case "i want you":
result = "Mo fẹ̣ ọ";
break;
case "good evening":
result = "Ẹ kaalẹ";
break;
case "i like you":
result = "Mo fẹran rẹ";
break;
case "how are you":
result = "Bawo lo se wa";
break;
case "i need your help":
result = "Mo nilo iranlọwọ re";
break;
case "i need money":
result = "Mo nilo owo";
break;
case "i miss you":
result = "Aro rẹ nsọ mi";
break;
case "what is my name":
result = "Kini orukọ mi";
break;
case "i love my husband":
result = "Mo niifẹ ọkọ mi";
break;
case "i am hungry":
result = "Ebi n pa mi";
break;
case "i want hot water":
result = "Mo fẹ omi gbona";
break;
case "look at me":
result = "Wo mi";
break;
case "come back":
result = "Pada wa";
break;
case "it is very good":
result = "O dara pupọ";
break;
case "come to school":
result = "Wa si ile-iwe";
break;
case "help yourself":
result = "Ran ara rẹ lọwọ";
break;
case "wait for me":
result = "Duro de mi";
break;
default:
if (checkInternetConnection()) {
getTranslateService();
result = translate();
} else {
result = getResources().getString(R.string.no_connection);
}
break;
}
editTranslate.setText(result);
}
});
将minimyenabled恢复为false已经解决了问题。