对话第二次我有这样的代码错误时显示在android系统
public class Activity_listview extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_listview);
LayoutInflater inflater=getLayoutInflater();
final View v=inflater.inflate(R.layout.dialog_radiobutton,null);
final RadioGroup radioGroup_decimalformat= (RadioGroup)v.findViewById(R.id.radioGroup_decimalformat);
final RadioButton radioButton_decimal1=(RadioButton)findViewById(R.id.radioButton_decimal1);
final RadioButton radioButton_decimal2=(RadioButton)findViewById(R.id.radioButton_decimal2);
final RadioButton radioButton_decimal3=(RadioButton)findViewById(R.id.radioButton_decimal3);
final RadioButton radioButton_decimal4=(RadioButton)findViewById(R.id.radioButton_decimal4);
final RadioButton radioButton_decimal5=(RadioButton)findViewById(R.id.radioButton_decimal5);
final RadioButton radioButton_decimal6=(RadioButton)findViewById(R.id.radioButton_decimal6);
final RadioButton radioButton_decimal7=(RadioButton)findViewById(R.id.radioButton_decimal7);
final RadioButton radioButton_decimal8=(RadioButton)findViewById(R.id.radioButton_decimal8);
final RadioButton radioButton_decimal9=(RadioButton)findViewById(R.id.radioButton_decimal9);
final RadioButton radioButton_decimal10=(RadioButton)findViewById(R.id.radioButton_decimal10);
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
final SharedPreferences.Editor editor;
editor =preferences.edit();
final int decimal;
ListView listView=(ListView)findViewById(R.id.listview);
String list_setting []={"number of decimal format"};
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,android.R.id.text1,list_setting);
listView.setAdapter(adapter);
radioGroup_decimalformat.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.radioButton_decimal1:
editor.putInt("decimal_key",1);
editor.commit();
break;
case R.id.radioButton_decimal2:
editor.putInt("decimal_key",2);
editor.commit();
break;
case R.id.radioButton_decimal3:
editor.putInt("decimal_key",3);
editor.commit();
break;
case R.id.radioButton_decimal4:
editor.putInt("decimal_key",4);
editor.commit();
break;
case R.id.radioButton_decimal5:
editor.putInt("decimal_key",5);
editor.commit();
break;
case R.id.radioButton_decimal6:
editor.putInt("decimal_key",6);
editor.commit();
break;
case R.id.radioButton_decimal7:
editor.putInt("decimal_key",7);
editor.commit();
break;
case R.id.radioButton_decimal8:
editor.putInt("decimal_key",8);
editor.commit();
break;
case R.id.radioButton_decimal9:
editor.putInt("decimal_key",9);
editor.commit();
break;
case R.id.radioButton_decimal10:
editor.putInt("decimal_key",10);
editor.commit();
break;
default:
editor.putInt("decimal_key",4);
editor.commit();
}
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(position==0){
final AlertDialog.Builder dialog=new AlertDialog.Builder(Activity_listview.this);
dialog.setView(v);
dialog.setTitle("Number of decimal format");
dialog.setPositiveButton("Done", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
dialog.show(); //here the error
}
}
});
}
}
XML的视图的对话框
当我在项目点击从列表视图中首次出现对话框 但当点击第二次停止应用程序,并给我这个错误
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
地方错误的:dialog.show(); 请帮助我..
其工作,非常感谢:) –