【问题标题】:Widget null pointer exception on orientation change方向更改时的小部件空指针异常
【发布时间】:2014-04-25 12:02:50
【问题描述】:

我有一个讨厌的问题,NumberPicker 和 ImageView 在纵向工作完全正常,并在方向更改时产生空指针异常(我花了一段时间才弄清楚它只在纵向模式下工作,因为我首先想到了方向改变它本身就是问题)。

代码如下:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo_viewer);

        Intent intent = getIntent();
        Bundle extras = intent.getExtras();

        mTouchImageView = (TouchImageView) findViewById(R.id.photoView);
        mTimePicker = (NumberPicker) findViewById(R.id.timePicker);
        mTimePickerScreen = (ImageView) findViewById(R.id.timePickerScreen);

        mImageButtonCrop = (ImageButton) findViewById(R.id.imageButtonCrop);

        mTimePickerScreen.setVisibility(ImageView.INVISIBLE);
        mTimePicker.setVisibility(NumberPicker.INVISIBLE);
        mTimePicker.setMaxValue(15);
        mTimePicker.setMinValue(1);
        ...

mTimePickerScreen.setVisibility(ImageView.INVISIBLE); 行会导致错误,如果 id 将其注释掉,则下一个会以此类推。在注释掉很多行后,我得出结论,问题只出在nTimePickernTimePickerScreen

XML:

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:theme="@style/TimePickerTheme">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/timePickerScreen"
            android:background="@color/black_translucent"/>

        <NumberPicker
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/timePicker"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true" />
    </RelativeLayout>

【问题讨论】:

    标签: android nullpointerexception android-widget android-orientation


    【解决方案1】:

    解决方案是将上述 XML 行也添加到 res/layout-land 中的横向布局 XML 中,现在一切正常。我知道这可能是一个业余错误,但我搜索了很多解决方案但找不到,所以我认为这可能对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多