I am trying to build a simple translator for my app. The app runs but
whenever I press the button to executes the translation, the app crashes.
Attached is my code and the error message my logcat gives me.
Your help will be greatly appreciated
--
You received this message because you are subscribed to the Google Groups
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/android-developers/2a1214c4-d892-44c9-aa56-3a7bdbd6e8abn%40googlegroups.com.
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.mlkit.common.model.DownloadConditions;
import com.google.mlkit.nl.translate.TranslateLanguage;
import com.google.mlkit.nl.translate.Translation;
import com.google.mlkit.nl.translate.Translator;
import com.google.mlkit.nl.translate.TranslatorOptions;
public class MainActivity extends AppCompatActivity {
EditText enteredText, translatedText;
Button translate;
boolean isDownloaded = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
enteredText = findViewById(R.id.editTextTextPersonName);
translatedText = findViewById(R.id.editTextTextPersonName2);
translate = findViewById(R.id.button);
// Create an English-Spanish translator:
TranslatorOptions options =
new TranslatorOptions.Builder()
.setSourceLanguage(TranslateLanguage.ENGLISH)
.setTargetLanguage(TranslateLanguage.SPANISH)
.build();
final Translator englishSpanishTranslator =
Translation.getClient(options);
DownloadConditions conditions = new DownloadConditions.Builder()
.requireWifi()
.build();
englishSpanishTranslator.downloadModelIfNeeded(conditions)
.addOnSuccessListener(
new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void v) {
// Model downloaded successfully. Okay to start
translating.
// (Set a flag, unhide the translation UI, etc.)
isDownloaded = true;
}
})
.addOnFailureListener(
new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// Model couldn’t be downloaded or other
internal error.
// ...
isDownloaded = false;
}
});
translate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isDownloaded) {
englishSpanishTranslator.translate(enteredText.getText().toString())
.addOnSuccessListener(
new OnSuccessListener<String>() {
@Override
public void onSuccess(@NonNull String
text) {
// Translation successful.
translatedText.setText(text);
}
})
.addOnFailureListener(
new OnFailureListener() {
@Override
public void onFailure(@NonNull
Exception e) {
// Error.
// ...
}
});
}
else{
Toast.makeText(MainActivity.this, "model is not downloaded",
Toast.LENGTH_SHORT).show();
}
}
});
}
}