3

App was working fine but as soon i try to add recyleView I get error something like this

Error in Log

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

I have no idea where it is coming from.

This is my Code.

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)
    }
}
piet.t
  • 11,035
  • 20
  • 40
  • 49
aditya kumar
  • 2,157
  • 5
  • 24
  • 50

0 Answers0