我有一个 EditText 视图和一个复选框(隐藏),以及两个单选按钮(隐藏和显示)。按钮和复选框的作用是隐藏或显示 EditView 的提示。我想第一个问题是为什么我首先要这样做。好吧,我对 android 完全陌生,我只是在尝试不同的东西。
但是,当我运行代码时,活动无法启动。我什至在放置任何单选按钮之前测试了该程序,并且仅使用程序中的复选框就可以正常工作。所以我假设问题出在单选按钮的某个地方,或者两者的组合用于同一任务。
这是我的代码:
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class EmPubLiteActivity extends Activity implements
CompoundButton.OnCheckedChangeListener {
CheckBox cb;
EditText et;
RadioButton rbHide;
RadioButton rbShow;
RadioGroup rg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
cb = (CheckBox)findViewById(R.id.cbHideHint);
cb.setOnCheckedChangeListener(this);
et = (EditText)findViewById(R.id.editText1);
rbHide = (RadioButton)findViewById(R.id.rbHide);
rbShow = (RadioButton)findViewById(R.id.rbShow);
RadioGroup rg = new RadioGroup(this);
rg.addView(rbHide);
rg.addView(rbShow);
rbHide.setOnCheckedChangeListener(this);
rbShow.setOnCheckedChangeListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.em_pub_lite, menu);
return true;
}
@Override
public void onCheckedChanged(CompoundButton view, boolean isChecked) {
switch (view.getId()) {
case R.id.cbHideHint:
if (isChecked) {
et.setHint(R.string.nothing);
rbHide.setChecked(true);
}
else {
et.setHint(R.string.text_here);
rbShow.setChecked(true);
}
break;
case R.id.rbHide:
et.setHint(R.string.nothing);
cb.setChecked(false);
break;
case R.id.rbShow:
et.setHint(R.string.text_here);
cb.setChecked(true);
}
}
}
我收到以下错误以及其他一些错误,但我觉得这就是问题所在:
java.lang.IllegalStateException: the specified child already has a parent.
You must call removeView() on the child's parent first.
这个错误是什么意思,我该如何解决?