Force Close on Changing time: Time Picker (NPE)

后端 未结 3 1160
伪装坚强ぢ
伪装坚强ぢ 2020-12-21 07:25

When I click on the time picker for changing the time I get force close.

Can anybody tell me where is the problem in my code. Android. and this is my code......

相关标签:
3条回答
  • 2020-12-21 07:48

    Changing the theme of my application to android:Theme.Black in my AndroidManifest.xml. solved the problem for me.

    adding this style to styles.xml

    The style of my fragment looks different but now it works.

    enter image description here

    Update: I have fixed this issue without changing the style, adding the property android:descendantFocusability="blocksDescendants" to my TimePicker

    <TimePicker
        android:id="@+id/toTimePicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:descendantFocusability="blocksDescendants" />
    
    0 讨论(0)
  • 2020-12-21 07:57

    As said, it seems to be a known bug. A possible work-around can be iterating through children until you find EditText and set it "focusable" property to false.

    Recursively,

    // 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);
    
            }
    
        }
    }
    
    0 讨论(0)
  • 2020-12-21 07:58

    This is a known native android bug. See bug #24387 in Android Bug Tracker.

    0 讨论(0)
提交回复
热议问题