4

当我单击时间选择器更改时间时,我会强制关闭。

谁能告诉我代码中的问题在哪里。安卓。这是我的代码......

timePickerDateTimePickerPopup = (TimePicker) layoutDateTimePickerPopup.findViewById(R.id.timePicker_datetimepicker);
            timePickerDateTimePickerPopup.setDescendantFocusability(TimePicker.FOCUS_BLOCK_DESCENDANTS);
            timePickerDateTimePickerPopup.setIs24HourView(true);
            timePickerDateTimePickerPopup.setCurrentHour(mCalendar.get(Calendar.HOUR_OF_DAY));
            timePickerDateTimePickerPopup.setCurrentMinute(mCalendar.get(Calendar.MINUTE));

            timePickerDateTimePickerPopup.setOnTimeChangedListener(this);

            dateTimePickerPopup.setTouchable(true);
            dateTimePickerPopup.setFocusable(true);
            dateTimePickerPopup.setOutsideTouchable(true);
            Drawable image_saved = context.getResources().getDrawable(R.drawable.dummy_bg);
            dateTimePickerPopup.setBackgroundDrawable(image_saved);

我的 Logcat 显示NullPointerException

STACK_TRACE=java.lang.NullPointerException
at android.widget.TimePicker.updateInputState(TimePicker.java:553)
at android.widget.TimePicker.access$000(TimePicker.java:55)
at android.widget.TimePicker$2.onValueChange(TimePicker.java:149)
at android.widget.NumberPicker.notifyChange(NumberPicker.java:1777)
at android.widget.NumberPicker.changeCurrent(NumberPicker.java:1455)
at android.widget.NumberPicker.changeCurrentByOne(NumberPicker.java:1481)
at android.widget.NumberPicker.access$100(NumberPicker.java:70)
at android.widget.NumberPicker$2.onClick(NumberPicker.java:592)
at android.view.View.performClick(View.java:3519)
at android.view.View$PerformClick.run(View.java:14140)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
at dalvik.system.NativeStart.main(Native Method)
4

3 回答 3

2

这是一个已知的原生 android 错误。请参阅Android Bug Tracker 中的错误 #24387

于 2013-06-06T13:49:37.760 回答
2

将我的应用程序主题更改为android:Theme.Black我的AndroidManifest.xml. 为我解决了这个问题。

将此样式添加到styles.xml

我的片段的风格看起来不同,但现在它可以工作了。

在此处输入图像描述

更新:我在不改变样式的情况下解决了这个问题,将属性添加 android:descendantFocusability="blocksDescendants" 到我的 TimePicker

<TimePicker
    android:id="@+id/toTimePicker"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:descendantFocusability="blocksDescendants" />
于 2014-09-02T20:55:45.647 回答
0

如前所述,这似乎是一个已知的错误。一种可能的解决方法是遍历子项,直到找到 EditText 并将其“可聚焦”属性设置为 false。

递归地,

// level is just a counter for reading better Logs
private void hideFocusable(ViewGroup vg, int level)
{
    for(int i=0; i < vg.getChildCount(); i++) 
    {
        try
        {
               ViewGroup vt = (ViewGroup) vg.getChildAt(i);
               Log.i("NODE",level+") "+ vg.getChildAt(i).getClass().getName()); 

               hideFocusable(vt, level+1);

        }catch (ClassCastException e) {
                View vf = (View) vg.getChildAt(i);

                Log.i("LEAF",level+") "+ vf.getClass().getName());

                // here you can pick EditText and setFocusable to false 
                if(vf.getClass().equals(EditText.class))
                   vf.setFocusable(false);

        }

    }
}
于 2014-03-03T12:05:57.280 回答