Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android – Return type of ‘getItem’ is not a subtype of the return type of the overridden member ‘@NonNull public abstract fun getItem(p0: Int)’

Posted on November 11, 2022 By

Solution 1 :

you should promisss the position is in 0..3 ,otherwise throw an IllArgumentException to tell you the wrong

Solution 2 :

when you remove the ? sign, and return getItem(position) in stead of null, it will fix.

import android.content.Context
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter

class MyAdapter(private val myContext: Context, fm: FragmentManager, internal 
var totalTabs: 
Int) : FragmentPagerAdapter(fm) {

// this is for fragment tabs
 override fun getItem(position: Int): Fragment {
when (position) {
    0 -> {
        //  val homeFragment: HomeFragment = HomeFragment()
        return GenelFragment()
    }
    1 -> {
        return GovdeFragment()
    }
    2 -> {
        // val movieFragment = MovieFragment()
        return InisFragment()
    }
    3 -> {
        // val movieFragment = MovieFragment()
        return UcusFragment()

   else->{throw IllegalStateException("$position is illegal") } 
  }
 }

Problem :

I am receiving error which is coming from Fragment.

I have changed android.support.v4.app.Fragment to androidx.fragment.app.Fragment and have gotten it down to this one error. Any help would be greatly appreciated.

import android.content.Context
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter

class MyAdapter(private val myContext: Context, fm: FragmentManager, internal var totalTabs: 
Int) : FragmentPagerAdapter(fm) {

// this is for fragment tabs
override fun getItem(position: Int): Fragment? {
    when (position) {
        0 -> {
            //  val homeFragment: HomeFragment = HomeFragment()
            return GenelFragment()
        }
        1 -> {
            return GovdeFragment()
        }
        2 -> {
            // val movieFragment = MovieFragment()
            return InisFragment()
        }
        3 -> {
            // val movieFragment = MovieFragment()
            return UcusFragment()

       else -> return  null
    }
}

How can we fix error there. I believe it has to do with the import, but ı didn’t fix.

Comments

Comment posted by Silence

the code:else->{throw IllegalStateException(“$position is illegal”)}

Comment posted by al3c

doesn’t this go in an infinite recursion if position is not 0, 1, 2 or 3? In case it never happens, then just throw an exception.

Comment posted by al3c

“In case it never happens, then just throw an exception”

READ  [FIXED] kotlin - Jetpack Compose and Android Room compatibility
Powered by Inline Related Posts

Android Tags:android, kotlin

Post navigation

Previous Post: [FIXED] java – The Android LoaderManager is deprecated. Now what?
Next Post: [FIXED] android – Is WorkManager the right solution for me?

Related Posts

[FIXED] reactjs – react-native apk not working on the android device Android
[FIXED] How to remove T and Zone from localdatetime in android Android
[FIXED] androids studio sqlite issue with selectionArgs argument Android
[FIXED] android – greyed small icon in header of notification Android
[FIXED] android – ViewPager2 error androidx….RecycleView.Adapter) in ViewPager2 can not be applied Android
[FIXED] android – HMS FusedLocationProvider and HuaweiMap is not working Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What is the rising action in Julius Caesar?
  • How do you secure a rope to itself?
  • Does waterproof laminate scratch easily?
  • What makes a building prewar?
  • What can you learn in a month without alcohol?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme