4

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"
/>
Josh Lee
  • 171,072
  • 38
  • 269
  • 275
Dennis
  • 41
  • 2

1 Answers1

11

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

    }
Labeeb Panampullan
  • 34,521
  • 28
  • 94
  • 112
  • 4
    @Dennis: As this seems to be your first StackOverflow question I feel it could be a good idea to tell you that if an answer was right and has helped you then you should mark it so and accept it. That is click on the check to do that. – Octavian Helm Nov 12 '10 at 07:13