单击按钮后更新 EditText
此 EditText 位于 Activity 内,该 Activity 是主 Activity 内 TabHost 的一部分。它应该是 4 个选项卡,每个选项卡上有一个 EditText 和两个按钮,一个用于递增 EditText 字段,一个用于递减 EditText 字段。但是,如果我尝试在其中一个 EditText 框上 setText() ,应用程序就会崩溃。因此,当我在 onCreate() 中调用 setText() 时,它会崩溃。任何帮助将不胜感激!
<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);
}
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的 xml 中,更改
to
In your xml, change
to
当您在引用的代码中绑定 EditBox 时,
这意味着 EditBox 需要一个 ID。
如果您将
android:label
更改为android:id
您的代码将正常运行When you bind your EditBox in code you reference to
This means the EditBox needs an ID.
If you change the
android:label
to anandroid:id
your code will run fine