Home > Software design >  Parameter specified as non-null is null :
Parameter specified as non-null is null :

Time:11-27

Hello there recently convert my project from java to kotlin, and faced many issues, and this is one of them

I don't know what exactly is trying to say and neither its pointing to the correct line where the error is there because at line 12 where it pointing is "import com.example.myappnotfinal.R"

Error

Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkNotNullParameter, parameter menuInfo
        at com.example.myappnotfinal.AdaptersAndMore.PostAdapter$PostViewHolder.onCreateContextMenu(Unknown Source:12)

PostAdapter.kt

class PostAdapter     /* ShimmerFrameLayout shimmerFrameLayout; */(
    var mcontext: Context, var mUploads: List<Upload>
) : RecyclerView.Adapter<PostAdapter.PostViewHolder>() {
    private val postDiffUtil = PostDiffUtil()
    val postListDiffer = AsyncListDiffer(this, postDiffUtil)
    private var mListener: OnItemClickListener? = null
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PostViewHolder {
        val view: View = LayoutInflater.from(parent.context)
            .inflate(R.layout.post_item_container_profile, parent, false)
        return PostViewHolder(view)
    }

    override fun onBindViewHolder(holder: PostViewHolder, position: Int) {
        val uploadCurrent = postListDiffer.currentList[position]
        val shimmer = ColorHighlightBuilder()
            .setBaseColor(Color.parseColor("#F3F3F3"))
            .setBaseAlpha(1f)
            .setHighlightColor(Color.parseColor("#E7E7E7"))
            .setHighlightAlpha(1f)
            .setDropoff(50f)
            .build()
        val shimmerDrawable = ShimmerDrawable()
        shimmerDrawable.setShimmer(shimmer)
        Glide.with(mcontext)
            .load(uploadCurrent.getmImageUrl())
            .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
            .placeholder(shimmerDrawable)
            .centerCrop()
            .fitCenter()
            .into(holder.imageView)
    }

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

    fun setOnItemClickListener(listener: OnItemClickListener?) {
        mListener = listener
    }

    fun setUploads(upload: List<Upload>) {
        mUploads = upload
    }


    interface OnItemClickListener {
        fun onClick(view: View?)
        fun onItemClick(position: Int)
        fun onDeleteClick(position: Int)
    }

    inner class PostViewHolder internal constructor(itemView: View) :
        RecyclerView.ViewHolder(itemView), View.OnClickListener, OnCreateContextMenuListener,
        MenuItem.OnMenuItemClickListener {
        var imageView: ShapeableImageView = itemView.findViewById(R.id.imagePost)
        override fun onClick(v: View) {
            if (mListener != null) {
                val position = adapterPosition
                if (position != RecyclerView.NO_POSITION) {
                    mListener!!.onItemClick(position)
                }
            }
        }

        override fun onCreateContextMenu(menu: ContextMenu, v: View, menuInfo: ContextMenuInfo) {
            val delete = menu.add(Menu.NONE, 2, 2, "Delete")
            delete.setOnMenuItemClickListener(this)
        }

        override fun onMenuItemClick(item: MenuItem): Boolean {
            if (mListener != null) {
                val position = adapterPosition
                if (position != RecyclerView.NO_POSITION) {
                    if (item.itemId == 2) {
                        mListener!!.onDeleteClick(position)
                        return true
                    }
                }
            }
            return false
        }

        init {
            itemView.setOnClickListener(this)
            itemView.setOnCreateContextMenuListener(this)
        }
    }

    companion object
}

CodePudding user response:

Change override fun onCreateContextMenu(menu: ContextMenu, v: View, menuInfo: ContextMenuInfo) to override fun onCreateContextMenu(menu: ContextMenu, v: View, menuInfo: ContextMenuInfo?) as ContextMenuInfo can be nullable

  • Related