how to i edit contents of TextViews in Android Dialogs?
i want to display a dialog in my android application where certain TextViews in the layout need to have content set during runtime.
while showing the dialog itself is no problem at all, accessing the TextViews via TextView.setText(String) crashes the application.
this is what i got so far:
dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog);
dialog.setTitle("my dialog");
dialog.show();
TextView tv;
tv = (TextView) findViewById(R.id.title);
tv.setText("bkjadsjbk");
while this is working just fine without the last 3 lines, running that code WITH the Tex开发者_C百科tView-stuff crashes my application.
is there any way to get this right?
It will be nice if you can post the error log for us to understand.
I would guess that this is happening since it is seatching for that textview in the actual layout file from which the dialog is opened. So a null pointer exception might occur.
To explicitly tell it to search for the view that is in the dialog layout that you are using., do this for the textview.
TextView tv = (TextView) dialog.findViewById(R.id.title);
tv.setText("your text...");
If this does not work then post your error log.
You are calling findViewById()
on your Activity
. Your Dialog
has the TextViews
. Call findViewById()
on the Dialog
, and you may have better luck.
change
TextView tv = (TextView) dialog.findViewById(R.id.title);
tv.setText("your text...");
to
final TextView tv = (TextView) dialog.findViewById(R.id.title);
tv.setText("your text...");
精彩评论