当前位置: 首页 > 知识库问答 >
问题:

使用TimePickerDialog时出现空指针异常

锺离玮
2023-03-14
java.lang.RuntimeException: Unable to pause activity
Caused by: java.lang.NullPointerException
at android.widget.TimePicker.updateInputState(TimePicker.java:995)
at android.widget.TimePicker.onSaveInstanceState(TimePicker.java:640)
at android.view.View.dispatchSaveInstanceState(View.java:13561)

>

  • 单击

    showDialog(END_TIME_PICKER_ID);
    

    受保护得空onPrepareDialog(int id,Dialog Dialog)

      case END_TIME_PICKER_ID :
            endHourOfDay = mEndCalendar.get(Calendar.HOUR_OF_DAY);
             endMinuteOfDay = mEndCalendar.get(Calendar.MINUTE);
    
            ((TimePickerDialog) dialog).updateTime(endHourOfDay, endMinuteOfDay);
            ((TimePickerDialog) dialog).show();
            break;
    

    受保护的对话框onCreateDialog(int id)

     case END_TIME_PICKER_ID :
      return new TimePickerDialog(this, mEndTimeSetListener, endHourOfDay, endMinuteOfDay, false);
    
     private TimePickerDialog.OnTimeSetListener mEndTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
    public void onTimeSet(TimePicker view, int hourOfDay, int minute)
    {
        endHourOfDay = hourOfDay;
        endMinuteOfDay = minute;
     GregorianCalendar endCalendar = (GregorianCalendar) mEndCalendar.clone();
        endCalendar.set(Calendar.DAY_OF_MONTH, mStartCalendar.get(Calendar.DAY_OF_MONTH));
        endCalendar.set(Calendar.HOUR_OF_DAY, endHourOfDay);
        endCalendar.set(Calendar.MINUTE, endMinuteOfDay);
        endCalendar.set(Calendar.SECOND, second);
     }
    
         java.lang.RuntimeException: Unable to start activity ComponentInfo
    

    如果我对timepickerdialog代码做了什么错误,请告诉我。

  • 共有1个答案

    刘才俊
    2023-03-14

    我在Android4.4上也遇到过同样的问题,在旧版本上没有发生过。一般而言,我发现这个问题与TimePicker有关。

    来自LOGCAT:

    E/AndroidRuntime( 8671): java.lang.NullPointerException
    
    E/AndroidRuntime( 8671):    at android.widget. (TimePicker.java:995)
    
    E/AndroidRuntime( 8671):    at android.widget.TimePicker.onSaveInstanceState(TimePicker.java:640)
    
    E/AndroidRuntime( 8671):    at android.view.View.dispatchSaveInstanceState(View.java:13570)
    
    E/AndroidRuntime( 8671):    at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2813)
    
    E/AndroidRuntime( 8671):    at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2819)
    
    E/AndroidRuntime( 8671):    at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2819)
    
    E/AndroidRuntime( 8671):    at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2819)
    
    E/AndroidRuntime( 8671):    at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2819)
    
    E/AndroidRuntime( 8671):    at android.view.View.saveHierarchyState(View.java:13553)
    
    E/AndroidRuntime( 8671):    at com.android.internal.policy.impl.PhoneWindow.saveHierarchyState(PhoneWindow.java:1875)
    
    E/AndroidRuntime( 8671):    at android.app.Dialog.onSaveInstanceState(Dialog.java:408)
    
    E/AndroidRuntime( 8671):    at android.app.TimePickerDialog.onSaveInstanceState(TimePickerDialog.java:238)
    
    E/AndroidRuntime( 8671):    at android.app.Activity.saveManagedDialogs(Activity.java:1267)
    
    E/AndroidRuntime( 8671):    at android.app.Activity.performSaveInstanceState(Activity.java:1185)
    
    E/AndroidRuntime( 8671):    at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1241)
    
    E/AndroidRuntime( 8671):    at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3331)
    
    E/AndroidRuntime( 8671):    at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3390)
    
    E/AndroidRuntime( 8671):    at android.app.ActivityThread.access$1100(ActivityThread.java:163)
    
    E/AndroidRuntime( 8671):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1279)
    
    E/AndroidRuntime( 8671):    at android.os.Handler.dispatchMessage(Handler.java:102)
    
    E/AndroidRuntime( 8671):    at android.os.Looper.loop(Looper.java:157)
    
    E/AndroidRuntime( 8671):    at android.app.ActivityThread.main(ActivityThread.java:5335)
    
    E/AndroidRuntime( 8671):    at java.lang.reflect.Method.invokeNative(Native Method)
    
    E/AndroidRuntime( 8671):    at java.lang.reflect.Method.invoke(Method.java:515)
    
    E/AndroidRuntime( 8671):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
    
    E/AndroidRuntime( 8671):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
    
    E/AndroidRuntime( 8671):    at dalvik.system.NativeStart.main(Native Method)
    

    我通过添加

    <TimePicker android:id="@+id/timePicker"     
         android:layout_width="wrap_content"    
         android:layout_height="wrap_content"    
         android:layout_gravity="center_horizontal"
    android:descendantFocusability="blocksDescendants">  
     <requestFocus />  
    </TimePicker>  
    
    timePicker = (TimePicker) viewContent.findViewById(R.id.timePicker);
    timePicker.setIs24HourView(true);
    timePicker.setOnTimeChangedListener(listener);
    
     类似资料:
    • 问题内容: MyClass.java: 编译MyClass.java文件的SimpleCompileTest.java的清单。 SimpleCompileTest.java: 我正在执行SimpleCompileTest类并获取NullPointerException。ToolProvider.getSystemJavaCompiler()返回null。有人可以告诉我代码有什么问题吗 问题答案:

    • 以下是在sun.reflect.nativeMethodAccessorImpl.Invoke0(本机方法)在sun.reflect.nativeMethodAccessorImpl.Invoke(未知源)在sun.reflect.NativeMethodAccessorImpl.Invoke(未知源)在java.lang.Reflect.Method.Invoke(未知源)在com.codena

    • 我试图用Powermock和Mockito编写一个JUnit测试用例。我正在从测试用例下面调用一个方法。此方法从method1调用,而该方法调用Method2。我已经添加了@runwith和@preparefortest注释。 //这是我的测试类: 错误跟踪:

    • 问题内容: 在查询Android日历中的事件之前,我需要检查权限。为此,Android studio警告我在查询之前需要先进行检查。自动生成的代码如下: 尝试运行它时,出现以下错误: 尝试 在空对象引用上调用虚拟方法’int android.content.Context.checkPermission(java.lang.String,int,int)’ 因此很明显,此时某些东西为空,我试图以不

    • 我一直在构建一个影院预订应用程序,正在尝试创建一个显示电影和放映时间的场景。当我使用锚点窗格和vbox来显示所有信息时,它起作用,但当我尝试插入一个额外的滚动窗格(在scenebuilder中)时,FXML加载程序返回一个空指针异常,我无法找出原因... 这是我的FXML代码 下面是controller类 编辑:fx:id'vbox'不会从getRoot()方法访问,即使它已被注入到FXML加载器

    • 问题内容: 此代码导致空指针异常。我不知道为什么: 我已经在调试器中检查了它,所有局部变量都不为空。怎么会这样呢?BiMap来自Google Collections。 问题答案: 空指针异常是将的结果拆箱的结果。如果不包含键,则返回“ of type” 。假设分配是给引用的,Java将值拆箱到中,导致空指针异常。 您应该检查或用作局部变量类型,以免取消装箱并采取相应措施。适当的机制取决于您的上下文