簡體   English   中英

更新到最新版本的 android studio 和 kotlin 后,kotlin 中的幾個空對象引用錯誤

[英]several on a null object reference error in kotlin after updated to the latest version of android studio and kotlin

我在類(CustomCalendar.kt)中有一個函數,我使用 CustomCalendar 的函數一個月了,但在這兩天里,我收到了不同的錯誤。 所以這個錯誤是

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewTreeObserver android.widget.ScrollView.getViewTreeObserver()' on a null object

這是我的 CustomCalendar 類的 makeCurrentMonth 函數

fun makeCurrentMonth(context: Context, view: View, activity: Activity) {
        val resources: Resources = activity.resources
        for (i in 1..maxDaysOfMonth) {

            val roundDay = GregorianCalendar(year, month, i)
            val dayOfWeek = roundDay.get(Calendar.DAY_OF_WEEK)
            val dayOfMonth = roundDay.get(Calendar.DAY_OF_MONTH)

//          ------------------1 to 9 number of day in month---------------------------------
            if (dayOfMonth in 1..9) {
                val add0 = "0$i".toInt()
                val dayText = choosingDayOfWeek(dayOfWeek) + "\n0$dayOfMonth"
                val dayViewId =
                    resources.getIdentifier("day_0$i", "id", context.packageName)
                Log.d("TAG", i.toString())
                view.findViewById<TextView>(dayViewId).text = dayText


//              -----------------current day-------------------------
                if (i == day) {
                view.radio_group.check(choosingToday(add0))
                val dayID = view.findViewById<TextView>(dayViewId)
                view.scroll_view.viewTreeObserver.addOnGlobalLayoutListener {
                    view.scroll_view.post {
                        view.scroll_view.smoothScrollTo(
                            dayID.left - 35,
                            0
                            )
                        }
                    }
                }

            }

//          -------------11 to 31 one day of month---------------------
            else {
                val dayText = choosingDayOfWeek(dayOfWeek) + "\n$dayOfMonth"
                val dayViewId =
                    resources.getIdentifier("day_$i", "id", context.packageName)
                Log.d("TAG", i.toString())
                view.findViewById<TextView>(dayViewId).text = dayText

//              ------------------current day ---------------------------
                if (i == day) {
                    view.radio_group.check(choosingToday(i))
                    val dayID = view.findViewById<TextView>(dayViewId)
                    view.scroll_view.viewTreeObserver.addOnGlobalLayoutListener {
                        view.scroll_view.post {
                            view.scroll_view.smoothScrollTo(
                                dayID.left - 35,
                                0
                            )
                        }
                    }
                }
            }
        }
    }

這是我在課堂上的變量

private val c = Calendar.getInstance()

private val year = c.get(Calendar.YEAR)
private val month = c.get(Calendar.MONTH)
private val day = c.get(Calendar.DAY_OF_MONTH)
private val currentDay = GregorianCalendar(year, month, day )

private val hour = c.get(Calendar.HOUR_OF_DAY)
private val minute = c.get(Calendar.MINUTE)
private val maxDaysOfMonth = currentDay.getActualMaximum(Calendar.DAY_OF_MONTH)

radio_buttons_month.xml

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:id="@+id/scroll_view"
    android:layout_height="100dp">


    <RadioGroup
        android:id="@+id/radio_group"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginTop="12dp"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:paddingHorizontal="16dp">


        <RadioButton
            android:id="@+id/day_01"
            android:layout_width="60dp"
            android:layout_height="100dp"
            android:layout_marginHorizontal="3dp"
            android:background="@drawable/days_radio_button"
            android:button="@android:color/transparent"
            android:elevation="10dp"
            android:fontFamily="@font/poppins_semibold"
            android:gravity="center"
            android:lineSpacingExtra="15dp"
            android:text="@string/_01"
            android:textColor="@color/orange"
            android:textSize="@dimen/text_size_days" />

        <RadioButton
            android:id="@+id/day_02"
            android:layout_width="60dp"
            android:layout_height="100dp"
            android:layout_marginHorizontal="3dp"
            android:background="@drawable/days_radio_button"
            android:button="@android:color/transparent"
            android:elevation="10dp"
            android:fontFamily="@font/poppins_semibold"
            android:gravity="center"
            android:lineSpacingExtra="15dp"
            android:text="@string/_02"
            android:textColor="@color/orange"
            android:textSize="@dimen/text_size_days" />

// and other radio buttons...

    </RadioGroup>


</HorizontalScrollView>

並且我在我的片段中使用了這個函數並且沒有問題並且沒有更改影響這條線的代碼(至少我知道)剛剛更新了kotlin並將android studio更新到4.2.2

首先嘗試參考您的視圖,我在您的代碼中看到您調用 viewTreeObserver.addOnGlobalLayoutListener,

view.scroll_view.viewTreeObserver.addOnGlobalLayoutListener或此錯誤表示您的 scroll_view 為空。

為避免這種情況,我認為您必須首先參考您的視圖,它使您的代碼也更干凈和可讀。

這是一個例子:

//If you use fragment
View view = inflater.inflate(R.layout.fragment, container, false);
   HorizontalScrollView scroll_view= (HorizontalScrollView ) view.findViewById(R.id.scroll_view);

上面的示例是使用視圖 ID 綁定視圖的最可靠方法(我認為是因為它在較新版本的 kotlin 中仍然有效)。

但是, 這里還要解釋在新版本的 kotlin 中綁定視圖必須遵循的步驟。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM