Bruce
Bruce

Reputation: 1

Update EditText after Button is clicked

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

Upvotes: 0

Views: 2417

Answers (2)

Mark Mooibroek
Mark Mooibroek

Reputation: 7696

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

Upvotes: 0

iamtheexp01
iamtheexp01

Reputation: 3466

In your xml, change

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

Upvotes: 1

Related Questions