【问题标题】:Using Radio buttons within a Dialog box在对话框中使用单选按钮
【发布时间】:2012-03-14 19:38:34
【问题描述】:

我希望能够让用户选择选项,并希望在对话框中提供一些单选按钮。我已经在 OnCreate 部分声明了这样的单选按钮

@Override
protected Dialog onCreateDialog(int id) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    if (id > 0)
    {
        LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View layout = layoutInflater.inflate(R.layout.lectsort_dialog, (ViewGroup) findViewById(R.id.lect_sort));

                builder.setView(layout);

        // Now configure the AlertDialog
        builder.setTitle(R.string.exsort_title);

        radio_date = (RadioButton) findViewById(R.id.RBdate);
        radio_loctn = (RadioButton) findViewById(R.id.RBloctn);
        radio_stream = (RadioButton) findViewById(R.id.RBstream);
        radio_date.setOnClickListener(radio_listener);
        radio_loctn.setOnClickListener(radio_listener);
        radio_stream.setOnClickListener(radio_listener);

        builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface arg0, int arg1) {
             }

radio_listener 过程是这样声明的

RadioButton.OnClickListener radio_listener =
       new RadioButton.OnClickListener()
      {
      @Override
      public void onClick(View v) {
          // Perform action on clicks
            RadioButton rb = (RadioButton) v;
            Toast.makeText(LecturesActivity.this, rb.getText(), Toast.LENGTH_SHORT).show();         
      }
};

但是当对话框被调用时,我在这一行得到一个 Null 异常错误

radio_date.setOnClickListener(radio_listener);

我做错了什么?

【问题讨论】:

    标签: android eclipse radio-button radio-group


    【解决方案1】:

    检查以下代码 这是单击按钮时带有单选按钮的警报

    final AlertDialog.Builder  builder = new AlertDialog.Builder(this);
    
    btn.setOnClickListener(new OnClickListener() {
    
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //int states = {false, false};
                builder.setTitle("Select the item  that you want to delete from that item ");
                builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
    
                    public void onClick(DialogInterface dialog, int id) {
    
                        // TODO Auto-generated method stub
                        if(id==0){
    
                        }
                        else
                        {
    
                });
    
                alert= builder.create();
                alert.show();
            }
        });
    

    你试试这个然后告诉我

    【讨论】:

      【解决方案2】:

      当您想要获取位于对话框中的 UI 元素的 ID 时,您需要这样做。

        Dialog dialog = new Dialog(mContext); // your dialog creation method here
        RadioButton radio_date = (RadioButton) dialog.findViewById(R.id.RBdate);
      

      如果您使用findViewById,那么您正在尝试捕获与当前活动视图关联的视图对象(您可以在setContentView API 中设置)

      所以它试图在它无法找到的活动视图中查找 ID 为 RBdate 的视图,因此返回 null。

      【讨论】:

      • 感谢您的回复,我确定这是正确的。但是我添加了以下代码 Context mContext = getApplicationContext();对话框对话框=新对话框(mContext); RadioButton radio_date = (RadioButton) dialog.findViewById(R.id.RBdate);但是我还是报了 Null 异常错误,是不是我声明的地方不对?
      • 你确定你在同一个地方得到了异常(对于 RBdate)?或不同的单选按钮。在调试模式下运行您的应用程序并逐步找出确切位置
      • 是的,我在这里收到错误 - radio_date.setOnClickListener(radio_listener);而且如果我尝试不同的方式,我会在这里得到错误 - if (radio_date.isChecked())
      • 你是对的 Ravi,我又经历了一遍,当我将代码更改为 RadioButton radio_date = (RadioButton) layout.findViewById(R.id.RBdate);有效。感谢您的帮助。
      • 天啊他妈的!!!!在类似的对话问题上浪费了 4 小时,都是因为没有使用 dialog.findViewById!有人应该对此提出很大的免责声明! (在其他任何地方都没有关于这个的点子)
      猜你喜欢
      • 1970-01-01
      • 2011-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-13
      • 2014-03-11
      • 2023-04-06
      • 1970-01-01
      相关资源
      最近更新 更多