开发者

Can't find xml id while inflating view

开发者 https://www.devze.com 2023-02-10 06:44 出处:网络
I\'m trying to inflate a开发者_开发问答n custom alertdialog and encountered something strange.

I'm trying to inflate a开发者_开发问答n custom alertdialog and encountered something strange.

layout = inflater.inflate(R.layout.call_or_sms_dialog,(ViewGroup)findViewById(R.id.contacts));

The inflate() method takes 2 argument, the resource to be inflated and the optional view to be the parent of the generated dialog. My problem comes at the optional view part.

I can't find the id of the root view from findViewById(R.id.contacts). "contacts" is a xml file that contains the controls for this particular activity. I was able to reference some other xml file of other activities but just couldn't reference this contacts.xml.

I've tried doing the "clean" build on Eclipse and regenerating the R.java but still it does not help. Is there any way to manually generate the ID of "contacts.xml" instead?


"contacts" is a xml file that contains the controls for this particular activity

If it's an XML, you cannot access it by using R.id; but something like R.xml or R.layout. Of course, if you are using findViewById you must pass a valid id (something referenced by R.id). So... what you have to do is give an ID to the view that you want to reference; for instance:

<ViewGroup
   android:id="@+id/contacts"
   blah

Also, keep in mind that, if you are using the findViewById method directly, the ID must be part of the current layout (I mean the layout set in setContentView). If the ID does not belong to the current layout you will want to execute something like referenceToTheViewContainingTheIDResource.findViewById() instead.


Please try this code:

LayoutInflater inflater = LayoutInflater.from(this);
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
    alertDialog.setTitle("SET YOUR TITLE");

View view = inflater.inflate(R.layout.call_or_sms_dialog, null);
v  = (ViewGroup)findViewById(R.id.contacts);
alertDialog.setView(view);


The second argument is meant to be an existing view, not the root view of the layout you are inflating. Is this the problem? You can always just pass null as the second argument.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号