2016-08-17 63 views
0

对话第二次我有这样的代码错误时显示在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(); 请帮助我..

回答

1

与您的代码的问题是,你正在创建每个项目点击新AlertDialog和对话框中有一个自定义视图。但是,该自定义视图只有一个实例(您不会重新创建它)。因此,您试图将相同的自定义视图v添加到不允许的多个对话框中。根据您的使用情况,我改变了代码,也重构了一点:

public class Activity_listview extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_activity_listview); 

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

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

        // inflating custom alert view 
        LayoutInflater inflater = getLayoutInflater(); 
        final View v = inflater.inflate(R.layout.dialog_radiobutton, null); 
        final RadioGroup radioGroup_decimalformat = (RadioGroup) v.findViewById(R.id.radioGroup_decimalformat); 

        dialog.setView(v); 
        dialog.setTitle("Number of decimal format"); 
        dialog.setPositiveButton("Done", new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialog, int which) { 
          // saving only on Done click 
          int checkedId = radioGroup_decimalformat.getCheckedRadioButtonId(); 

          if (checkedId != -1) 
           modifyPreferenceOnCheck(); 
          dialog.cancel(); 
         } 
        }); 

        dialog.show();  //here the error 
       } 
      } 
     }); 
    } 


    // method to save the preference values 
    private void modifyPreferenceOnCheck(int checkedId) { 
     final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); 
     final SharedPreferences.Editor editor = preferences.edit(); 

     switch (checkedId) { 
      case R.id.radioButton_decimal1: 
       editor.putInt("decimal_key", 1); 
       break; 
      case R.id.radioButton_decimal2: 
       editor.putInt("decimal_key", 2); 
       break; 
      case R.id.radioButton_decimal3: 
       editor.putInt("decimal_key", 3); 
       break; 
      case R.id.radioButton_decimal4: 
       editor.putInt("decimal_key", 4); 
       break; 
      case R.id.radioButton_decimal5: 
       editor.putInt("decimal_key", 5); 
       break; 
      case R.id.radioButton_decimal6: 
       editor.putInt("decimal_key", 6); 
       break; 
      case R.id.radioButton_decimal7: 
       editor.putInt("decimal_key", 7); 
       break; 
      case R.id.radioButton_decimal8: 
       editor.putInt("decimal_key", 8); 
       break; 
      case R.id.radioButton_decimal9: 
       editor.putInt("decimal_key", 9); 
       break; 
      case R.id.radioButton_decimal10: 
       editor.putInt("decimal_key", 10); 
       break; 
      default: 
       editor.putInt("decimal_key", 4); 
     } 

     editor.apply(); 
    } 

} 

让我知道如果你遇到任何问题。

+0

其工作,非常感谢:) –

0

试试这个:

dialog.setPositiveButton("Done", new DialogInterface.OnClickListener() { 
    @Override 
    public void onClick(DialogInterface dialog, int which) { 
     ... 
     dialog.dismiss(); 
    } 
}); 
dialog.setCancelable(true); 
dialog.show();