默认时间选择器对话框崩溃

时间:2018-05-08 03:43:53

标签: java android timepicker

我正在尝试使用timepicker对话框,但每次启动对话框时,它都会崩溃,而我似乎无法弄清楚问题。我在Android Studio中模拟的Nexus 5X API Level P上运行它。在此之前,我尝试在没有片段的情况下制作TimePicker对话框,它给了我同样的错误。我现在得到了。

编辑:意识到它适用于我的手机,但不适用于android studio中的模拟器。

TimePickerDialogFragment

public class TimePickerDialogFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener{
public interface TimePickerDialogListener {
    public void onTimePickerDialogTimeSet(int hour, int minute);
}

TimePickerDialogListener mListener;

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Date date = new Date();
    Calendar calendar = GregorianCalendar.getInstance();
    calendar.setTime(date);
    int currentHour = calendar.get(Calendar.HOUR_OF_DAY);
    int currentMinute = calendar.get(Calendar.MINUTE);

    return new TimePickerDialog(getActivity(), this, currentHour, currentMinute, true);
}

public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    mListener.onTimePickerDialogTimeSet(hourOfDay, minute);
}

}

从MainActivity调用

   TextView timeEdit = (TextView)findViewById(R.id.alarmTime);
    timeEdit.setText(dreamAlarm.getAlarmTimeString());
    timeEdit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DialogFragment TimePickerDialogFragment = new TimePickerDialogFragment();
            TimePickerDialogFragment.show(getFragmentManager(), "alarm-time-picker");
        }
    });

MainActivity中的接收者

@Override
public void onTimePickerDialogTimeSet(int hour, int minute) {
    dreamAlarm.setAlarmTime(hour, minute);
    TextView timeEdit = (TextView)findViewById(R.id.alarmTime);
    timeEdit.setText(dreamAlarm.getAlarmTimeString());
}

堆栈跟踪

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.poon.afteralarm, PID: 14286
              android.view.InflateException: Binary XML file line #20: Binary XML file line #20: Error inflating class android.widget.TimePicker
              Caused by: android.view.InflateException: Binary XML file line #20: Error inflating class android.widget.TimePicker
              Caused by: java.lang.reflect.InvocationTargetException
                  at java.lang.reflect.Constructor.newInstance0(Native Method)
                  at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
                  at android.view.LayoutInflater.createView(LayoutInflater.java:647)
                  at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
                  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:720)
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:788)
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
                  at android.app.TimePickerDialog.<init>(TimePickerDialog.java:120)
                  at android.app.TimePickerDialog.<init>(TimePickerDialog.java:80)
                  at com.example.poon.afteralarm.TimePickerDialogFragment.onCreateDialog(TimePickerDialogFragment.java:28)
                  at android.app.DialogFragment.onGetLayoutInflater(DialogFragment.java:411)
                  at android.app.Fragment.performGetLayoutInflater(Fragment.java:1339)
                  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1298)
                  at android.app.FragmentManagerImpl.addAddedFragments(FragmentManager.java:2426)
                  at android.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2205)
                  at android.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2161)
                  at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2062)
                  at android.app.FragmentManagerImpl$1.run(FragmentManager.java:738)
                  at android.os.Handler.handleCallback(Handler.java:819)
                  at android.os.Handler.dispatchMessage(Handler.java:99)
                  at android.os.Looper.loop(Looper.java:164)
                  at android.app.ActivityThread.main(ActivityThread.java:6656)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
               Caused by: android.view.InflateException: Binary XML file line #29: Binary XML file line #29: Error inflating class android.widget.RadialTimePickerView
               Caused by: android.view.InflateException: Binary XML file line #29: Error inflating class android.widget.RadialTimePickerView
               Caused by: java.lang.reflect.InvocationTargetException
                  at java.lang.reflect.Constructor.newInstance0(Native Method)
                  at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
                  at android.view.LayoutInflater.createView(LayoutInflater.java:647)
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
                  at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
                  at android.widget.TimePickerClockDelegate.<init>(TimePickerClockDelegate.java:146)
                  at android.widget.TimePicker.<init>(TimePicker.java:141)
                  at android.widget.TimePicker.<init>(TimePicker.java:114)
                  at android.widget.TimePicker.<init>(TimePicker.java:110)
                  at java.lang.reflect.Constructor.newInstance0(Native Method)
                  at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
                  at android.view.LayoutInflater.createView(LayoutInflater.java:647)
                  at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
                  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:720)
E/AndroidRuntime:     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:788)
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
                  at android.app.TimePickerDialog.<init>(TimePickerDialog.java:120)
                  at android.app.TimePickerDialog.<init>(TimePickerDialog.java:80)
                  at com.example.poon.afteralarm.TimePickerDialogFragment.onCreateDialog(TimePickerDialogFragment.java:28)
                  at android.app.DialogFragment.onGetLayoutInflater(DialogFragment.java:411)
                  at android.app.Fragment.performGetLayoutInflater(Fragment.java:1339)
                  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1298)
                  at android.app.FragmentManagerImpl.addAddedFragments(FragmentManager.java:2426)
                  at android.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2205)
                  at android.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2161)
                  at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2062)
                  at android.app.FragmentManagerImpl$1.run(FragmentManager.java:738)
                  at android.os.Handler.handleCallback(Handler.java:819)
                  at android.os.Handler.dispatchMessage(Handler.java:99)
                  at android.os.Looper.loop(Looper.java:164)
                  at android.app.ActivityThread.main(ActivityThread.java:6656)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
               Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 5: TypedValue{t=0x2/d=0x101042a a=1}
                  at android.content.res.TypedArray.getColorStateList(TypedArray.java:538)
                  at android.widget.RadialTimePickerView.applyAttributes(RadialTimePickerView.java:419)
                  at android.widget.RadialTimePickerView.<init>(RadialTimePickerView.java:331)
                  at android.widget.RadialTimePickerView.<init>(RadialTimePickerView.java:324)
                  at android.widget.RadialTimePickerView.<init>(RadialTimePickerView.java:320)
                    ... 42 more

MainActivity XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Switch
        android:id="@+id/alarmSwitch"
        android:layout_width="333dp"
        android:layout_height="47dp"
        android:text="@string/app_name"
        android:textSize="24sp"
        app:layout_constraintBottom_toTopOf="@+id/alarmTime"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/alarmTime"
        android:layout_width="255dp"
        android:layout_height="173dp"
        android:ems="10"
        android:textSize="36sp"
        app:layout_constraintBottom_toTopOf="@+id/recordForText"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/alarmSwitch" />

    <TextView
        android:id="@+id/recordForText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:ems="10"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/alarmTime" />

</android.support.constraint.ConstraintLayout>

编辑:意识到它适用于我的手机,但不适用于android studio中的模拟器。

1 个答案:

答案 0 :(得分:0)

尝试将您的通话部分 TimePickerDialogFragment.show(getFragmentManager(), "alarm-time-picker"); 更改为低于一个。

TextView timeEdit = (TextView)findViewById(R.id.alarmTime);
timeEdit.setText(dreamAlarm.getAlarmTimeString());
timeEdit.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

    TimePickerDialogFragment dialogFragment = new TimePickerDialogFragment();
    dialogFragment.show(getFragmentManager(), "timePicker");
    dialogFragment.setListener(new TimePickerDialogFragment.TimePickerDialogListener() {
        @Override
        public void onTimePickerDialogTimeSet(int hour, int minute) {
            //here use the time
            Log.d("hour= ", hour + " minute= " + minute);
        }
    });
    }

});

并且您的TimePickerDialogFragment应该如下所示

   public class TimePickerDialogFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {

    private TimePickerDialogListener listener;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Date date = new Date();
        Calendar calendar = GregorianCalendar.getInstance();
        calendar.setTime(date);
        int currentHour = calendar.get(Calendar.HOUR_OF_DAY);
        int currentMinute = calendar.get(Calendar.MINUTE);
        return new TimePickerDialog(getContext(), this, currentHour, currentMinute, true);
    }

    @Override
    public void onTimeSet(TimePicker timePicker, int i, int i1) {
        listener.onTimePickerDialogTimeSet(i, i1);
        getDialog().dismiss();
    }

    public void setListener(TimePickerDialogListener listener) {
        this.listener = listener;
    }

    /**
     * listener
     */
    public interface TimePickerDialogListener {
        public void onTimePickerDialogTimeSet(int hour, int minute);
    }
}