findViewById () zwraca wartość null dla niestandardowego komponentu w układzie XML, a nie dla innych komponentów

mamres/layout/main.xml w tym te elementy i inne:

<some.package.MyCustomView android:id="@+id/foo" (some other params) />
<TextView android:id="@+id/boring" (some other params) />

W mojej działalności na stronie Utwórz to:

setContentView(R.layout.main);
TextView boring = (TextView) findViewById(R.id.boring);
// ...find other elements...
MyCustomView foo = (MyCustomView) findViewById(R.id.foo);
if (foo == null) { Log.d(TAG, "epic fail"); }

Pozostałe elementy zostaną znalezione pomyślnie, alefoo wraca null. MyCustomView ma konstruktoraMyCustomView(Context c, AttributeSet a) i aLog.d(...) na końcu tego konstruktora pojawia się pomyślnie w logcat tuż przed „epickim niepowodzeniem”.

Dlaczego jestfoo zero?

questionAnswers(18)

yourAnswerToTheQuestion