开发者

Update EditText after Button is clicked

开发者 https://www.devze.com 2023-02-25 05:57 出处:网络
This EditText is inside an Activity that is part of a TabHost within the main Activity. It\'s just supposed to be 4 tabs with an EditText and two Buttons on each, one to increment and one to decrement

This EditText is inside an Activity that is part of a TabHost within the main Activity. It's just supposed to be 4 tabs with an EditText and two Buttons on each, one to increment and one to decrement the EditText field. However, if I ever try to setText() on one of the EditText boxes, the app crashes. So when I call setText() in onCreate() it crashes. Any help would be greatly appreciated!

<EditText
    android:label="@+id/LifeForP1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:autoText="true"
    android:cursorVisible="false"
    android:background="@null"
    android:textColor="#999999"
    android:color="@null"
    android:layout_x="90px"
    android:layout_y="0px"
    android:textSize="250px"
    android:maxLength="3"
    android:capitalize="sentences"
    android:layout_weight="1"
    android:freezesText="true"
    android:text="20"
/>

public class ActivityTab1 extends Activity {

private EditText lifeView;
int p1Life = 20;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.content1layout);

    lifeView = (EditText) findViewById(R.id.LifeForP1);
    lifeView.setText(getString(R.string.lifeStart)); //Error here
}

@Override
protected void onResume() {
    super.onResume();
}

public void p1GainLifeListener(View 开发者_运维问答view) {
    AlertDialog alertDialog = new AlertDialog.Builder(this).create();
    alertDialog.setTitle("test gain 1");
    alertDialog.show();
    //String show = String.format("", Integer.toString(++p1Life));
    //lifeView.setText(show);
}


In your xml, change

android:label="@+id/LifeForP1"
to
android:id="@+id/LifeForP1"


When you bind your EditBox in code you reference to

findViewById(R.id.LifeForP1);

This means the EditBox needs an ID.

If you change the android:label to an android:id your code will run fine

0

精彩评论

暂无评论...
验证码 换一张
取 消