When is Context to Activity listed?

In the showAlret(String message, Context ctx)warning Dialog class method, I am trying to get a link to the TextView in the XML dialog format:

TextView tv = (TextView)((MyActivity)ctx).findViewById(R.id.tv_about);

Not until calling inflate (), of course:

LayoutInflater li = LayoutInflater.from(ctx);
View view = li.inflate(R.layout.aboutdialog, null);

The problem is that this call returns tvas null .

Since the code compiles without any warnings, I assume this is due to the “illegal” casting of Context to MyActivity.

I came across quite a few cases where Context to Activity casting is the only thing that really works, so why doesn't it work in this case?

When does Context to Activity listing make sense?

What are the “unspoken rules" in this regard?

+3
4
LayoutInflater li = LayoutInflater.from(ctx);
View view = li.inflate(R.layout.aboutdialog, null);
TextView tv = view.findViewById(R.id.tv_about);

, xml, findViewById() .

+3

, . , hs TextView xml , ?

+1

. , :

TextView tv = (TextView)((MyActivity)ctx).findViewById(R.id.tv_about);

:

TextView tv = (TextView)view.findViewById(R.id.tv_about);
+1

, Activity, Activity. - Activity.

, Context. - , , , , , .

0

All Articles