【问题标题】:Launching a Custom Dialog启动自定义对话框
【发布时间】:2011-04-13 19:34:13
【问题描述】:

我遵循了有关创建自定义对话框的 Android 文档。当我单击启动自定义对话框的按钮时,应用程序。崩溃。我该如何着手解决这个问题?

对话框:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/relativeLayout1" android:gravity="center_vertical" android:layout_height="wrap_content" android:layout_width="fill_parent">
<CheckBox android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/chkBlueTooth" android:id="@+id/chkBlueTooth" android:layout_below="@+id/chkAudio" android:layout_alignLeft="@+id/chkAudio"></CheckBox>
<Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_below="@+id/chkBlueTooth" android:layout_alignLeft="@+id/chkBlueTooth" android:id="@+id/btnOK" android:text="@string/btnOK"></Button>
<CheckBox android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/chkNetwork" android:id="@+id/chkNetwork" android:layout_above="@+id/chkBlueTooth" android:layout_toRightOf="@+id/chkBlueTooth"></CheckBox>
<CheckBox android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/chkWifi" android:id="@+id/chkWifi" android:layout_below="@+id/chkNetwork" android:layout_alignLeft="@+id/chkNetwork" android:layout_alignRight="@+id/chkNetwork"></CheckBox>
<CheckBox android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/chkAudio" android:id="@+id/chkAudio" android:layout_below="@+id/txtChoose" android:layout_alignLeft="@+id/txtChoose"></CheckBox>
</RelativeLayout>

对话框的代码: 静态最终 int TIME_DIALOG_ID = 0; 静态最终 int POWER_OFF_OPTIONS = 1;

@覆盖 受保护的对话 onCreateDialog(int id) { 对话对话框; 开关(ID){ 案例 TIME_DIALOG_ID: 返回新的 TimePickerDialog(这个, mTimeSetListener, mHour24, mMinute, false);

        case POWER_OFF_OPTIONS:
            AlertDialog.Builder builder;
            AlertDialog alertDialog;

            Context mContext = getApplicationContext();
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
            View layout = inflater.inflate(R.layout.options,
                    (ViewGroup)findViewById(R.id.relativeLayout1));
            //Capture view elements
            mChkAudio = (CheckBox) findViewById(R.id.chkAudio);
            mChkBluetooth = (CheckBox) findViewById(R.id.chkBlueTooth);
            mChkNetwork = (CheckBox) findViewById(R.id.chkNetwork);
            mChkWifi = (CheckBox) findViewById(R.id.chkWifi);
            mBtnOK = (Button) findViewById(R.id.btnOK);
            mBtnOK.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                  DismissPowerOptions();
                    }
            }); 
            builder = new AlertDialog.Builder(mContext);
            builder.setView(layout);
            alertDialog = builder.create();
            //return builder;
        default:
            dialog = null;
        }
        return dialog;
    }

显示对话框并使应用程序崩溃的按钮:

  mBtnPowerOffOptions.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            //stuff for Options dialog
            showDialog(POWER_OFF_OPTIONS);
        }
    });

【问题讨论】:

  • 调试器消息是什么?您需要将代码分解为简单的操作,直到它起作用。然后添加越来越多的功能。

标签: android dialog


【解决方案1】:

您在错误的对象上调用 findViewById;父视图(或活动)。

在 builder.setView(...) 之后

AlertDialog myDialog = builder.create();

现在您可以调用 findViewById

//捕获视图元素 mChkAudio = (CheckBox) myDialog.findViewById(R.id.chkAudio); . . .

【讨论】:

  • 我从来没有真正得到这个工作,并放弃了自定义对话框。我现在可能可以让它工作,因为我有更多的 android 编程经验,但现在不需要。我所做的是建立一个自定义列表警报对话框。这里有一个类似的例子:stackoverflow.com/questions/3608018/…
猜你喜欢
  • 1970-01-01
  • 2011-06-16
  • 1970-01-01
  • 1970-01-01
  • 2019-02-23
  • 1970-01-01
  • 2012-07-22
相关资源
最近更新 更多