NullPointerException @SavedStateHandle ViewModel w/ Hilt
NullPointerException @ SavedStateHandle ViewModel w/ Hilt
我的应用程序在 运行 我的视图模型低于
时崩溃
@HiltViewModel
class ReportViewModel @Inject internal constructor(
savedStateHandle: SavedStateHandle,
private val monthRepository: MonthRepository
) : ViewModel() {
//error in this line (monthId) says NullPointerException
private val monthId: Long = savedStateHandle.get<Long>(MONTH_ID_SAVED_STATE_KEY)!!
val report = monthRepository.getMonthData(monthId).asLiveData()
companion object {
private const val MONTH_ID_SAVED_STATE_KEY = "monthId"
}
}
错误是java.lang.NullPointerException
。我正在使用 safeArgs。请参阅下面我的 ViewHolder。
inner class ViewHolderOne(var binding: ListMonthItemBinding) :
RecyclerView.ViewHolder(binding.root) {
init {
binding.root.setOnClickListener {
binding.month?.let { month ->
navigateToMonth(month, it)
}
}
}
private fun navigateToMonth(
month: Month,
view: View
) {
val direction =
MonthFragmentDirections.actionNavMonthsToNavReports(
month.identification
)
view.findNavController().navigate(direction)
}
fun bind(months: Month) {
binding.apply {
month = months
executePendingBindings()
}
}
}
当我开始导航到我的 detailFragment 时。应用程序开始崩溃并提示 NullPointerException。
下面的代码是我的nav_graph
<fragment
android:id="@+id/nav_months"
android:name="com.appname.ui.report.MonthFragment"
android:label="@string/menu_months"
tools:layout="@layout/fragment_month" >
<action
android:id="@+id/action_nav_months_to_nav_reports"
app:destination="@id/nav_reports" />
</fragment>
<fragment
android:id="@+id/nav_reports"
android:name="com.appname.ui.report.ReportFragment"
android:label="@string/report" >
<argument
android:name="monthId"
app:argType="long" />
</fragment>
这里是 ReportFragment
@AndroidEntryPoint
class ReportFragment : Fragment() {
private val reportViewModel: ReportViewModel by viewModels()
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
val binding = DataBindingUtil.inflate<FragmentReportBinding>(
inflater,
R.layout.fragment_report,
container,
false
).apply {
viewmodel = reportViewModel
lifecycleOwner = viewLifecycleOwner
}
return binding.root
}
}
我参考了Google向日葵App的代码。但我想我错过了什么。
如何修复此崩溃?
问题已解决。问题是我从未注意到我在 ReportViewModel
的 MONTH_ID_SAVED_STATE_KEY
值与我在 nav_graph
的 argument
名称不同
应该是
// ReportViewModel.kt
companion object {
private const val MONTH_ID_SAVED_STATE_KEY = "monthId"
}
// nav_graph.xml
<argument
android:name="monthId"
app:argType="long" />
已经在我的 post 更新了代码以供将来参考。
我的应用程序在 运行 我的视图模型低于
时崩溃@HiltViewModel
class ReportViewModel @Inject internal constructor(
savedStateHandle: SavedStateHandle,
private val monthRepository: MonthRepository
) : ViewModel() {
//error in this line (monthId) says NullPointerException
private val monthId: Long = savedStateHandle.get<Long>(MONTH_ID_SAVED_STATE_KEY)!!
val report = monthRepository.getMonthData(monthId).asLiveData()
companion object {
private const val MONTH_ID_SAVED_STATE_KEY = "monthId"
}
}
错误是java.lang.NullPointerException
。我正在使用 safeArgs。请参阅下面我的 ViewHolder。
inner class ViewHolderOne(var binding: ListMonthItemBinding) :
RecyclerView.ViewHolder(binding.root) {
init {
binding.root.setOnClickListener {
binding.month?.let { month ->
navigateToMonth(month, it)
}
}
}
private fun navigateToMonth(
month: Month,
view: View
) {
val direction =
MonthFragmentDirections.actionNavMonthsToNavReports(
month.identification
)
view.findNavController().navigate(direction)
}
fun bind(months: Month) {
binding.apply {
month = months
executePendingBindings()
}
}
}
当我开始导航到我的 detailFragment 时。应用程序开始崩溃并提示 NullPointerException。
下面的代码是我的nav_graph
<fragment
android:id="@+id/nav_months"
android:name="com.appname.ui.report.MonthFragment"
android:label="@string/menu_months"
tools:layout="@layout/fragment_month" >
<action
android:id="@+id/action_nav_months_to_nav_reports"
app:destination="@id/nav_reports" />
</fragment>
<fragment
android:id="@+id/nav_reports"
android:name="com.appname.ui.report.ReportFragment"
android:label="@string/report" >
<argument
android:name="monthId"
app:argType="long" />
</fragment>
这里是 ReportFragment
@AndroidEntryPoint
class ReportFragment : Fragment() {
private val reportViewModel: ReportViewModel by viewModels()
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
val binding = DataBindingUtil.inflate<FragmentReportBinding>(
inflater,
R.layout.fragment_report,
container,
false
).apply {
viewmodel = reportViewModel
lifecycleOwner = viewLifecycleOwner
}
return binding.root
}
}
我参考了Google向日葵App的代码。但我想我错过了什么。
如何修复此崩溃?
问题已解决。问题是我从未注意到我在 ReportViewModel
的 MONTH_ID_SAVED_STATE_KEY
值与我在 nav_graph
argument
名称不同
应该是
// ReportViewModel.kt
companion object {
private const val MONTH_ID_SAVED_STATE_KEY = "monthId"
}
// nav_graph.xml
<argument
android:name="monthId"
app:argType="long" />
已经在我的 post 更新了代码以供将来参考。