Search code examples
androidandroid-layoutandroid-sourcefindviewbyid

findViewById() returns null for a view found by getChildAt()


I'm making some modifications to the AOSP SystemUI app, and have run into a very particular problem when trying to run it on an arbitrary device: findViewById() returns null, while navigating through the its children reveals that it's really there.

I'm working with this AOSP source code and building it through lunch full-eng and make. It compiles fine, but SystemUI crashes with the IllegalArgumentException in the logic following line 337:

mRecentsContainer = (ViewGroup) findViewById(R.id.recents_container);
[...]
if (mRecentsContainer instanceof RecentsHorizontalScrollView){
    [...]
} else if (mRecentsContainer instanceof RecentsVerticalScrollView){
    [...]
}
else {
    throw new IllegalArgumentException("missing Recents[Horizontal]ScrollView");
}

A bit of debugging reveals that mRecentsContainer is in fact null.

I considered that perhaps a layout was being inflated that didn't contain that particular view, so to check, I navigated recursively through all the view's children using getChildCount() and getChildAt(). That wasn't the case; @id/recents_container was right there, two levels deep:

> @id/recents_root (7f0e0070) [RecentsPanelView]: (2 children)
--> @id/recents_bg_protect (7f0e0071) [FrameLayout]: (2 children)
----> @id/recents_container (7f0e0072) [RecentsVerticalScrollView]: (1 children)
------> @id/recents_linear_layout (7f0e0073) [LinearLayout]: (0 children)
----> @id/recents_no_apps (7f0e0074) [FrameLayout]: (1 children)
------> [unnamed] (ffffffff) [TextView]
--> @id/recents_dismiss_button (7f0e0075) [View]

A singular hint appears in Logcat from system_process when installing the app:

12-11 22:38:01.090: W/ResourceType(165): Failure getting entry for 0x7f060000 (t=5 e=0) in package 0 (error -75)

Inspecting R.java in the SystemUI_intermediates directory reveals that it points to a seemingly unrelated status_bar_recents_app_label_color, which is simply a defined color in res/values/color.xml.

What can be causing this? Is the layout not inflating correctly because I'm running it on a device I didn't compile against? I considered that perhaps the problem may be SystemUI's references to internal Android resources, which might not map correctly to those on my device. Is that possible?


Solution

  • Evidently some generated files were sticking around.

    Rebuilding the source from scratch through make clean resolved it.

    Alternatively, I've figured out that deleting the following intermediate directories makes for a faster build, but am not 100% sure that this covers all directories:

    rm -rf out/target/product/generic/obj/APPS/SystemUI_intermediates/
    rm -rf out/target/common/R/com/android/systemui
    rm -rf out/target/product/generic/obj/APPS/framework-res_intermediates/
    rm -f /out/target/common/R/com/android/internal/R.java
    rm -rf out/target/product/generic/obj/PACKAGING/
    rm -f out/target/product/generic/system/app/SystemUI.apk