我正在Kotlin中尝试使用GSON和Volley解析JSON,这是我第一次使用这些工具,可能需要一些帮助。我遇到以下错误
2022-04-17 20:57:13.967 24411-24411/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 24411
java.lang.NullPointerException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkNotNullParameter, parameter spellList
at com.example.myapplication.SpellAdapter.<init>(SpellAdapter.kt)
at com.example.myapplication.RuleActivity.getDataSpells$lambda-1(RuleActivity.kt:84)
at com.example.myapplication.RuleActivity.$r8$lambda$OVOQfC5FGUmxexEtOchBO7ewf4I(RuleActivity.kt)
以下是我的活动中的相关片段
fun getDataSpells(){
val rvSpellData = findViewById<RecyclerView>(R.id.rvSpellData)
val urlSpells = "https://www.dnd5eapi.co/api/spells/fireball"
val queue = Volley.newRequestQueue(this)
val request = StringRequest(Request.Method.GET,urlSpells, { response ->
val data = response.toString()
Log.d("Logs", data.toString())
val SpellReportModel = Gson().fromJson(data, SpellReportModel::class.java)
Log.d("Logs", SpellReportModel.toString())
rvSpellData.adapter = SpellAdapter(SpellReportModel.SpellReportList)
rvSpellData.layoutManager = LinearLayoutManager(this)
}, {
Toast.makeText(this,"Error",Toast.LENGTH_SHORT).show()
})
queue.add(request)
}
我的模型类
data class SpellReportModel(
var SpellReportList: ArrayList<SpellReport>
)
data class SpellReport(
@SerializedName("_id")
var id: String,
@SerializedName("index")
var index: String,
@SerializedName("name")
var name: String,
)
还有我的适配器
class SpellAdapter(private val spellList: List<SpellReport>): RecyclerView.Adapter<SpellAdapter.SpellAdapterViewHolder>(){
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SpellAdapterViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.activity_rule,
parent, false)
return SpellAdapterViewHolder(itemView)
}
override fun onBindViewHolder(holder: SpellAdapterViewHolder, position: Int) {
val currentSpell = spellList[position]
holder.spellName.text = currentSpell.name
//holder.spellDesc.text = currentSpell.desc
//holder.spellRange.text = currentSpell.range
}
override fun getItemCount() = spellList.size
class SpellAdapterViewHolder(itemView: View): RecyclerView.ViewHolder(itemView){
val spellName: TextView = itemView.findViewById(R.id.tv_SpellName)
val spellDesc: TextView = itemView.findViewById(R.id.tv_SpellDesc)
val spellRange: TextView = itemView.findViewById(R.id.tv_SpellRange)
}
}
我觉得我的课程有些不对劲,但是即使我看了一大堆的教程,我还是没能把它弄清楚。任何帮助都将非常感谢。
感谢您抽出宝贵时间。
1条答案
按热度按时间r7s23pms1#
我假设你想得到一个咒语列表。在这种情况下,你的请求网址是错误的,它应该是:
您的模型类应该如下所示: