我有一个res / layout / main.xml包括这些元素和其他:
- <some.package.MyCustomView android:id="@+id/foo" (some other params) />
- <TextView android:id="@+id/boring" (some other params) />
在我的Activity的onCreate,我这样做:
- 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"); }
其他元素成功找到,但foo回到null。 MyCustomView有一个构造函数MyCustomView(Context c,AttributeSet a)和一个Log.d(…)在构造函数的末尾成功地出现在logcat就在“史诗失败”之前。
为什么是foo null?