Dennis
Dennis

Reputation: 41

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"
/>

Upvotes: 4

Views: 2021

Answers (1)

Labeeb Panampullan
Labeeb Panampullan

Reputation: 34823

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"); 

    }

Upvotes: 11

Related Questions