2016-11-07 57 views
-1

我創建一個自定義警告對話框添加自定義佈局空引用錯誤的警告對話框

// Biuld the dialog 
AlertDialog.Builder alert = new AlertDialog.Builder(this); 

// Create the dialog 
AlertDialog alertToShow = alert.create(); 

// Set keyboard to the dialog 
alertToShow.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); 

// Add custom layout to the dialog 
LayoutInflater inflater = this.getLayoutInflater(); 
View dialogView = inflater.inflate(R.layout.dialog_view, null); 
alert.setView(dialogView); 

// Show the dialog 
alertToShow.show(); 

但是從上面,我加我的自定義佈局

f1.addView(inflater.inflate(R.layout.dialog_view, f1, false)); 

導致錯誤的下面一行

嘗試在空對象引用上調用虛擬方法'void android.widget.FrameLayout.addView(android.view.View)'

任何想法如何解決錯誤?

回答

0

您需要在顯示對話框或佈局膨脹後找到視圖。在此之前

// Add custom layout to the dialog 
LayoutInflater inflater = getLayoutInflater(); 
View dialogView = inflater.inflate(R.layout.dialog_view, f1, false) 
FrameLayout f1 = (FrameLayout) dialogView.findViewById(android.R.id.custom); 
f1.addView(dialogView); 

// Show the dialog 
alertToShow.show(); 

不過,我想你想alertToShow.setView(dialogView),也許

0

我有我創建對話框

// Add custom layout to the dialog 
    LayoutInflater inflater = this.getLayoutInflater(); 
    View dialogView = inflater.inflate(R.layout.dialog_view, null); 
    alert.setView(dialogView); 

    // Create the dialog 
    AlertDialog alertToShow = alert.create(); 

    // Set keyboard to the dialog 
    alertToShow.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); 

    // Show the dialog 
    alertToShow.show(); 
前加意見