NoSuchMethodError: No virtual method contains(I)Z in class Landroid/util/SparseArray;
Asked Answered
S

4

5

1.Whenever I am swiping for next picture in my app, app crashed and I don't know what's going wrong

2.This problem getting after update to Android 30 before its working fine

java.lang.NoSuchMethodError: No virtual method contains(I)Z in class Landroid/util/SparseArray; or its super classes (declaration of 'android.util.SparseArray' appears in /system/framework/framework.jar!classes2.dex)

class PostViewFragment : DialogFragment() {
private var player: SimpleExoPlayer? = null
private lateinit var mediaDataSourceFactory: DataSource.Factory
//var post: FeedNodeModel? = null
private val STORAGE_PERMISSION_CODE = 100
private var post: FeedNodeModel? = null

var currentPos = 0
var registeredFragments = SparseArray<SidecarPagerFragment>()
var isFirstLoad = true

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setStyle(STYLE_NORMAL, R.style.FullScreenDialogStyle)

    isCancelable = true
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    /*if (createdView != null) {
        return createdView
    }*/

    val view = inflater.inflate(R.layout.fragment_post_view, container, false)

    //post = arguments?.getSerializable("post") as FeedNodeModel?

    when (post?.__typename) {
        "GraphImage",
        "GraphStoryImage" -> {
            Picasso.get()
                    .load(post?.sourceUrl)
                    .into(view.img_post)
            view.img_post.visibility = View.VISIBLE
            view.playerView.visibility = View.GONE
            view.viewpager.visibility = View.GONE
        }
        "GraphVideo",
        "GraphStoryVideo" -> {
            Glide.with(requireContext())
                    .load(post?.thumbnailUrl)
                    .into(object : CustomTarget<Drawable>() {
                        override fun onLoadCleared(p0: Drawable?) {

                        }

                        override fun onResourceReady(p0: Drawable, p1: Transition<in Drawable>?) {
                            initializePlayer(view.playerView,
                                    post?.sourceUrl)
                        }
                    })
            view.img_post.visibility = View.GONE
            view.playerView.visibility = View.VISIBLE
            view.viewpager.visibility = View.GONE
        }
        "GraphSidecar" -> {
            val calHeight =
                    (resources.displayMetrics.widthPixels *
                            (post?.feedNodeModel?.get(0)?.dimenHeight!!)) /
                            post?.feedNodeModel?.get(0)?.dimenWidth!!

            view.txt_pager_count.text = "1/${post?.feedNodeModel?.size}"
            view.viewpager.layoutParams.height = calHeight
            view.viewpager.adapter = object : FragmentPagerAdapter(childFragmentManager,
                    FragmentStatePagerAdapter.POSITION_NONE) {
                override fun getItem(p0: Int): Fragment {
                    val fragment = SidecarPagerFragment.create(post?.feedNodeModel?.get(p0))
                    registeredFragments.put(p0, fragment)
                    if (isFirstLoad) {
                        fragment.isFirstLoad = true
                        isFirstLoad = false
                    }

                    return fragment
                }

                override fun getCount(): Int = post?.feedNodeModel?.size!!
            }

            view.viewpager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
                override fun onPageScrollStateChanged(state: Int) {

                }

                override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

                }

                @RequiresApi(Build.VERSION_CODES.R)
                override fun onPageSelected(position: Int) {
                    if (post?.feedNodeModel?.get(currentPos)?.__typename == "GraphVideo") {
                        registeredFragments[currentPos]?.onPause()
                    }

                    currentPos = position
                    view.txt_pager_count.text = "${currentPos+1}/${post?.feedNodeModel?.size}"

                    if (registeredFragments.contains(position)) {
                        registeredFragments[position]?.loadVideo()
                    }
                }
            })

            view.viewpager.visibility = View.VISIBLE
            view.img_post.visibility = View.GONE
            view.playerView.visibility = View.GONE
        }
    }

App Crash On This Line

 if (registeredFragments.contains(position)) {
                        registeredFragments[position]?.loadVideo()
                    }
Schumacher answered 20/8, 2020 at 15:36 Comment(0)
L
4

The intuitive method contains(int key) was actually introduced in API 30, so it will crash older API devices. Very new methods don't give you a handy warning like depreciated methods, making them considerably more hazardous. Even if you target below 30 Android Studio will still compile contains() without protest.

The reference gives the following:

public boolean contains (int key):
Returns true if the key exists in the array. This is equivalent to indexOfKey(int) >= 0

You could use the indexOfKey() test equivalent, although personally I prefer testing for null with get():

get(key) != null
Lundell answered 8/6, 2021 at 20:20 Comment(0)
S
3

Use containsKey

/** Returns true if the collection contains [key]. */
inline fun <T> SparseArray<T>.containsKey(key: Int) = indexOfKey(key) >= 0

Insight by my team leader Mike

Stoker answered 15/9, 2020 at 9:24 Comment(0)
S
1

I had same error, but with containtsKey() method. This was happening only for Huawei P30 Pro with Api 30. Looks like they don't support it. I couldn't use

get(key) != null

because null is one of possible values.

Also contains(key) has been added in Api 30 according to this:
https://developer.android.com/reference/android/util/SparseArray#contains(int)
In this case it is better to stick to

indexOfKey(int) >= 0

which is available since Api 1.

Spumescent answered 27/9, 2021 at 10:16 Comment(0)
P
0

Please use below code it worked with me.

 if (registeredFragments.containsKey(position)) {
                        registeredFragments[position]?.loadVideo()
                    }

hope it will work with you also

Profusive answered 20/8, 2020 at 15:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.