简体   繁体   中英

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

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

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM