2013-05-21 65 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

相關問題