In a showAlret(String message, Context ctx) method of an alert Dialog class, I am trying to get a reference to TextView in the dialog’s layout XML:
TextView tv = (TextView)((MyActivity)ctx).findViewById(R.id.tv_about);
Not before calling inflate() of course:
LayoutInflater li = LayoutInflater.from(ctx);
View view = li.inflate(R.layout.aboutdialog, null);
The problem is that this call returns tv as null.
Since the code compiles without any warnings, I am assuming that this is due to “illegal” casting of Context to MyActivity.
I have encountered quite a few cases in which casting Context to Activity is the only thing that really works, so why doesn’t this work in this case?
When does casting Context to Activity make sense?
What are the “unspoken rules” in this regard?
I understand that your textview is a part of the dialog xml so, you need to findViewById() on the dialogs view instead of the activity.