Can one not use @+id with ArrayAdapter layout XML file?

by blindfold » Sun, 28 Dec 2008 17:03:56 GMT


Sponsored Links
 Not using @+id through

ArrayAdapter<String> MyList = new ArrayAdapter<String>(this,
R.layout.mylist);

works fine, but using @+id through

ArrayAdapter<String> MyList = new ArrayAdapter<String>(this,
R.id.mylist);

does not, where my mylist.xml reads

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android=" http://schemas.android.com/apk/res/android" ;
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:gravity="center_vertical"
    android:paddingLeft="6dip"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:id="@+id/mylist"
/>

It all compiles just fine, but at runtime I get

ERROR/AndroidRuntime(1356): android.content.res.Resources
$NotFoundException: Resource ID #0x7f06001a type #0x12 is not valid

Am I missing some conceptual issue with the use of ArrayAdapter or the
way @+id gets expanded? Of course I can just use R.layout.mylist, but
it feels a bit inconsistent as I use the R.id referencing in my source
code with many other items (albeit through findViewById()).

Thanks

--~--~---------~--~----~------------~-------~--~----~



Can one not use @+id with ArrayAdapter layout XML file?

by Mark Murphy » Sun, 28 Dec 2008 21:07:53 GMT


 


R.layout points to layout resources. Layout resources are used whenever
Android wants to inflate a tree of Views, such as setContentView(),
manual inflation via getLayoutInflater(), etc.

R.id points to individual widgets (Views) within an inflated layout.
R.id values have no meaning outside the context of a specific layout --
in other words, R.id.fu means nothing on its own, but
myInflatedView.findViewById(R.id.fu) might, if myInflatedView was
inflated from a layout resource that had something with
android:id="@+id/fu".

In the documentation for the ArrayAdapter constructor you're using, the
second parameter is described as:

"The resource ID for a layout file containing a TextView to use when
instantiating views."

Hence, it is expecting something in the R.layout space ("resource ID for
a layout file").

-- 
Mark Murphy (a Commons Guy)
 http://commonsware.com 
_The Busy Coder's Guide to Android Development_ Version 1.9 Available!

--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


Can one not use @+id with ArrayAdapter layout XML file?

by blindfold » Mon, 29 Dec 2008 08:42:50 GMT


 Thanks a lot for your clarification, Mark! Very lucid.

Regards





--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. Bluetooth functionality to work in Emulator

Hi,

I am working on a assignment for which the hardware is still not ready
and using the android emulator for development purpose.I wanted to use
the bluetooth in emulator using BT USB dongle.

Is it possible to make bluetooth functionality work in the emulator?

Any comments/help will be highly appreciated.

Thanks,
Shekhar

-- 

2. API to test if an application has the Auto Update flag enabled?

I'd like to get a list of my applications that have the auto update flag 
enabled.  After searching the APIs and lots of googling, i can't seem to 
find an API that would allow me to test if an Application has this set or 
not.  Does anyone know how to check for this behaviour?

Thanks.

-- 

3. Resigned app not listed under Apps

4. [ask] android httpclient login into web and request data

5. Android version doubts

6. Fake Multitouch : was --禄 WTA: CSL Blueberry MI 410

7. [WTA-renew] Optimus2X internal storage & usb tethering problem