NOT NULL我已经在我的Android项目一个非常怪异的行为。所有的查看#findViewById返回在运行时为空,但在调试器
首先,我创建了一个名为ANumberPicker自定义视图(与OS版本< 3.0使用它在Android设备)。下面是类的代码片段:
public class ANumberPicker extends LinearLayout {
// ...
@NotNull
private final NumberPickerButton incrementButton;
@NotNull
private final NumberPickerButton decrementButton;
public ANumberPicker(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
setOrientation(VERTICAL);
// INFLATING LAYOUT
final LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.number_picker, this, true);
final InputFilter inputFilter = new NumberPickerInputFilter();
numberInputFilter = new NumberRangeKeyListener();
incrementButton = (NumberPickerButton) this.findViewById(R.id.increment);
incrementButton.setNumberPicker(this);
decrementButton = (NumberPickerButton) this.findViewById(R.id.decrement);
decrementButton.setNumberPicker(this);
// ...
}
// ...
}
这里是number_picker.xml布局:
<merge xmlns:a="http://schemas.android.com/apk/res/android">
<org.solovyev.android.view.NumberPickerButton
a:id="@+id/increment"
a:layout_width="fill_parent"
a:layout_height="wrap_content"
a:background="@drawable/timepicker_up_btn"/>
<EditText a:id="@+id/timepicker_input"
a:layout_width="fill_parent"
a:layout_height="wrap_content"
a:gravity="center"
a:singleLine="true"
style="?android:attr/textAppearanceLargeInverse"
a:textColor="@android:color/primary_text_light"
a:textSize="30sp"
a:inputType="numberDecimal"
a:background="@drawable/timepicker_input"/>
<org.solovyev.android.view.NumberPickerButton
a:id="@+id/decrement"
a:layout_width="fill_parent"
a:layout_height="wrap_content"
a:background="@drawable/timepicker_down_btn"/>
在ANumberPicker构造我夸大 'number_picker.xml' 布局并尝试获取视图通过id:inflater与attachToRoot ='true'参数一起使用。但是,我在此行上运行时遇到了NullPointerException:
incrementButton.setNumberPicker(this);
即incrementButton = null。
而且调试中Jetbrains的IDEA这个代码我得到了“手表”窗户旁边值(断点与NPE的源代码行设置):
(NumberPickerButton) this.findViewById(R.id.increment) = {[email protected]}
R.id.increment = 2131296256
this.getChildAt(0).getId() = 2131296256
incrementButton = null
即完全相同的代码返回的调试器不为空。 我不能看着办吧...有什么建议?问题在哪里?
PS我清理项目和重建它(我用maven)
编辑 我发现了线索:R.id.increment和观点有不同的整数标识符(我刚刚登录它们的ID在logcat中)。但问题仍然存在 - 清理项目不帮助...