我一直在尝试制作翻译应用程序而且我已经相应地设置了所有内容。但我仍然遇到这个特殊的错误,并试图解决它一段时间。
package mobi.the404.appointed;
import android.content.Intent;
import android.database.Cursor;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import com.memetix.mst.language.Language;
import com.memetix.mst.translate.Translate;
import java.util.ArrayList;
import java.util.List;
public class TranslateActivity extends ActionBarActivity{
private TextView textViewReceived;
private TextView textViewTranslatedText;
private Spinner spinner;
private Button buttonTTranslate;
private Button buttonTBack;
private String selectedAp;
private DBAdapter db = new DBAdapter(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_translate);
Translate.setClientId("ID");
Translate.setClientSecret("SECRET");
this.setTitle("Translate Appointment");
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if(extras != null) {
selectedAp = intent.getExtras().getString("selectedAp");
}
textViewReceived = (TextView) findViewById(R.id.textViewRecieved);
textViewTranslatedText = (TextView) findViewById(R.id.textViewTranslatedText);
spinner = (Spinner) findViewById(R.id.spinner);
buttonTBack = (Button) findViewById(R.id.buttonTBack);
buttonTTranslate = (Button) findViewById(R.id.buttonTTranslate);
try {
loadDetails();
} catch (Exception e) {
e.printStackTrace();
}
buttonTBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(TranslateActivity.this, AppointedActivity.class);
startActivity(intent);
try {
this.finalize();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
});
buttonTTranslate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
textViewTranslatedText.setText(translate(textViewReceived.getText().toString(), spinner.getSelectedItem().toString()));
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
private void loadDetails() throws Exception {
db.open();
Cursor item = db.getRecord(selectedAp);
textViewReceived.append(item.getString(3));
db.close();
List<String> listLangs = new ArrayList<>();
listLangs.add("Arabic");
listLangs.add("Dutch");
listLangs.add("German");
listLangs.add("French");
listLangs.add("Greek");
listLangs.add("Hindi");
listLangs.add("Italian");
listLangs.add("Portuguese");
listLangs.add("Thai");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, listLangs);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
}
public String translate(String text, String lang) throws Exception {
String translatedText = "";
Language send = null;
if(lang == "Arabic"){
send = Language.ARABIC;
} if(lang == "Dutch"){
send = Language.DUTCH;
} if(lang == "German"){
send = Language.GERMAN;
} if(lang == "French"){
send = Language.FRENCH;
} if(lang == "Greek"){
send = Language.GREEK;
} if(lang == "Hindi"){
send = Language.HINDI;
} if(lang == "Italian"){
send = Language.ITALIAN;
} if(lang == "Portuguese"){
send = Language.PORTUGUESE;
} if(lang == "Thai"){
send = Language.THAI;
}
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();
translatedText = Translate.execute(text, send);
return translatedText;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_translate, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
发出的错误就是这个;
04-03 14:51:41.721 23897-23897/mobi.the404.appointed W/System.err﹕ java.lang.Exception: [microsoft-translator-api] Error retrieving translation : null
04-03 14:51:41.726 23897-23897/mobi.the404.appointed W/System.err﹕ at com.memetix.mst.MicrosoftTranslatorAPI.retrieveString(MicrosoftTranslatorAPI.java:202)
04-03 14:51:41.726 23897-23897/mobi.the404.appointed W/System.err﹕ at com.memetix.mst.translate.Translate.execute(Translate.java:61)
04-03 14:51:41.726 23897-23897/mobi.the404.appointed W/System.err﹕ at com.memetix.mst.translate.Translate.execute(Translate.java:76)
04-03 14:51:41.726 23897-23897/mobi.the404.appointed W/System.err﹕ at mobi.the404.appointed.TranslateActivity.translate(TranslateActivity.java:129)
04-03 14:51:41.726 23897-23897/mobi.the404.appointed W/System.err﹕ at mobi.the404.appointed.TranslateActivity$2.onClick(TranslateActivity.java:73)
04-03 14:51:41.726 23897-23897/mobi.the404.appointed W/System.err﹕ at android.view.View.performClick(View.java:5184)
04-03 14:51:41.726 23897-23897/mobi.the404.appointed W/System.err﹕ at android.view.View$PerformClick.run(View.java:20910)
04-03 14:51:41.726 23897-23897/mobi.the404.appointed W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:739)
04-03 14:51:41.726 23897-23897/mobi.the404.appointed W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
04-03 14:51:41.726 23897-23897/mobi.the404.appointed W/System.err﹕ at android.os.Looper.loop(Looper.java:145)
04-03 14:51:41.726 23897-23897/mobi.the404.appointed W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5942)
04-03 14:51:41.726 23897-23897/mobi.the404.appointed W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
04-03 14:51:41.726 23897-23897/mobi.the404.appointed W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:372)
04-03 14:51:41.726 23897-23897/mobi.the404.appointed W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
04-03 14:51:41.726 23897-23897/mobi.the404.appointed W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
04-03 14:51:41.726 23897-23897/mobi.the404.appointed W/System.err﹕ Caused by: android.os.NetworkOnMainThreadException
04-03 14:51:41.736 23897-23897/mobi.the404.appointed W/System.err﹕ at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
04-03 14:51:41.736 23897-23897/mobi.the404.appointed W/System.err﹕ at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
04-03 14:51:41.736 23897-23897/mobi.the404.appointed W/System.err﹕ at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
04-03 14:51:41.736 23897-23897/mobi.the404.appointed W/System.err﹕ at java.net.InetAddress.getAllByName(InetAddress.java:215)
04-03 14:51:41.736 23897-23897/mobi.the404.appointed W/System.err﹕ at com.android.okhttp.HostResolver$1.getAllByName(HostResolver.java:29)
04-03 14:51:41.736 23897-23897/mobi.the404.appointed W/System.err﹕ at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:232)
04-03 14:51:41.736 23897-23897/mobi.the404.appointed W/System.err﹕ at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:124)
04-03 14:51:41.736 23897-23897/mobi.the404.appointed W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:367)
04-03 14:51:41.736 23897-23897/mobi.the404.appointed W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:295)
04-03 14:51:41.736 23897-23897/mobi.the404.appointed W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:373)
04-03 14:51:41.736 23897-23897/mobi.the404.appointed W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:106)
04-03 14:51:41.736 23897-23897/mobi.the404.appointed W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:208)
04-03 14:51:41.736 23897-23897/mobi.the404.appointed W/System.err﹕ at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218)
04-03 14:51:41.736 23897-23897/mobi.the404.appointed W/System.err﹕ at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:25)
04-03 14:51:41.736 23897-23897/mobi.the404.appointed W/System.err﹕ at com.memetix.mst.MicrosoftTranslatorAPI.getToken(MicrosoftTranslatorAPI.java:133)
04-03 14:51:41.736 23897-23897/mobi.the404.appointed W/System.err﹕ at com.memetix.mst.MicrosoftTranslatorAPI.retrieveResponse(MicrosoftTranslatorAPI.java:160)
04-03 14:51:41.736 23897-23897/mobi.the404.appointed W/System.err﹕ at com.memetix.mst.MicrosoftTranslatorAPI.retrieveString(MicrosoftTranslatorAPI.java:199)
04-03 14:51:41.736 23897-23897/mobi.the404.appointed W/System.err﹕ ... 14 more
04-03 14:52:45.296 23897-23897/mobi.the404.appointed V/ActivityThread﹕ updateVisibility : ActivityRecord{2c2786f6 token=android.os.BinderProxy@3379767a {mobi.the404.appointed/mobi.the404.appointed.TranslateActivity}} show : true
应用程序没有崩溃但是给出了错误。我做错了什么?
PS:我已经在清单中获得了互联网访问许可。
谢谢.ID
答案 0 :(得分:2)
这里的问题是在主线程上禁止从API 11
网络调用开始,否则将阻止主线程(UI线程)等待响应。如果您查看堆栈跟踪,您会发现,当您在点击处理程序中调用以下行时:
textViewTranslatedText.setText(translate(textViewReceived.getText().toString(), spinner.getSelectedItem().toString()));
网络呼叫将由您在以下行使用的API
执行:
Translate.execute(text, send);
要避免此异常,您应该以异步方式调用Translate.execute
。如果此网络请求不需要花费太多时间,最简单的方法是使用AsyncTask
:
private static class MyAsyncTask extends AsyncTask<Request, Void, String>{
private WeakReference<TextView> translatedView;
public MyAsyncTask(TextView translatedView) {
this.translatedView = new WeakReference<TextView>(translatedView);
}
@Override
protected String doInBackground(Request... params) {
Request req = params[0];
String translatedText = Translate.execute(req.getText(), req.getLanguage());
return translatedText;
}
@Override
protected void onPostExecute(String s) {
TextView output = translatedView.get();
if (output != null){
output.setText(s);
}
}
}
private static class Request {
private String text;
private Language language;
public Request(Language language, String text) {
this.language = language;
this.text = text;
}
public Language getLanguage() {
return language;
}
public void setLanguage(Language language) {
this.language = language;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
但是,还有另外一种方法可以执行异步操作,例如使用IntentService
,创建HandlerThread
等。解决方案取决于您的需求和要求。您可以在此处阅读一些信息http://blog.nikitaog.me/2014/10/11/android-looper-handler-handlerthread-i/
答案 1 :(得分:0)
日志中的这一行是最重要的一行:
Caused by: android.os.NetworkOnMainThreadException
因此,每次使用调用Translate.execute
的函数时,请创建一个新的线程:
new Thread(){
public void run() {
// ...
translate(...);
// ...
}
}.start();