Hi
if I start CriminalIntent after Listing 9.3 the emulator says Criminal Intent keeps stopping
Logcat says
at com.bignerdranch.android.criminalintent.CrimeListFragment.getCrimeListViewModel(Unknown Source:2)
at com.bignerdranch.android.criminalintent.CrimeListFragment.access$getCrimeListViewModel$p(CrimeListFragment.kt:9)
at com.bignerdranch.android.criminalintent.CrimeListFragment$crimeListViewModel$2.invoke(CrimeListFragment.kt:12)
at com.bignerdranch.android.criminalintent.CrimeListFragment$crimeListViewModel$2.invoke(CrimeListFragment.kt:9)
What should I do?
Changing
implementation 'com.android.support:appcompat-v7:28.0.0'
to
implementation ‘androidx.appcompat:appcompat:1.0.2’
doesn’t help
other lines are
implementation “androidx.fragment:fragment-ktx:$fragment_version”
implementation “androidx.lifecycle:lifecycle-extensions:2.0.0”
implementation “androidx.recyclerview:recyclerview:1.0.0”
Any suggestions?
Listing of CrimeListFragment
package com.bignerdranch.android.criminalintent
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
private const val TAG =“CrimeListFragment”
class CrimeListFragment : Fragment() {
private lateinit var crimeRecyclerView :RecyclerView
private val crimeListViewModel : CrimeListViewModel by lazy{
ViewModelProviders.of(this).get(crimeListViewModel::class.java)
}
override fun onCreate(savedInstanceState: Bundle?){
super.onCreate(savedInstanceState)
Log.d(TAG, "Total crimes: ${crimeListViewModel.crimes.size}")
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view =inflater.inflate(R.layout.fragment_crime_list,container,false)
crimeRecyclerView =view.findViewById(R.id.crime_recycler_view) as RecyclerView
crimeRecyclerView.layoutManager=LinearLayoutManager(context)
return view
}
private inner class CrimeHolder(view: View)
:RecyclerView.ViewHolder(view)
{
val titleTextView: TextView = itemView.findViewById(R.id.crime_title)
val dateTextView: TextView = itemView.findViewById(R.id.crime_date)
}
private inner class CrimeAdapter(var crimes: List<Crime>)
: RecyclerView.Adapter<CrimeHolder>(){
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int)
: CrimeHolder{val view = layoutInflater.inflate(R.layout.list_item_crime, parent,false)
return CrimeHolder(view)
}
override fun getItemCount() = crimes.size
override fun onBindViewHolder(holder: CrimeHolder, position: Int) {
val crime = crimes[position]
holder.apply { titleTextView.text = crime.title
dateTextView.text=crime.date.toString()}
}
}
companion object{
fun newInstance():CrimeListFragment{
return CrimeListFragment()
}
}
}
@HothCoder were you able to resolve this? If not please post the full stack trace from Logcat.