'The problem of the applicaion keep stopping [duplicate]
I have faced the problem of my application always show keep stopping. I try to see the terminal and it show me the problem is not been initialized. However, I have found many ways but still cannot show it. Can somebody help me? Thank for your help
Below are my original code which problem out :
package com.example.assignment_mad
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.ViewPager2
import com.example.assignment_mad.databinding.ActivityMainBinding
import com.google.android.material.imageview.ShapeableImageView
import com.google.android.material.tabs.TabLayout
import kotlinx.android.synthetic.*
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.fragment_notification_.*
class Notification_Fragment : Fragment( ) {
private lateinit var newRecyclerView: RecyclerView
private lateinit var newArrayList: ArrayList<Company>
private lateinit var tempArrayList: ArrayList<Company>
lateinit var imageId:Array<Int>
lateinit var heading:Array<String>
lateinit var news:Array<String>
private var layoutManager: RecyclerView.LayoutManager? = null
private var adapter: RecyclerView.Adapter<NotificationAdapter.MyViewHolder>? = null
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_notification_, container, false)
}
override fun onViewCreated(itemView: View, savedInstanceState: Bundle?) {
imageId= arrayOf(
R.drawable.company_logo_1,
R.drawable.company_logo_2,
R.drawable.company_logo_3,
R.drawable.company_logo_4,
R.drawable.company_logo_5,
R.drawable.company_logo_6,
R.drawable.company_logo_7,
R.drawable.company_logo_8,
R.drawable.company_logo_9,
R.drawable.company_logo_10
)
heading= arrayOf(
"Candidate Biden Called Saudi Arable a Pareft eaft.",
"The definitive text on the healing powers of the mind/body connection.",
"This is the definitive book on mindfulness from the beloved Zen master.",
"A timeless classic in personal development.",
"The definitive text on the healing powers of the mind/body connection.",
"The definitive text on the healing powers of the mind/body connection.",
"The definitive text on the healing powers of the mind/body connection.",
"The definitive text on the healing powers of the mind/body connection.",
"The definitive text on the healing powers of the mind/body connection.",
"The definitive text on the healing powers of the mind/body connection."
)
news= arrayOf(
getString(R.string.news_a),
getString(R.string.news_b),
getString(R.string.news_c),
getString(R.string.news_d),
getString(R.string.news_e),
getString(R.string.news_f),
getString(R.string.news_g),
getString(R.string.news_h),
getString(R.string.news_i),
getString(R.string.news_j)
)
newArrayList= arrayListOf<Company>()
tempArrayList= arrayListOf<Company>() //it show here get error
getUserdata()
super.onViewCreated(itemView, savedInstanceState)
newRecyclerView.apply {
layoutManager=LinearLayoutManager(activity)
newRecyclerView=findViewById(R.id.recyclerView)
newRecyclerView.setHasFixedSize(true)
}
recyclerView.apply {
// set a LinearLayoutManager to handle Android
// RecyclerView behavior
layoutManager = LinearLayoutManager(activity)
// set the custom adapter to the RecyclerView
adapter = NotificationAdapter(newArrayList)
}
}
private fun getUserdata() {
for (i in imageId.indices){
val company=Company(imageId[i],heading[i])
newArrayList.add(company)
}
newRecyclerView.adapter=NotificationAdapter(newArrayList) //it show here get error
}
}
Below are my problem showing out :
kotlin.UninitializedPropertyAccessException: lateinit property newRecyclerView has not been initialized
at com.example.assignment_mad.Notification_Fragment.getUserdata(Notification_Fragment.kt:122)
at com.example.assignment_mad.Notification_Fragment.onViewCreated(Notification_Fragment.kt:89)
Below are my Notification Adapter :
package com.example.assignment_mad
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.imageview.ShapeableImageView
class NotificationAdapter(private val companyList:ArrayList<Company>): RecyclerView.Adapter<NotificationAdapter.MyViewHolder>() {
private lateinit var mListener:onItemClickListener
interface onItemClickListener{
fun onItemClick(position: Int)
}
fun setOnItemClickListener(listener: onItemClickListener){
mListener=listener
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val itemView=
LayoutInflater.from(parent.context).inflate(R.layout.fragment_notification_,parent,false)
return MyViewHolder(itemView,mListener)
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val currentItem=companyList[position]
holder.titleImage.setImageResource(currentItem.titleImage)
holder.tvHeading.text=currentItem.heading
}
override fun getItemCount(): Int {
return companyList.size
}
//to insert the post detail
class MyViewHolder(itemView: View, listener: onItemClickListener): RecyclerView.ViewHolder(itemView){
val titleImage: ShapeableImageView =itemView.findViewById(R.id.title_image)
val tvHeading: TextView =itemView.findViewById(R.id.tvHeading)
init {
itemView.setOnClickListener{
listener.onItemClick(adapterPosition)
}
}
}
}
Solution 1:[1]
Try to findViewById before apply
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
var views = inflater.inflate(R.layout.fragment_notification_, container, false)
newRecyclerView=views.findViewById(R.id.recyclerView)
return views
}
...
newRecyclerView.apply {
layoutManager=LinearLayoutManager(activity)
newRecyclerView.setHasFixedSize(true)
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
