TextView.setText (); glitch

The setText () method returns null in my application, why?

public class GetValue extends Activity {
    char letter = 'g';
    int ascii = letter;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TextView textView = (TextView)findViewById(R.id.txt_1);
            textView.setText(ascii);
    }
}

It doesn’t matter what text I insert, it still crashes. Why does setText () return null?

Thanks in advance

Solution: My error was in an XML file. I wrote: Android: text = "@ + identifier / txt _1" When it should say: android: identifier = "@ + identifier / txt _1"

Thank you very much for the answers and comments!

+3
source share
3 answers

You tried to pass an integer as a parameter to setText, which assumes this is a resource identifier. To display the calculated text, pass it as a string:textView.setText("g");

: XML , - ,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/txt_1"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="myTextView"/>
</LinearLayout>

, (Project- > Clean in Eclipse), R- ADT.

+8

:

textView.setText(Integer.toString(ascii)); 

, xml TextView txt_1

+5

I tried:

Integer.toString(char) and String.valueOf(char)

Both did not work. Only decision:

txt.setText(""+char);

This is not very efficient in terms of optimization, but it does the trick :)

+2
source

All Articles