java.lang.IllegalArgumentException: 非空参数为 null:- Android 应用启动后立即崩溃

huangapple go评论77阅读模式
英文:

java.lang.IllegalArgumentException: Parameter specified as non-null is null: - android, app starting but crashing straight away

问题

嗨,大家好,我在启动我的应用程序时遇到问题,我不知道问题出在哪里,它在我启动它时立即崩溃了。从日志中我猜是与数据绑定有关,但我现在真的不知道出了什么问题 :C

2020-04-10 22:22:22.716 32624-32624/? I/rss.ing_projec: 正在启用 -Xcheck:jni
2020-04-10 22:22:22.746 32624-32624/? E/rss.ing_projec: 运行时标志中设置了未知位: 0x8000
2020-04-10 22:22:23.053 32624-32624/com.lswarss.ing_project I/Perf: 连接到性能服务。
2020-04-10 22:22:23.070 32624-32624/com.lswarss.ing_project V/Font: 更改字体:1
2020-04-10 22:22:23.071 32624-32624/com.lswarss.ing_project V/Font: 默认字体族:android.graphics.Typeface@177a2b82
...

这是 Android Studio 给出的日志,
并且它与这个类有一些问题,但是出了什么问题呢:

```kotlin
package com.lswarss.ing_project.adapters

import android.view.View
import android.widget.ImageView
import androidx.databinding.BindingAdapter
import androidx.recyclerview.widget.RecyclerView
import com.lswarss.ing_project.R
import com.lswarss.ing_project.domain.PostItem
import com.lswarss.ing_project.fragments.PostsApiStatus

@BindingAdapter("listData")
fun bindRecyclerView(recyclerView: RecyclerView, data: List<PostItem>?){
    val adapter  = recyclerView.adapter as PostsAdapter
    adapter.submitList(data)
}

@BindingAdapter("postApiStatus")
fun bindStatus(statusImageView: ImageView, status: PostsApiStatus?) {
    when (status) {
        PostsApiStatus.LOADING -> {
            statusImageView.visibility = View.VISIBLE
            statusImageView.setImageResource(R.drawable.loading_animation)
        }
        PostsApiStatus.ERROR -> {
            statusImageView.visibility = View.VISIBLE
            statusImageView.setImageResource(R.drawable.ic_connection_error)
        }
        PostsApiStatus.DONE -> {
            statusImageView.visibility = View.GONE
        }
    }
}

我尝试使用 Udacity 课程中制作的应用程序 Mars Real Estate App 来创建这个应用程序。

这是我应用程序在 GitHub 上的链接: https://github.com/LSWarss/ing_project
我遇到问题的代码位于 new_architecture 分支。

如果这是一个愚蠢的问题,我很抱歉,因为我对 Android 和 Kotlin 都很陌生,甚至对在 Stack 上发帖也不熟悉 java.lang.IllegalArgumentException: 非空参数为 null:- Android 应用启动后立即崩溃


<details>
<summary>英文:</summary>

Hey guys I have problem with starting my app, I don&#39;t know what is the problem, it just crashes right away when I start it, from the log I suppose it is something about databinding but I don&#39;t really know what&#39;s
wrong right now :C

2020-04-10 22:22:22.716 32624-32624/? I/rss.ing_projec: Late-enabling -Xcheck:jni
2020-04-10 22:22:22.746 32624-32624/? E/rss.ing_projec: Unknown bits set in runtime_flags: 0x8000
2020-04-10 22:22:23.053 32624-32624/com.lswarss.ing_project I/Perf: Connecting to perf service.
2020-04-10 22:22:23.070 32624-32624/com.lswarss.ing_project V/Font: Change font:1
2020-04-10 22:22:23.071 32624-32624/com.lswarss.ing_project V/Font: Default family:android.graphics.Typeface@177a2b82
2020-04-10 22:22:23.077 32624-32676/com.lswarss.ing_project E/Perf: Fail to get file list com.lswarss.ing_project
2020-04-10 22:22:23.077 32624-32676/com.lswarss.ing_project E/Perf: getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array
2020-04-10 22:22:23.077 32624-32676/com.lswarss.ing_project E/Perf: Fail to get file list com.lswarss.ing_project
2020-04-10 22:22:23.078 32624-32676/com.lswarss.ing_project E/Perf: getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array
2020-04-10 22:22:23.156 32624-32624/com.lswarss.ing_project W/rss.ing_projec: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
2020-04-10 22:22:23.157 32624-32624/com.lswarss.ing_project W/rss.ing_projec: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
2020-04-10 22:22:23.213 32624-32624/com.lswarss.ing_project V/FlingOptimizerScroller: FlingOptimizerOverScroller Init
2020-04-10 22:22:23.251 32624-32624/com.lswarss.ing_project D/AndroidRuntime: Shutting down VM
2020-04-10 22:22:23.253 32624-32624/com.lswarss.ing_project E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.lswarss.ing_project, PID: 32624
java.lang.RuntimeException: Failed to call observer method
at androidx.lifecycle.ClassesInfoCache$MethodReference.invokeCallback(ClassesInfoCache.java:226)
at androidx.lifecycle.ClassesInfoCache$CallbackInfo.invokeMethodsForEvent(ClassesInfoCache.java:194)
at androidx.lifecycle.ClassesInfoCache$CallbackInfo.invokeCallbacks(ClassesInfoCache.java:185)
at androidx.lifecycle.ReflectiveGenericLifecycleObserver.onStateChanged(ReflectiveGenericLifecycleObserver.java:37)
at androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.java:361)
at androidx.lifecycle.LifecycleRegistry.forwardPass(LifecycleRegistry.java:300)
at androidx.lifecycle.LifecycleRegistry.sync(LifecycleRegistry.java:339)
at androidx.lifecycle.LifecycleRegistry.moveToState(LifecycleRegistry.java:145)
at androidx.lifecycle.LifecycleRegistry.handleLifecycleEvent(LifecycleRegistry.java:131)
at androidx.fragment.app.Fragment.performStart(Fragment.java:2735)
at androidx.fragment.app.FragmentStateManager.start(FragmentStateManager.java:365)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1194)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1356)
at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1434)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1497)
at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2625)
at androidx.fragment.app.FragmentManager.dispatchStart(FragmentManager.java:2583)
at androidx.fragment.app.Fragment.performStart(Fragment.java:2739)
at androidx.fragment.app.FragmentStateManager.start(FragmentStateManager.java:365)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1194)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1356)
at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1434)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1497)
at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2625)
at androidx.fragment.app.FragmentManager.dispatchStart(FragmentManager.java:2583)
at androidx.fragment.app.FragmentController.dispatchStart(FragmentController.java:258)
at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:550)
at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:201)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1514)
at android.app.Activity.performStart(Activity.java:7838)
at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3398)
at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221)
at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2109)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
Caused by: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter data
at com.lswarss.ing_project.adapters.BindingAdaptersKt.bindRecyclerView(Unknown Source:7)
at com.lswarss.ing_project.databinding.PostsFragmentBindingImpl.executeBindings(PostsFragmentBindingImpl.java:125)
2020-04-10 22:22:23.253 32624-32624/com.lswarss.ing_project E/AndroidRuntime: at androidx.databinding.ViewDataBinding.executeBindingsInternal(ViewDataBinding.java:473)
at androidx.databinding.ViewDataBinding.executePendingBindings(ViewDataBinding.java:445)
at androidx.databinding.ViewDataBinding$OnStartListener.onStart(ViewDataBinding.java:1687)
at java.lang.reflect.Method.invoke(Native Method)
at androidx.lifecycle.ClassesInfoCache$MethodReference.invokeCallback(ClassesInfoCache.java:216)
... 41 more

That&#39;s the log given by android studio 
And it have some problems with this class but what wrong with it: 

package com.lswarss.ing_project.adapters

import android.view.View
import android.widget.ImageView
import androidx.databinding.BindingAdapter
import androidx.recyclerview.widget.RecyclerView
import com.lswarss.ing_project.R
import com.lswarss.ing_project.domain.PostItem
import com.lswarss.ing_project.fragments.PostsApiStatus

@BindingAdapter("listData")
fun bindRecyclerView(recyclerView: RecyclerView, data: List<PostItem>?){
val adapter = recyclerView.adapter as PostsAdapter
adapter.submitList(data)
}

@BindingAdapter("postApiStatus")
fun bindStatus(statusImageView: ImageView, status: PostsApiStatus?) {
when (status) {
PostsApiStatus.LOADING -> {
statusImageView.visibility = View.VISIBLE
statusImageView.setImageResource(R.drawable.loading_animation)
}
PostsApiStatus.ERROR -> {
statusImageView.visibility = View.VISIBLE
statusImageView.setImageResource(R.drawable.ic_connection_error)
}
PostsApiStatus.DONE -> {
statusImageView.visibility = View.GONE
}
}
}

I was trying to do this application using the app made in udacity course ***Mars Real Estate App***

Here is link to my app on github: https://github.com/LSWarss/ing_project
The code I have problems with is on branch new_architecture

Sory if this is stupid question but I&#39;m new to android and koltlin, or even posting on stack :)


</details>


# 答案1
**得分**: 1

由于在您的“PostsFragment”中,根据我从GitHub上看到的,您没有将适配器设置给RecyclerView,所以当绑定适配器尝试访问它时,它为“null”。

此外,我在您的主分支中注意到了以下内容:

viewModel.posts.observe(viewLifecycleOwner, Observer {
    posts -> recycler_view_posts.also{
    it.layoutManager = LinearLayoutManager(requireContext())
    it.setHasFixedSize(true)
    it.adapter = PostsAdapter(posts)
}

您不必每次观察到内容时都初始化RecyclerView。

在片段的“onViewCreated()”方法中,您可以设置适配器和布局管理器(如果在PostsFragment中添加,这也将解决您的错误):

private fun initRecyclerView(){
    recycler_view_posts.run{
        adapter = PostsAdapter(posts)
        layoutManager = LinearLayoutManager(requireContext())
        setHasFixedSize(true)
    }
}

然后在观察时,您只需使用new_architecture分支中的方法(adapter.submitList(list))刷新内容,或者通过将新列表设置到适配器中并调用adapter.notifyDataSetChange()来刷新内容。


<details>
<summary>英文:</summary>


       Caused by: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter data
       at com.lswarss.ing_project.adapters.BindingAdaptersKt.bindRecyclerView(Unknown Source:7

This is happening because as long as I saw from github, in your ``PostsFragment`` you are not setting the adapter to the recycler view, so it&#39;s ``null`` when the binding adapter is trying to access it.


Also, I noticed this in your master branch:

    viewModel.posts.observe(viewLifecycleOwner, Observer {
        posts -&gt; recycler_view_posts.also{
        it.layoutManager = LinearLayoutManager(requireContext())
        it.setHasFixedSize(true)
        it.adapter = PostsAdapter(posts)
    }
You don&#39;t have to initialize your recycler view each time you get something on observe. 

In ``onViewCreated()`` method from fragment you can set the adapter and the layoutManager (and this will also solve your error if you add it in PostsFragment):

    private fun initRecyclerView(){
        recycler_view_posts.run{
            adapter = PostsAdapter(posts)
            layoutManager = LinearLayoutManager(requireContext())
            setHasFixedSize(true)
        }
    }
and then on observe you only have to refresh the content with the approach from new_architecture branch (```adapter.submitList(list)```) or by setting the new list in adapter and calling ```adapter.notifyDataSetChange()```.

</details>



# 答案2
**得分**: 0

更改 `bindRecyclerView` 如下:

```kotlin
@BindingAdapter("listData")
fun bindRecyclerView(recyclerView: RecyclerView, data: List<PostItem>?){
    val adapter = recyclerView.adapter as PostsAdapter? // 更改为可为空类型
    adapter?.submitList(data) // 进行了空值检查
}
英文:

change bindRecyclerView to this:

@BindingAdapter(&quot;listData&quot;)
fun bindRecyclerView(recyclerView: RecyclerView, data: List&lt;PostItem&gt;?){
    val adapter  = recyclerView.adapter as PostsAdapter? // changed to nullable type
    adapter?.submitList(data)// nullablity checked
}

huangapple
  • 本文由 发表于 2020年4月11日 04:35:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/61148284.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定