尝试使用AlertDialog.Builder类时,Null Pointer错误

时间:2013-05-21 16:17:11

标签: android android-widget scrollwheel

试图给AlertDialog.Builder充气。我正试图让日期轮(Yuri Kanivets的轮子)显示在我的对话框中。由于我需要的确切代码存在于他的一个类中,我只是试图实例化他的DateActivity类的新实例(我已导入到我的项目中),然后将其添加到我的对话框中。不幸的是,我似乎无法将我的DateActivity对象与我的对话框连接起来。我认为这将是我膨胀视图的论据之一,但崩溃了。这是我的代码:

编辑:澄清一下,在下面的代码中没有错误。我提到的问题是我的DateActivity变量与AlertDialog.Builder没有用处,因此没有关联。我已经尝试使用该变量(dateWheelSelector)作为builderView的参数以及构建器变量实例化,但这两个都崩溃了。我需要弄清楚如何连接它们,因为我现在的对话框是空的。

private void setStartDate() {

    //somehow I need to use this variable, but where???
    DateActivity dateWheelSelector = new DateActivity();

    LayoutInflater inflater = LayoutInflater.from(this);

    View builderView = inflater.inflate(R.layout.wheel_date_layout, null);
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setView(builderView);       
    alert = builder.create();

    /* Set the title of this dialog programatically */
    TextView title = (TextView) builderView.findViewById(R.id.date_title);
    title.setText("Choose Start Date");

    alert.show();
}

感谢您的任何建议。

1 个答案:

答案 0 :(得分:2)

您无法将活动添加到对话框中。您可以将Activity定义为Dialog(参见Android Activity as a dialog),也可以将DateActivity重构为DialogFragment(参见http://developer.android.com/reference/android/app/DialogFragment.html),可以用作Fragment或Dialog。