Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

Image

public class MainActivity extends AppCompatActivity {\
    TextView outputBottom = (TextView)findViewById(R.id.output);

}
public void play_the_big_lie(View view) {
    the_big_lie.start();
    outputBottom.setText("ObamaCare, the big lie");
}
like image 756
Dynamic Index Avatar asked Feb 07 '16 21:02

Dynamic Index


People also ask

What is the use of SetText in Android?

SetText(String, TextView+BufferType)Sets the text to be displayed using a string resource identifier.

What is@ string in Android Studio?

A string resource provides text strings for your application with optional text styling and formatting. There are three types of resources that can provide your application with strings: String. XML resource that provides a single string.

How to write special characters in string XML in Android?

To include special characters inside XML files you must use the numeric character reference instead of that character. The numeric character reference must be UTF-8 because the supported encoding for XML files is defined in the prolog as encoding="UTF-8" and should not be changed.

What is string XML file?

String. xml file contains all the strings which will be used frequently in Android project. String. xml file present in the values folder which is sub folder of res folder in project structure.In Android Studio, we have many Views such as TextView,Button,EditText,CheckBox,RadioButton etc.


2 Answers

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

public class MainActivity extends AppCompatActivity {

    private TextView outputBottom;    

    protected void onCreate(Bundle b) {
        super.onCreate(b);
        setContentView(R.layout.activity_main);
        outputBottom = (TextView)findViewById(R.id.output);
    }
like image 174
OneCricketeer Avatar answered Oct 27 '22 09:10

OneCricketeer


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.

public class MainActivity extends AppCompatActivity {

  private TextView outputBottom;    

  protected void onCreate(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.

like image 25
geonz Avatar answered Oct 27 '22 08:10

geonz