我正在尝试将我的edittext设置为隐藏,当我打开对话框但是当我选中复选框时它应该可见...这个dialop在我的选项选择菜单中 不幸的是我得到了这个例外
07-31 15:23:31.129:E / AndroidRuntime(4143):致命异常:主要 07-31 15:23:31.129:E / AndroidRuntime(4143):java.lang.NullPointerException 07-31 15:23:31.129:E / AndroidRuntime(4143):at com.EVS.homeautomation.MainActivity.onOptionsItemSelected(MainActivity.java:1153)
这是代码
final Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.testing);
dialog.setTitle("Configurations");
final EditText up_num=((EditText)findViewById(R.id.unum));
up_num.setVisibility(View.VISIBLE);
CheckBox cb= ((CheckBox) findViewById(R.id.checkBox1));
cb.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
up_num.setVisibility(View.VISIBLE);
// perform logic
}
else
{
up_num.setVisibility(View.GONE);
}
}
});
答案 0 :(得分:0)
您使Dialog中的EditText无效。因此您必须使用以下方式初始化此EditText:
EditText up_num=((EditText) dialog.findViewById(R.id.unum));
由于
答案 1 :(得分:0)
首先,您需要对视图进行充气,然后才能调用setContentView()
所以你可以尝试:
final Dialog dialog = new Dialog(MainActivity.this);
LayoutInflater inflater1 = this.getLayoutInflater();
final View customview = inflater1.inflate(
R.layout.testing, null);
dialog.setContentView(customview);
dialog.setTitle("Configurations");
final EditText up_num=((EditText)findViewById(R.id.unum));
up_num.setVisibility(View.VISIBLE);
CheckBox cb= ((CheckBox) findViewById(R.id.checkBox1));
cb.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
up_num.setVisibility(View.VISIBLE);
// perform logic
}
else
{
up_num.setVisibility(View.GONE);
}
}
});
我希望这会对你有所帮助:)。