ViewGroup.findViewWithTag() does not find children in the view hierarchy

1,618 views
Skip to first unread message

Moritz Post

unread,
Aug 30, 2011, 12:27:22 PM8/30/11
to Robolectric
Hallo Roboelectric

I am trying to find a View within my layout based on its tag
(View.setTag()). This does not work when using roboelectric but it
does work when running the code on a physical device. I am using
robolectric-0.9.4-all.jar and you can find a test case for the
described faulty behavior below:



@RunWith(RobolectricTestRunner.class)
public class FindViewByTagTest {

private static final String CHILD_TAG = "child";

@Test
public void testFindViewByTag() throws Exception {
Activity activity = new Activity();

LinearLayout rootGroup = new LinearLayout( activity );
rootGroup.setTag( "root" );
activity.addContentView( rootGroup, null );

TextView childGroup = new TextView( activity );
childGroup.setTag( CHILD_TAG );
rootGroup.addView( childGroup );

View childAt = rootGroup.getChildAt( 0 );
assertEquals( CHILD_TAG, childAt.getTag() );

assertEquals( childGroup,
rootGroup.findViewWithTag( CHILD_TAG ) );
}
}



Greets
Moritz

Moritz Post

unread,
Aug 31, 2011, 4:43:24 AM8/31/11
to Robolectric
Hallo Roboelectric

I have update the used roboelectric library to 1.0M5. This version is
showing the correct behaivor and the test passes nicely.

A side effect of updating the lib is, that i get seceral warnings like
these:

Warning: an error occurred while binding shadow class:
ShadowAndroidHttpClient
Warning: an error occurred while binding shadow class:
ShadowItemizedOverlay
Warning: an error occurred while binding shadow class:
ShadowNdefMessage
Warning: an error occurred while binding shadow class:
ShadowNdefRecord
Warning: an error occurred while binding shadow class:
ShadowNfcAdapter
WARNING: Unable to find path to Android SDK

I have set the android.jar and the maps.jar but am missing some other
jars i recon. Also why is it asking for a path to the android sdk?

In fact i don't use any classes from the maps.jar nore the other
features that can not be bound. Couldn't i simply remove those? Why is
roboelectric complaining?

And finally... is 1.0RC5 the latest version? I am very much confused
of the release policy. No idea what is the latest version and where to
get it from. Is 2.0 current? 2.0 doesn't work for me.

Greest
Moritz Post

Joseph Moore

unread,
Aug 31, 2011, 9:14:45 AM8/31/11
to robol...@googlegroups.com
Hi Moritz --

The release process is something we are working on improving. RC5 is head/master, as a new RC5 snapshot is created by our CI process after every commit on the master branch. 2.0 is a test release where we are testing Robolectric backed by android jars that have real implementations. You can ignore 2.0.

Regarding the errors try running "android update project -p ." to update local.properties and see if that helps.

-- Joe

Tyler Schultz

unread,
Aug 31, 2011, 10:31:15 AM8/31/11
to robol...@googlegroups.com
The warnings about errors binding shadow classes is because you're using a version of Android that does not contain those classes.  The Android classes those shadows are backing were introduced in Gingerbread, so robolectric is unable to bind to those classes.  These errors are caught and logged out as an attempt to maintain backwards compatibility, and allow developers to see that a shadow class is having problems. 

We've been throwing around ideas on how to handle these cases, perhaps the shadow class could be annotated with the minimum version of Android they belong to, so they can be skipped in such cases. On the other hand, features like this are weighed against a mountain of feature work to be done.  Pull requests are welcome.

--Tyler
Reply all
Reply to author
Forward
0 new messages