kotlin 空对象引用上的Android com.google.android.material.datepicker.MaterialCalendar.getCurrentMonth()'

wwwo4jvm  于 2023-08-06  发布在  Kotlin
关注(0)|答案(1)|浏览(125)

我的Android应用crashlytics已记录此错误:

androidx.fragment.app.Fragment.performSaveInstanceState
Fragment.java, line 3292
java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.material.datepicker.Month com.google.android.material.datepicker.MaterialCalendar.getCurrentMonth()' on a null object 
reference
com.google.android.material.datepicker.MaterialDatePicker.onSaveInstanceState MaterialDatePicker.java:154
androidx.fragment.app.Fragment.performSaveInstanceState Fragment.java:3292
androidx.fragment.app.FragmentStateManager.saveBasicState FragmentStateManager.java:681
androidx.fragment.app.FragmentStateManager.saveState FragmentStateManager.java:647
androidx.fragment.app.FragmentStore.saveActiveFragments FragmentStore.java:214
androidx.fragment.app.FragmentManager.saveAllStateInternal FragmentManager.java:2371
androidx.fragment.app.Fragment.performSaveInstanceState Fragment.java:3294
androidx.fragment.app.FragmentStateManager.saveBasicState FragmentStateManager.java:681
androidx.fragment.app.FragmentStateManager.saveState FragmentStateManager.java:647
androidx.fragment.app.FragmentStore.saveActiveFragments FragmentStore.java:214
androidx.fragment.app.FragmentManager.saveAllStateInternal FragmentManager.java:2371
androidx.fragment.app.Fragment.performSaveInstanceState Fragment.java:3294
androidx.fragment.app.FragmentStateManager.saveBasicState FragmentStateManager.java:681
androidx.fragment.app.FragmentStateManager.saveState FragmentStateManager.java:647
androidx.fragment.app.FragmentStore.saveActiveFragments FragmentStore.java:214
androidx.fragment.app.FragmentManager.saveAllStateInternal FragmentManager.java:2371

字符串
这种情况并不总是发生,但大多数受影响的操作系统是在11,12和13,大多数是在三星Galaxy设备
我目前正在使用

implementation 'com.google.android.material:material:1.4.0'


你知不知道该怎么避免这种事?

axr492tv

axr492tv1#

你可以使用材料库获取当前月份使用以下代码。

fun Calendar.getCurrentMonth(): Pair<String, String> {
// add -1 month to current month
    add(Calendar.MONTH, 0)
// set DATE to 1, so first date of previous month
    this[Calendar.DATE] = 1
    val firstDateOfPreviousMonth = time
// set actual maximum date of previous month
    this[Calendar.DATE] = getActualMaximum(Calendar.DAY_OF_MONTH)
//read it
    val lastDateOfPreviousMonth = time
    return Pair(
        firstDateOfPreviousMonth.format("yyyy-MM-dd"),
        lastDateOfPreviousMonth.format("yyyy-MM-dd")
    )
}

字符串

相关问题