>我正在开发新的,我正在从资产文件夹中读取 JSON 文件,但我收到以下异常
java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference
at yodgorbek.komilov.runtastictask1.CustomAdapter.getView(CustomAdapter.kt:45)
at android.widget.AbsListView.obtainView(AbsListView.java:2365)
at android.widget.ListView.makeAndAddView(ListView.java:2052)
at android.widget.ListView.fillDown(ListView.java:786)
at android.widget.ListView.fillFromTop(ListView.java:847)
at android.widget.ListView.layoutChildren(ListView.java:1826)
at android.widget.AbsListView.onLayout(AbsListView.java:2164)
at android.view.View.layout(View.java:19828)
at android.view.ViewGroup.layout(ViewGroup.java:6154)
at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1915)
at android.view.View.layout(View.java:19828)
at android.view.ViewGroup.layout(ViewGroup.java:6154)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:19828)
at android.view.ViewGroup.layout(ViewGroup.java:6154)
at androidx.appcompat.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:446)
at android.view.View.layout(View.java:19828)
at android.view.ViewGroup.layout(ViewGroup.java:6154)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:19828)
at android.view.ViewGroup.layout(ViewGroup.java:6154)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
at android.view.View.layout(View.java:19828)
at android.view.ViewGroup.layout(ViewGroup.java:6154)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:765)
at android.view.View.layout(View.java:19828)
at android.view.ViewGroup.layout(ViewGroup.java:6154)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2643)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2348)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1482)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7124)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1008)
at android.view.Choreographer.doCallbacks(Choreographer.java:804)
at android.view.Choreographer.doFrame(Choreographer.java:732)
at android.view.Choreographer$FrameHandler.handleMessage(Choreographer.java:907)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:6662)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
在我的代码下面要点
您对组数据类中的成员使用了错误的键,它是数据,而在 json 中它显示键是成员使用以下类,它将起作用
data class Group(
@SerializedName("group_id")
val group_id: Int,
@SerializedName("group_name")
val group_name: String,
@SerializedName("members")
val members: List<Member>
)
您没有使用正确的密钥
data class Group(
@SerializedName("group_id")
val group_id: Int,
@SerializedName("group_name")
val group_name: String,
@SerializedName("data")
val members: List<Member>)
根据您的 json 架构,它应该是
data class Group(
@SerializedName("group_id")
val group_id: Int,
@SerializedName("group_name")
val group_name: String,
@SerializedName("members")
val members: List<Member>)