首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android Studio Kotlin: RecyclerView不能为空RuntimeException

Android Studio Kotlin: RecyclerView不能为空RuntimeException
EN

Stack Overflow用户
提问于 2021-03-01 20:45:14
回答 2查看 133关注 0票数 0

我一直在尝试解决在运行时抛出的问题,其中我使用的recyclerview为空。从我在网上看到的这个错误消息的大多数例子中,它通常是在片段中使用RecyclerView时出现的。这个RecyclerView只是在正常的Kotlin活动中使用。

打开OrderActivity.kt时出错

代码语言:javascript
运行
复制
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.pos, PID: 6004
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pos/com.example.pos.OrderActivity}: java.lang.IllegalStateException: orderRecyclerView must not be null
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
     Caused by: java.lang.IllegalStateException: orderRecyclerView must not be null
        at com.example.pos.OrderActivity.onCreate(OrderActivity.kt:19)
        at android.app.Activity.performCreate(Activity.java:7009)
        at android.app.Activity.performCreate(Activity.java:7000)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6494) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

OrderActivity.kt

代码语言:javascript
运行
复制
package com.example.pos

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.multiplerecyclerview.OrderAdapter
import kotlinx.android.synthetic.main.activity_order.*

class OrderActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        // Adapter class is initialized and list is passed in the param.
        val orderAdapter = OrderAdapter(this, getItemsList())

        //Set the LayoutManager that this RecyclerView will use.
        orderRecyclerView.layoutManager = LinearLayoutManager(this)

        //adapter instance is set to the recyclerview to inflate the items.
        orderRecyclerView.adapter = orderAdapter
    }

    private fun getItemsList(): ArrayList {
        val list = ArrayList()

        list.add(DataModel("Romana","1","12.50", "Pepperoni", "Aubergine", "Ex Mozz.", "Salami", OrderAdapter.TOPPINGS_4))
        list.add(DataModel("American","1","12.50", viewType = OrderAdapter.NO_TOPPING))

        return list
    }
}

OrderAdapter.kt

代码语言:javascript
运行
复制
package com.example.multiplerecyclerview

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.example.pos.DataModel
import com.example.pos.R
import kotlinx.android.synthetic.main.food_item.view.*
import kotlinx.android.synthetic.main.food_item_4.view.*


class OrderAdapter(val context: Context, val items: ArrayList) :
    RecyclerView.Adapter() {

    companion object {
        const val NO_TOPPING = 1
        const val TOPPINGS_4 = 4
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {

        if(viewType == NO_TOPPING) {
            return ViewHolder(
                LayoutInflater.from(context).inflate(
                    R.layout.food_item,
                    parent,
                    false
                )
            )
        } else if (viewType == TOPPINGS_4) {
            return ViewHolder4(
                LayoutInflater.from(context).inflate(
                    R.layout.food_item_4,
                    parent,
                    false
                )
            )
        } else {
            return ViewHolder(
                LayoutInflater.from(context).inflate(
                    R.layout.food_item,
                    parent,
                    false
                )
            )
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val item = items.get(position)

        if(holder is ViewHolder) {

            holder.productQuantity.text = item.itemName
            holder.productName.text = item.itemName
            holder.productPrice.text = item.itemPrice

        } else if(holder is ViewHolder4) {

            holder.productQuantity4.text = item.itemName
            holder.productName4.text = item.itemName
            holder.productPrice4.text = item.itemPrice

            holder.topping1.text = item.topping1
            holder.topping2.text = item.topping2
            holder.topping3.text = item.topping3
            holder.topping4.text = item.topping4
        }

    }

    override fun getItemViewType(position: Int): Int {
        return items[position].viewType
    }

    override fun getItemCount(): Int {
        return items.size
    }

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val productQuantity = view.productQuantityView
        val productName = view.productNameView
        val productPrice = view.productPriceView
    }

    class ViewHolder4(view: View) : RecyclerView.ViewHolder(view) {
        val productQuantity4 = view.productQuantityView4
        val productName4 = view.productNameView4
        val productPrice4 = view.productPriceView4

        val topping1 = view.topping1View
        val topping2 = view.topping2View
        val topping3 = view.topping3View
        val topping4 = view.topping4View
    }
}

DataModel.kt

代码语言:javascript
运行
复制
package com.example.pos

data class DataModel(val itemName: String, val itemQuantity: String, val itemPrice: String, val topping1: String? = null,  val topping2: String? = null,  val topping3: String? = null,  val topping4: String? = null, val viewType: Int)

活动

_

order.xml

代码语言:javascript
运行
复制
food_item.xml



    

        

        

        

    



food_item4.xml
EN

Stack Overflow用户

发布于 2021-03-01 21:00:01

我认为问题在于您在OrderActivity中使用了错误的布局。看看这一行:

代码语言:javascript
运行
复制
setContentView(R.layout.activity_main)

它应该是:

代码语言:javascript
运行
复制
setContentView(R.layout.activity_order)
票数 2
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66422417

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档