在android中第二次显示对话框时出错

时间:2016-08-17 11:01:20

标签: android

我有这个代码

StopAllCoroutines();

对话框的xml视图

当我第一次点击列表视图中的项目时出现对话框 但是当第二次点击停止应用并给我这个错误时

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

错误的地方:dialog.show(); 请帮帮我..

2 个答案:

答案 0 :(得分: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();
    }

}

如果您遇到任何问题,请告诉我。

答案 1 :(得分:0)

试试这个:

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