DialogはfindViewByIdを使用して空のポインタ異常を報告する
2717 ワード
問題の説明
Dialogの作成が完了したら、DialogのTextViewの値を変更して、ポインタ異常を報告します.
問題の原因
注記は、dialogがまだ完全に作成されていない場合(
問題解決
コード実行順序を調整し、
Dialogの作成が完了したら、DialogのTextViewの値を変更して、ポインタ異常を報告します.
Dialog dialog = new CustomDialog(context); // Dialog
TextView textView=dialog.findViewById(R.id.textView1); //
textView.setText(" UI ")
dialog.show();
問題の原因
Dialog#findViewById
ソースコードの表示 /**
* Finds a child view with the given identifier. Returns null if the
* specified child view does not exist or the dialog has not yet been fully
* created (for example, via {@link #show()} or {@link #create()}).
*
* @param id the identifier of the view to find
* @return The view with the given id or null.
*/
public View findViewById(int id) {
return mWindow.findViewById(id);
}
注記は、dialogがまだ完全に作成されていない場合(
show()
またはcreate()
メソッドが呼び出されていない場合)は、null
に戻ります.問題解決
コード実行順序を調整し、
Dialog#show()
メソッドを呼び出してからDialog#findViewById
メソッドを呼び出す Dialog dialog = new CustomDialog(context); // Dialog
dialog.show();
TextView textView=dialog.findViewById(R.id.textView1);
textView.setText(" UI ")