我的android项目中行为异常。

首先,我创建了一个名为ANumberPicker的自定义视图(以在OS版本

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) = {org.solovyev.android.view.NumberPickerButton@830082344688}
R.id.increment = 2131296256
this.getChildAt(0).getId() = 2131296256
incrementButton = null


也就是说,完全相同的代码在调试器中不会返回null。
我不知道...有什么建议吗?问题可能出在哪里?

PS我清理并重建了项目(我使用Maven)

编辑
我找到了线索:R.id.increment和view具有不同的整数标识符(我刚刚将其ID记录在Logcat中)。但是问题仍然存在-清理项目无济于事...

最佳答案

已解决:Jetbrains IDEA将先前构建的R文件存储在缓存中,因此“使缓存无效”解决了该问题。

关于java - View#findViewById在运行时返回null,但在调试器中不返回null,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11862606/

10-16 21:47