嘗試膨脹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();
}
感謝您的任何建議。
該片段看起來不錯。發佈logcat錯誤 – Blackbelt
對不起,我不清楚。看到我上面的編輯。謝謝! – Alex
你有沒有NPE? – Blackbelt