-1

I have noticed a weird thing in Android. I can't use EditText functions like getText() - for example, when I type:

final EditText txtUser = (EditText) inflater.inflate(R.layout.layout_dialog, null).findViewById(R.id.txtUsername);

And when I use:

View v = inflater.inflate(R.layout.layout_dialog, null);
final EditText txtUser = (EditText) v.findViewById(R.id.txtUsername);

everything works fine. I noticed that inflater.inflate(R.layout.layout_dialog, null); returns view, so I don't understand why I can't use getText() function when I don't use a view instance.

Thank You.

Sam Starling
  • 5,298
  • 3
  • 35
  • 52
zb22
  • 3,126
  • 3
  • 19
  • 34

1 Answers1

0

You are inflating Custom dialog. So in order to use its component, you first need to initialise it as View. If you want to use method of other class, you must create instance of that class first. In first code you have not declared inflater.inflate(R.layout.layout_dialog,null); as type View, so compiler doesn't know what you are intended to use.

But in second code, you have declared your inflater.inflate(R.layout.layout_dialog,null); as View type so you can use method that is applicable for View class.

Crawler
  • 1,988
  • 3
  • 21
  • 42