フラグメントを使用して Android 用の翻訳アプリを作成しています (タブがあるため)。
Microsoft の API で翻訳するための優れたチュートリアルをいくつか見つけましたが、これを見つけるまで、どれも機能しませんでした。
アクティビティとしては機能しますが、フラグメントに変更すると機能しません。
少し変更した後のコードは次のとおりです。
public class HomeFragment extends Fragment
{
TextView text;
String translatedText;
View rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_home, container, false);
text=(TextView)rootView.findViewById(R.id.translatedtext);
text.setText("<This text should change after translation has occurred in AsyncTask>");
new MyAsyncTask() {
protected void onPostExecute(Boolean result) {
text.setText(translatedText);
}
}.execute();
return rootView;
}
class MyAsyncTask extends AsyncTask<Void, Integer, Boolean>
{
@Override
protected Boolean doInBackground(Void... arg0) {
Translate.setClientId("MicrosoftTranslatorJavaAPI");
Translate.setClientSecret("0VHbhXQnJrZ7OwVqcoX/PDZlyLJS9co3cVev1TPr8iM=");
try {
translatedText = Translate.execute("I should probably set this to something a little less profane", Language.ENGLISH, Language.FRENCH);
} catch(Exception e) {
translatedText = e.toString();
}
return true;
}
}
}
XML は現在、単純な TextView です (Github の例のように)。
asynctask を機能させるには何を変更する必要がありますか?