2013-05-21 67 views
0

尝试膨胀AlertDialog.Builder。我试图让日期轮(Yuri Kanivets的轮子)出现在我的对话框中。由于我需要的确切代码存在于他的一个类中,我只是试图实例化他的DateActivity类(我已经导入到我的项目中)的一个新实例,然后将其添加到我的对话框中。不幸的是,我似乎无法将我的DateActivity对象与我的对话框连接起来。我认为这将是我夸大观点的论据之一,但是这会导致崩溃。这是我的代码:尝试使用AlertDialog.Builder的类时发生空指针错误

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

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

感谢您的任何建议。

+0

该片段看起来不错。发布logcat错误 – Blackbelt

+0

对不起,我不清楚。看到我上面的编辑。谢谢! – Alex

+0

你有没有NPE? – Blackbelt

回答

2

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

+0

或者你也可以重构你的DateActivity成为一个自定义的视图,并将其添加一次到一个活动,另一次添加到一个对话框。 – SimonSays

+0

好的。谢谢。我会研究这些。我担心可能会是这样。 – Alex

相关问题