2011-09-15 9 views
0

私はthe Android tutorialを非常によく辿ったと思います。私はある時点でshowDialog(DIALOG_EXPORT);を呼び出すListActivityを持っています。私のonCreateDialog()は、ダイアログを作成し、XMLビューを設定し、そのダイアログの要素で何かをしようとしますが、findViewById()の直後はすべてnullです。どうして?Android:ダイアログを作成する

ここでコード:

protected Dialog onCreateDialog(int id) { 
    switch(id){ 
    case DIALOG_EXPORT: 
     final Dialog dial = new Dialog(this); 
     dial.setContentView(R.layout.export_dialog); 
     dial.setTitle(R.string.dialog_export_title); 
     EditText eFile = (EditText) findViewById(R.id.e_dialog_export); 
     Button bOkay = (Button) findViewById(R.id.b_export_okay); 
     Button bCancel = (Button) findViewById(R.id.b_export_cancel); 
     <here all View elements are empty> 
     ... 
     return dial; 

    ... 
    } 
} 
+0

すべてが有効です。レイアウトXMLを貼り付けてください。 –

答えて

2

あなたはビューを膨らませるために必要なだけのfindViewById()

+0

私は目の非常に単純なエラーと思った。ありがとう! – erikbwork

0

あなたはチュートリアルあたりのダイアログのレイアウトを膨らませるのを忘れていました。それをもう一度見てください。それはそこにある。レイアウトを膨らませることなく、他のビューはnullに戻ります。

1

dial.findViewById()の代わりを使用する必要があります。

@Override 
protected Dialog onCreateDialog(int id) { 

    LayoutInflater inflator = LayoutInflater.from(context); 
    View view = inflator.inflate(R.layout.yourview, null); 
    Button positive = (Button)view.findViewById(R.id.btn_positive); 
    Button negative = (Button)view.findViewById(R.id.btn_negative); 

    positive.setOnClickListener(new Button.OnClickListener(){ 
     public void onClick(View v) { 
      removeDialog(0); 
     } 
    }); 

    negative.setOnClickListener(new Button.OnClickListener(){ 
     public void onClick(View v) { 
      removeDialog(0); 
     } 
    }); 

    AlertDialog dialog = new AlertDialog.Builder(this).create(); 
    dialog.setView(view); 

    return dialog; 
} 
関連する問題