设置文本时TextView崩溃

时间:2010-11-12 04:29:17

标签: java android textview settext

由于某些原因,当我尝试设置textview的文本时,我的应用程序崩溃了?我怎么能过来这个。

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

1 个答案:

答案 0 :(得分:11)

一个小错误,你应该先做setContentView

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

    }