w:警告:sleepTrackerViewModel.startButtonVisible.getValue()是一个装箱的字段,但需要取消装箱才能执行android:enabled

时间:2019-06-19 04:25:29

标签: android kotlin

应用程序运行正常,但是一旦我尝试添加recyleView,就会收到类似这样的错误

日志错误

2019-06-19 10:12:31.052 7364-7364/com.example.android.trackmysleepqualityrecyclerview E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.android.trackmysleepqualityrecyclerview, PID: 7364
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.trackmysleepqualityrecyclerview/com.example.android.trackmysleepquality.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean androidx.recyclerview.widget.RecyclerView$ViewHolder.shouldIgnore()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2985)

w: warning: sleepTrackerViewModel.startButtonVisible.getValue() is a boxed field but needs to be un-boxed to execute android:enabled. This may cause NPE so Data Binding will safely unbox it. You can change the expression and explicitly wrap sleepTrackerViewModel.startButtonVisible.getValue() with safeUnbox() to prevent the warning

我不知道它从哪里来。

这是我的代码。

fragment_sleep_tracker.xml

<androidx.recyclerview.widget.RecyclerView
            android:id="@+id/sleep_list"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toTopOf="@+id/clear_button"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/stop_button">

            <!-- In the TextView, we can access the nightsString LiveData,
                 which keeps it displayed and updated in the TextView
                 whenever it changes. -->

            <TextView
                android:id="@+id/textview"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/margin"
                android:layout_marginTop="@dimen/margin"
                android:layout_marginEnd="@dimen/margin"
                android:text="@{sleepTrackerViewModel.nightsString}" />
        </androidx.recyclerview.widget.RecyclerView>

sleepNightAdapter.kt

class SleepNightAdapter :RecyclerView.Adapter<TextItemViewHolder>(){

    var data = listOf<SleepNight>()
        set(value){
            field = value
            notifyDataSetChanged()
        }

    override fun getItemCount() = data.size

    override fun onBindViewHolder(holder: TextItemViewHolder, position: Int) {
        var item = data[position]
        holder.textView.text = item.sleepQuality.toString()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TextItemViewHolder {
        val layoutInflater = LayoutInflater.from(parent.context)
        val view = layoutInflater.inflate(R.layout.text_item_view, parent, false) as TextView
        return TextItemViewHolder(view)
    }
}

0 个答案:

没有答案