无法在文本视图中显示翻译后的文本
Unable to display translated text on text view
我正在使用 ML 套件的设备翻译 API 来翻译 text.Although 翻译工作得很好,但我无法在我的文本中显示翻译后的文本 view.I 已尝试使用 setText 方法但文本中没有显示任何内容 view.Everything 正在 logcat 中显示。
我在Android工作室工作
编辑:
这显示正在下载的模型
TranslatorOptions options =
new TranslatorOptions.Builder()
.setSourceLanguage(TranslateLanguage.ENGLISH)
.setTargetLanguage(TranslateLanguage.BENGALI)
.build();
final Translator englishBengaliTranslator =
Translation.getClient(options);
//DOWNLOAD THE MODEL
DownloadConditions conditions = new DownloadConditions.Builder()
.requireWifi()
.build();
englishBengaliTranslator.downloadModelIfNeeded(conditions)
.addOnSuccessListener(
new OnSuccessListener() {
@Override
public void onSuccess(Object o) {
}
//@Override
public void onSuccess(Void v) {
// Model downloaded successfully. Okay to start translating.
// (Set a flag, unhide the translation UI, etc.)
Toast.makeText(getApplicationContext(), "Model Downloaded.Translation will start ", Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(
new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// Model couldn’t be downloaded or other internal error.
// ...
Toast.makeText(getApplicationContext(), "Model could not be Downloaded. ", Toast.LENGTH_SHORT).show();
}
});
在这段代码中,我调用了翻译函数(因为现在已经下载了模型)
englishBengaliTranslator.translate(stringResult)
.addOnSuccessListener(
new OnSuccessListener() {
@Override
public void onSuccess(Object o) {
}
//@Override
public void onSuccess(@NonNull String translatedText1) {
textViewTranslatedText.setText(translatedText1);
}
})
.addOnFailureListener(
new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// Error.
// ...
Toast.makeText(getApplicationContext(), "Text could not be translated. ", Toast.LENGTH_LONG).show();
}
});
我已经使用了 setText 函数,但翻译后的文本仍然显示在 logcat 而不是文本视图中(翻译工作完全正常)。如何在我的文本视图中显示它而不是 logcat?任何线索都会有所帮助!
这里是 logcat 的图像,其中显示了翻译后的文本:enter image description here
试试这个代码,
englishBengaliTranslator.downloadModelIfNeeded(conditions)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
// Model downloaded successfully. Okay to start translating.
// (Set a flag, unhide the translation UI, etc.)
Toast.makeText(getApplicationContext(), "Model Downloaded.Translation will start ", Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(
new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// Model couldn’t be downloaded or other internal error.
// ...
Toast.makeText(getApplicationContext(), "Model could not be Downloaded. ", Toast.LENGTH_SHORT).show();
}
});
以上代码没有任何作用。我刚刚删除了不必要的接口。
但是下面的代码做了一些事情。我的意思是它指定了 return 类型。
englishBengaliTranslator.translate(stringResult)
.addOnSuccessListener(new OnSuccessListener<String>() {
@Override
public void onSuccess(String s) {
if(textViewTranslatedText!=null)
textViewTranslatedText.setText(translatedText1);
else
Log.d(TAG, "onSuccess: translation Done but text view null");
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
//Handle onFailure
}
});
我正在使用 ML 套件的设备翻译 API 来翻译 text.Although 翻译工作得很好,但我无法在我的文本中显示翻译后的文本 view.I 已尝试使用 setText 方法但文本中没有显示任何内容 view.Everything 正在 logcat 中显示。
我在Android工作室工作
编辑: 这显示正在下载的模型
TranslatorOptions options =
new TranslatorOptions.Builder()
.setSourceLanguage(TranslateLanguage.ENGLISH)
.setTargetLanguage(TranslateLanguage.BENGALI)
.build();
final Translator englishBengaliTranslator =
Translation.getClient(options);
//DOWNLOAD THE MODEL
DownloadConditions conditions = new DownloadConditions.Builder()
.requireWifi()
.build();
englishBengaliTranslator.downloadModelIfNeeded(conditions)
.addOnSuccessListener(
new OnSuccessListener() {
@Override
public void onSuccess(Object o) {
}
//@Override
public void onSuccess(Void v) {
// Model downloaded successfully. Okay to start translating.
// (Set a flag, unhide the translation UI, etc.)
Toast.makeText(getApplicationContext(), "Model Downloaded.Translation will start ", Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(
new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// Model couldn’t be downloaded or other internal error.
// ...
Toast.makeText(getApplicationContext(), "Model could not be Downloaded. ", Toast.LENGTH_SHORT).show();
}
});
在这段代码中,我调用了翻译函数(因为现在已经下载了模型)
englishBengaliTranslator.translate(stringResult)
.addOnSuccessListener(
new OnSuccessListener() {
@Override
public void onSuccess(Object o) {
}
//@Override
public void onSuccess(@NonNull String translatedText1) {
textViewTranslatedText.setText(translatedText1);
}
})
.addOnFailureListener(
new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// Error.
// ...
Toast.makeText(getApplicationContext(), "Text could not be translated. ", Toast.LENGTH_LONG).show();
}
});
我已经使用了 setText 函数,但翻译后的文本仍然显示在 logcat 而不是文本视图中(翻译工作完全正常)。如何在我的文本视图中显示它而不是 logcat?任何线索都会有所帮助!
这里是 logcat 的图像,其中显示了翻译后的文本:enter image description here
试试这个代码,
englishBengaliTranslator.downloadModelIfNeeded(conditions)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
// Model downloaded successfully. Okay to start translating.
// (Set a flag, unhide the translation UI, etc.)
Toast.makeText(getApplicationContext(), "Model Downloaded.Translation will start ", Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(
new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// Model couldn’t be downloaded or other internal error.
// ...
Toast.makeText(getApplicationContext(), "Model could not be Downloaded. ", Toast.LENGTH_SHORT).show();
}
});
以上代码没有任何作用。我刚刚删除了不必要的接口。
但是下面的代码做了一些事情。我的意思是它指定了 return 类型。
englishBengaliTranslator.translate(stringResult)
.addOnSuccessListener(new OnSuccessListener<String>() {
@Override
public void onSuccess(String s) {
if(textViewTranslatedText!=null)
textViewTranslatedText.setText(translatedText1);
else
Log.d(TAG, "onSuccess: translation Done but text view null");
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
//Handle onFailure
}
});