2012-02-18 135 views
4

我有我的設置功能自定義對話框:動態設置自定義AlertDialog內容

public void customDialog(Bitmap bm, String title){ 
    TextView dialogtext = (TextView)findViewById(R.id.layout_root); 
    //For above also tried r.id.popup which is the containing file for the layout 
    ImageView dialogimage = (ImageView)findViewById(R.id.popupimage); 

    dialogtext.setText(title); 
    dialogimage.setImageBitmap(bm); 

    LayoutInflater inflater = getLayoutInflater(); 
    View layout = inflater.inflate(R.layout.popup, (ViewGroup) findViewById(R.id.layout_root)); 
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setView(layout); 
    builder.show(); 
} 

對話框沒有每當我試圖用一個空指針異常動態設置XML領域。我難過,有什麼想法?我是否需要爲清單添加一些自定義對話框?

+0

你能張貼的佈局XML文件? – koopaking3 2012-02-18 00:58:19

回答

2

首先要做的事:

View layout = inflater.inflate(R.layout.popup, (ViewGroup) findViewById(R.id.layout_root));

然後,定義layout後,這樣做:

ImageView dialogimage = (ImageView) layout.findViewById(R.id.popupimage);

呼叫findViewByID()上創建新的佈局,而不是父內容視圖。

所以兩個變化:訂貨,並layout.findViewfindView

+0

什麼是layout_root。請幫助 – 2016-04-22 19:10:11

+0

將它設置爲等於null並且它工作。謝啦 – 2016-04-22 19:16:29