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中)。但問題仍然存在 - 清理項目不幫助...