我正在使用谷歌翻译API编写一个基本的翻译器应用程序,但是当我单击按钮并到达翻译器翻译初始化时,应用程序崩溃了,我尝试了几种不同的解决方案,但似乎找不到有效的解决方案。 这是我第一次在这里发布信息,我相信我在下面发布的是崩溃日志。
import com.google.cloud.translate.Translate;
import com.google.cloud.translate.Translate.TranslateOption;
import com.google.cloud.translate.TranslateOptions;
import com.google.cloud.translate.Translation;
...
public void onClick(View v) {
Log.d("translatecheck", "I clicked a button");
String[] isoArr = getResources().getStringArray(R.array.iso);
Log.d("translatecheck", "isoarr created");
TextView textElement = (TextView) findViewById(R.id.view);
Log.d("translatecheck", "textElement initialized");
//This is where the app cashes
//Translate translate = TranslateOptions.getDefaultInstance().getService();
Translate translate = createTranslateService();
Log.d("translatecheck", "translate service created");
String sourceLang = "en";
String targetLang = isoArr[pos];
TranslateOption srcLang = TranslateOption.sourceLanguage(sourceLang);
TranslateOption tgtLang = TranslateOption.targetLanguage(targetLang);
Log.d("translatecheck", "translate service initialized");
// The text to translate
editText = findViewById(R.id.edit);
String text = editText.getText().toString();
Log.d("translatecheck", "converted edit text to string");
//Translates some text into spinner language
Translation translation = translate.translate(text, srcLang, tgtLang);
/*Translation translation =
translate.translate(
text,
TranslateOption.sourceLanguage("en"),
TranslateOption.targetLanguage(isoArr[pos]));
*/
Log.d("translatecheck", "we have translated");
//textElement.setText(translation.toString()); //leave this line to assign a specific text
textElement.setText(translation.getTranslatedText());
}
...
public static Translate createTranslateService() {
return TranslateOptions.getDefaultInstance().getService();
}
崩溃日志:
04-09 01:00:59.442 4076-4076/com.example.translate01 I/DpmTcmClient: RegisterTcmMonitor from: com.android.okhttp.TcmIdleTimerMonitor
04-09 01:00:59.455 4076-4076/com.example.translate01 D/AndroidRuntime: Shutting down VM
04-09 01:00:59.456 4076-4076/com.example.translate01 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.translate01, PID: 4076
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
at java.net.InetAddress.lookupHostByName(InetAddress.java:431)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
at java.net.InetAddress.getAllByName(InetAddress.java:215)
at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:188)
at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:157)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:100)
at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:358)
at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:341)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:331)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:249)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:437)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:114)
at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:142)
at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:84)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1040)
at com.google.cloud.ServiceOptions.getAppEngineProjectIdFromMetadataServer(ServiceOptions.java:460)
at com.google.cloud.ServiceOptions.getAppEngineProjectId(ServiceOptions.java:429)
at com.google.cloud.ServiceOptions.getDefaultProjectId(ServiceOptions.java:336)
at com.google.cloud.ServiceOptions.getDefaultProject(ServiceOptions.java:313)
at com.google.cloud.ServiceOptions.<init>(ServiceOptions.java:264)
at com.google.cloud.translate.TranslateOptions.<init>(TranslateOptions.java:143)
at com.google.cloud.translate.TranslateOptions.<init>(TranslateOptions.java:39)
at com.google.cloud.translate.TranslateOptions$Builder.build(TranslateOptions.java:138)
at com.google.cloud.translate.TranslateOptions.getDefaultInstance(TranslateOptions.java:261)
at com.example.translate01.MainActivity.createTranslateService(MainActivity.java:96)
at com.example.translate01.MainActivity$1.onClick(MainActivity.java:51)
at android.view.View.performClick(View.java:5209)
at android.view.View$PerformClick.run(View.java:21247)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5428)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:0)
您有一个NetworkOnMainThreadException,这意味着您正在主线程上运行与Internet相关的任务。
您无法在Android主线程上运行与Internet相关的任务(有关原因,请参见Keeping your app responsive)。
只需将执行Internet任务的转换操作(大概为translate.translate()
)移动到新的Thread,Async任务或您喜欢的任何结构中,然后侦听结果的回调即可。