Skip to content Skip to sidebar Skip to footer

Android Studio Error: String Literal In Settext Cannot Be Translated

This is my first app and I'm having some trouble. When I run the app it crashes and I don't know how to fix this error. public class MainActivity extends AppCompatActivity {\

Solution 1:

String literal in setText cannot be translated

This is not an error and you can safely ignore it (unless you need translation in your app).

It is simply a notification by Android Studio that you should be using a string resource file instead of a hard-coded string.


If I had to guess at the source of the issue since you did not post the logcat, it is this.

You can't use findViewById before setContentView because there is no view to "find".

Please try something like the following code

publicclassMainActivityextendsAppCompatActivity {

    private TextView outputBottom;    

    protectedvoidonCreate(Bundle b) {
        super.onCreate(b);
        setContentView(R.layout.activity_main);
        outputBottom = (TextView)findViewById(R.id.output);
    }

Solution 2:

There are two issues here. First, you can't use findViewById until you have "created" things and have a view to find things with, so as the previous answer you want to separate them.

publicclassMainActivityextendsAppCompatActivity {

  private TextView outputBottom;    

  protectedvoidonCreate(Bundle b) {
      super.onCreate(b);
      setContentView(R.layout.activity_main);
      outputBottom = (TextView)findViewById(R.id.output);
  }

  ...
}

To set text, it's better not to "hardcode" with a string in quotes, but to create a string file and ask for it, which will look more like this:

outputBottom.setText(R.string.my_words);

Here are the developer notes on strings.

If you're using Android Studio there are tutorials for how to make that happen.

Solution 3:

You can ignore these warning by adding

 lintOptions {
    disable'MissingTranslation'
}

to the gradle.build file.

Post a Comment for "Android Studio Error: String Literal In Settext Cannot Be Translated"