Reputation: 189
I have been trying to resolve an issue being thrown at runtime where the recyclerview I am using is null. From most examples of this error message I have seen online it is usually when using a RecyclerView is being used in a fragment. This RecyclerView is just being used in a normal Kotlin Activity.
Error When OrderActivity.kt is opened
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
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<DataModel> {
val list = ArrayList<DataModel>()
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
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<DataModel>) :
RecyclerView.Adapter<RecyclerView.ViewHolder>() {
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
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)
activity_order.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".OrderActivity">
<Button
android:id="@+id/customerButton"
android:layout_width="175dp"
android:layout_height="75dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="65dp"
android:text="Customer"
app:layout_constraintEnd_toStartOf="@+id/cancelOrderButton"
app:layout_constraintTop_toTopOf="parent" />
<Switch
android:id="@+id/sideOrderSwitch"
android:layout_width="128dp"
android:layout_height="45dp"
android:layout_marginStart="28dp"
android:layout_marginTop="95dp"
android:text="Side Orders"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/dealOneButton"
android:layout_width="132dp"
android:layout_height="71dp"
android:layout_marginStart="40dp"
android:layout_marginTop="72dp"
android:text="Deal One"
app:layout_constraintStart_toEndOf="@+id/sideOrderSwitch"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/dealTwoButton"
android:layout_width="132dp"
android:layout_height="71dp"
android:layout_marginStart="24dp"
android:layout_marginTop="72dp"
android:text="Deal Two"
app:layout_constraintStart_toEndOf="@+id/dealOneButton"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/dealThreeButton"
android:layout_width="132dp"
android:layout_height="71dp"
android:layout_marginStart="24dp"
android:layout_marginTop="72dp"
android:text="Deal Three"
app:layout_constraintStart_toEndOf="@+id/dealTwoButton"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/dealMaxButton"
android:layout_width="132dp"
android:layout_height="71dp"
android:layout_marginStart="24dp"
android:layout_marginTop="72dp"
android:layout_marginEnd="20dp"
android:text="Deal Max"
app:layout_constraintStart_toEndOf="@+id/dealThreeButton"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/cancelOrderButton"
android:layout_width="165dp"
android:layout_height="75dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="20dp"
android:text="Cancel Order"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/margButton"
android:layout_width="133dp"
android:layout_height="70dp"
android:layout_marginStart="28dp"
android:layout_marginTop="40dp"
android:text="Margherita"
android:textSize="12sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/sideOrderSwitch" />
<Button
android:id="@+id/americanButton"
android:layout_width="133dp"
android:layout_height="70dp"
android:layout_marginStart="32dp"
android:layout_marginTop="36dp"
android:text="American"
android:textSize="12sp"
app:layout_constraintStart_toEndOf="@+id/margButton"
app:layout_constraintTop_toBottomOf="@+id/dealOneButton" />
<Button
android:id="@+id/HawaiianButton"
android:layout_width="133dp"
android:layout_height="70dp"
android:layout_marginStart="32dp"
android:layout_marginTop="36dp"
android:text="Hawaiian"
android:textSize="12sp"
app:layout_constraintStart_toEndOf="@+id/americanButton"
app:layout_constraintTop_toBottomOf="@+id/dealTwoButton" />
<Button
android:id="@+id/romanaButton"
android:layout_width="133dp"
android:layout_height="70dp"
android:layout_marginStart="32dp"
android:layout_marginTop="36dp"
android:text="Romana"
android:textSize="12sp"
app:layout_constraintStart_toEndOf="@+id/HawaiianButton"
app:layout_constraintTop_toBottomOf="@+id/dealThreeButton" />
<Button
android:id="@+id/cheese4Button"
android:layout_width="133dp"
android:layout_height="70dp"
android:layout_marginStart="32dp"
android:layout_marginTop="36dp"
android:text="4 Cheeses"
android:textSize="12sp"
app:layout_constraintStart_toEndOf="@+id/romanaButton"
app:layout_constraintTop_toBottomOf="@+id/dealMaxButton" />
<Button
android:id="@+id/polloButton"
android:layout_width="133dp"
android:layout_height="70dp"
android:layout_marginStart="34dp"
android:layout_marginTop="36dp"
android:text="Pollo"
android:textSize="12sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/margButton" />
<Button
android:id="@+id/melanzanaButton"
android:layout_width="133dp"
android:layout_height="70dp"
android:layout_marginStart="24dp"
android:layout_marginTop="36dp"
android:text="Melanzana"
android:textSize="12sp"
app:layout_constraintStart_toEndOf="@+id/polloButton"
app:layout_constraintTop_toBottomOf="@+id/americanButton" />
<Button
android:id="@+id/vegButton"
android:layout_width="133dp"
android:layout_height="70dp"
android:layout_marginStart="24dp"
android:layout_marginTop="36dp"
android:text="Veg Pizza"
android:textSize="12sp"
app:layout_constraintStart_toEndOf="@+id/melanzanaButton"
app:layout_constraintTop_toBottomOf="@+id/HawaiianButton" />
<Button
android:id="@+id/queryButton"
android:layout_width="133dp"
android:layout_height="70dp"
android:layout_marginStart="24dp"
android:layout_marginTop="36dp"
android:text="Query"
android:textSize="12sp"
app:layout_constraintStart_toEndOf="@+id/vegButton"
app:layout_constraintTop_toBottomOf="@+id/romanaButton" />
<Button
android:id="@+id/whiteRockButton"
android:layout_width="133dp"
android:layout_height="70dp"
android:layout_marginStart="24dp"
android:layout_marginTop="36dp"
android:text="White Rock"
android:textSize="12sp"
app:layout_constraintStart_toEndOf="@+id/queryButton"
app:layout_constraintTop_toBottomOf="@+id/cheese4Button" />
<Button
android:id="@+id/sorrentoButton"
android:layout_width="133dp"
android:layout_height="70dp"
android:layout_marginStart="24dp"
android:layout_marginTop="32dp"
android:text="Sorrento"
android:textSize="12sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/polloButton" />
<Button
android:id="@+id/dylanParkButton"
android:layout_width="133dp"
android:layout_height="70dp"
android:layout_marginStart="32dp"
android:layout_marginTop="32dp"
android:text="Dylan's Park"
android:textSize="12sp"
app:layout_constraintStart_toEndOf="@+id/sorrentoButton"
app:layout_constraintTop_toBottomOf="@+id/melanzanaButton" />
<Button
android:id="@+id/coliemoreButton"
android:layout_width="133dp"
android:layout_height="70dp"
android:layout_marginStart="32dp"
android:layout_marginTop="32dp"
android:text="Coliemore Harbour"
android:textSize="12sp"
app:layout_constraintStart_toEndOf="@+id/dylanParkButton"
app:layout_constraintTop_toBottomOf="@+id/vegButton" />
<Button
android:id="@+id/bullockButton"
android:layout_width="133dp"
android:layout_height="70dp"
android:layout_marginStart="32dp"
android:layout_marginTop="32dp"
android:text="Bullock Harbour"
android:textSize="12sp"
app:layout_constraintStart_toEndOf="@+id/coliemoreButton"
app:layout_constraintTop_toBottomOf="@+id/queryButton" />
<Button
android:id="@+id/fortyFootButton"
android:layout_width="133dp"
android:layout_height="70dp"
android:layout_marginStart="32dp"
android:layout_marginTop="32dp"
android:text="40 Foot"
android:textSize="12sp"
app:layout_constraintStart_toEndOf="@+id/bullockButton"
app:layout_constraintTop_toBottomOf="@+id/whiteRockButton" />
<Button
android:id="@+id/catsButton"
android:layout_width="133dp"
android:layout_height="70dp"
android:layout_marginStart="34dp"
android:layout_marginTop="36dp"
android:text="Cat's Ladder"
android:textSize="12sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/sorrentoButton" />
<Button
android:id="@+id/killineyHillButton"
android:layout_width="133dp"
android:layout_height="70dp"
android:layout_marginStart="24dp"
android:layout_marginTop="36dp"
android:text="Killiney Hill"
android:textSize="12sp"
app:layout_constraintStart_toEndOf="@+id/catsButton"
app:layout_constraintTop_toBottomOf="@+id/dylanParkButton" />
<Button
android:id="@+id/killineyBeachButton"
android:layout_width="133dp"
android:layout_height="70dp"
android:layout_marginStart="24dp"
android:layout_marginTop="36dp"
android:text="Killiney Beach"
android:textSize="12sp"
app:layout_constraintStart_toEndOf="@+id/killineyHillButton"
app:layout_constraintTop_toBottomOf="@+id/coliemoreButton" />
<Button
android:id="@+id/castleButton"
android:layout_width="133dp"
android:layout_height="70dp"
android:layout_marginStart="24dp"
android:layout_marginTop="36dp"
android:text="The Castle"
android:textSize="12sp"
app:layout_constraintStart_toEndOf="@+id/killineyBeachButton"
app:layout_constraintTop_toBottomOf="@+id/bullockButton" />
<Button
android:id="@+id/martelloButton"
android:layout_width="133dp"
android:layout_height="70dp"
android:layout_marginStart="24dp"
android:layout_marginTop="36dp"
android:text="Martello Tower"
android:textSize="12sp"
app:layout_constraintStart_toEndOf="@+id/castleButton"
app:layout_constraintTop_toBottomOf="@+id/fortyFootButton" />
<Button
android:id="@+id/loretoButton"
android:layout_width="133dp"
android:layout_height="70dp"
android:layout_marginStart="24dp"
android:layout_marginTop="32dp"
android:text="Loreto"
android:textSize="12sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/catsButton" />
<Button
android:id="@+id/vicoButton"
android:layout_width="133dp"
android:layout_height="70dp"
android:layout_marginStart="32dp"
android:layout_marginTop="32dp"
android:text="Vico"
android:textSize="12sp"
app:layout_constraintStart_toEndOf="@+id/loretoButton"
app:layout_constraintTop_toBottomOf="@+id/killineyHillButton" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/orderRecyclerView"
android:layout_width="633dp"
android:layout_height="575dp"
android:clipToPadding="false"
android:padding="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="@+id/fortyFootButton"
app:layout_constraintTop_toBottomOf="@+id/cancelOrderButton"
tools:listitem="@layout/food_item" />
</androidx.constraintlayout.widget.ConstraintLayout>
<!--android:onClick="insertItem" -->
food_item.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/cardViewItem1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:padding="10dp"
android:layout_margin="4dp"
app:cardElevation="10dp"
app:cardCornerRadius="8dp"
app:cardPreventCornerOverlap="false">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="12dp">
<TextView
android:id="@+id/productQuantityView"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:layout_marginLeft="10dp"
android:layout_marginEnd="15dp"
android:text="1"
android:textColor="@color/shopColour"
android:textSize="25sp"
android:textStyle="bold" />
<TextView
android:id="@+id/productNameView"
android:layout_width="wrap_content"
android:layout_height="46dp"
android:layout_marginEnd="10dp"
android:layout_toEndOf="@+id/productQuantityView"
android:text="12'' Killiney Beach"
android:textColor="@color/shopColour"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/productPriceView"
android:layout_width="wrap_content"
android:layout_height="52dp"
android:layout_marginEnd="20dp"
android:layout_toEndOf="@id/productNameView"
android:text="14.50"
android:textColor="@color/shopColour"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
food_item4.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/cardViewItem4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_margin="4dp"
app:cardElevation="10dp"
app:cardCornerRadius="8dp"
app:cardPreventCornerOverlap="false">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="12dp">
<TextView
android:id="@+id/productQuantityView4"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:layout_marginLeft="10dp"
android:layout_marginEnd="15dp"
android:text="1"
android:textColor="@color/shopColour"
android:textSize="25sp"
android:textStyle="bold" />
<TextView
android:id="@+id/productNameView4"
android:layout_width="wrap_content"
android:layout_height="46dp"
android:layout_marginEnd="10dp"
android:layout_toEndOf="@+id/productQuantityView4"
android:text="12'' Killiney Beach"
android:textColor="@color/shopColour"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/productPriceView4"
android:layout_width="wrap_content"
android:layout_height="52dp"
android:layout_marginEnd="20dp"
android:layout_toEndOf="@id/productNameView4"
android:text="14.50"
android:textColor="@color/shopColour"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/topping1View"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginStart="20dp"
android:layout_marginTop="3dp"
android:layout_toEndOf="@+id/productPriceView4"
android:text="Pepperoni"
android:textColor="@color/logoYellow"
android:textSize="14sp"
android:textStyle="bold" />
<TextView
android:id="@+id/topping2View"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/topping1View"
android:layout_marginStart="20dp"
android:layout_marginTop="13dp"
android:layout_toEndOf="@+id/productPriceView4"
android:text="Aubergine"
android:textColor="@color/logoYellow"
android:textSize="14sp"
android:textStyle="bold" />
<TextView
android:id="@+id/topping3View"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginStart="20dp"
android:layout_marginTop="3dp"
android:layout_toEndOf="@+id/topping1View"
android:text="Semi Dried Tomatos"
android:textColor="@color/logoYellow"
android:textSize="14sp"
android:textStyle="bold" />
<TextView
android:id="@+id/topping4View"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/topping1View"
android:layout_marginStart="20dp"
android:layout_marginTop="13dp"
android:layout_toEndOf="@+id/topping2View"
android:text="Smoked Applewood"
android:textColor="@color/logoYellow"
android:textSize="14sp"
android:textStyle="bold" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
Upvotes: 0
Views: 519
Reputation: 71
I think the problem is that you are using wrong layout in OrderActivity. Take a look at this line:
setContentView(R.layout.activity_main)
It should be:
setContentView(R.layout.activity_order)
Upvotes: 2
Reputation: 8191
setContentView(R.layout.activity_main)
this is the layout you're using in the create of your activity
but you've posted
activity_order.xml
and this is the file which contains the specific component you're referencing (the recyclerview)
so you need to change your setContentView in your activity to:
setContentView(R.layout.activity_order)
synthetic imports will (quite happily) add imports to resolve references, but those references could be to entirely different and irrelevant files.
you can see here:
import kotlinx.android.synthetic.main.activity_order.*
it is importing everything from this file, but that's not the file you've used inside your setContentView, so that's why it crashes
Upvotes: 3