Problem with cusom Dialog for ProgressDialog
I have a LoginDialog called from Activity. In LoginDialog I am trying to open another Dialog (mProgressDlg) to indicate the progress. In mProgressDlg layout I have 2 TextView. I get NullPointerException
on setText()
of any of those TextView
.
LoginDialog :
private Dialog mProgressDlg;
private TextView mMessage, mprogTitle, mProgMessage;
public LoginDialog(Context context) {
super(context);
mContext = context;
ld = null;
init();
}
private void init() {
this.setContentView(R.layout.user_pass_dialog);
mMessage = (TextView) findViewById(R.id.messageText);
mMessage.setText("Loaded ");
// ProgressDialog
mProgressDlg = new Dialog(mContext);
mProgressDlg.setContentVie开发者_如何学Gow(R.layout.progress_dialog);
mProgTitle = (TextView) findViewById(R.id.titleText_progressDlg);
mProgMessage = (TextView) findViewById(R.id.message_progressDlg);
// On below 2 lines, I get NPE.
mProgTitle.setText("Hello");
mProgMessage.setText("Good Day");
}
progressDialog.xml
<TextView android:id="@+id/titleText_progressDlg" android:text="TextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:layout_marginTop="10dp" android:textStyle="bold"></TextView>
<TextView android:id="@+id/message_progressDlg" android:text="TextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:layout_marginTop="10dp"></TextView>
LogCat :
04-06 14:54:29.087: ERROR/AndroidRuntime(379): Caused by: java.lang.NullPointerException
04-06 14:54:29.087: ERROR/AndroidRuntime(379): at orange.android.vpn.LoginDialog.init(LoginDialog.java:88) ***// THIS IS mProgTitle.setText("Hello");***
04-06 14:54:29.087: ERROR/AndroidRuntime(379): at orange.android.vpn.LoginDialog.<init>(LoginDialog.java:50) ***// THIS IS CALLING init() FROM CONSTRUCTOR***
Can anyone help me know, why am I getting this NullPointerException
in init()
while accessing mProgTitle
and/or mProgMessage
. Any help is highly appreciated.
Thanks
mProgressDlg.setContentView(R.layout.progress_dialog);
mProgTitle = (TextView) findViewById(R.id.titleText_progressDlg);
should be
mProgressDlg.setContentView(R.layout.progress_dialog);
mProgTitle = (TextView) mProgressDlg.findViewById(R.id.titleText_progressDlg);
because you are accessing components in the dialog layout. just findViewById is used in as a function in activity because you: with setContentView({layout}) you set activity's layout and access components with findViewById, the same for dialog you type dialog.setContentView and you access components the samo diloag.findViewById
精彩评论