Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextView Crashes When Setting Text

For some reason my application crashes when I try to set the text of a textview? How can I over come this.

private TextView strStatus;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
    txtUsername = (EditText) findViewById(R.id.username);
    strStatus = (TextView) findViewById(R.id.status);
    //strStatus.setText("test"); // SEEMS TO BE CAUSING THE CRASH
    setContentView(R.layout.main);   
}
<TextView
android:id="@+id/status"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
like image 527
Dennis Avatar asked Dec 21 '22 21:12

Dennis


1 Answers

One small mistake , You should do that setContentView first

  @Override
    public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        txtUsername = (EditText) findViewById(R.id.username);
        strStatus = (TextView) findViewById(R.id.status);
        strStatus.setText("test"); 

    }
like image 168
Labeeb Panampullan Avatar answered Dec 24 '22 09:12

Labeeb Panampullan