javaandroidkotlinfragmentandroid-safe-args

When i use FragmentArgs i get this error 'java.lang.RuntimeException: java.lang.reflect.InvocationTargetException'


Fragment Class

Did make my model class Serializable. Getting the args from another fragment.

class AnimeFragment : Fragment() {
    private lateinit var binding: FragmentAnimeBinding
    val args by navArgs<AnimeFragmentArgs>()

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentAnimeBinding.inflate(inflater, container, false)
        println(args)
        binding.animeBgDisc.text = args.anime.background
        binding.animeTitle.text = args.anime.titles[0].title
        Glide
            .with(this)
            .load(args.anime.trailer.images.maximum_image_url)
            .placeholder(R.drawable.place_holder)
            .into(binding.animeMaxImg)

        return binding.root
    }

}

Navigating from this fragment

    private fun setRecyclerView(anime: List<Anime>) {
        val adapter = AnimeListAdapter(anime) {
            // onClickListener
            val bundle = Bundle().apply {
                putSerializable("anime", it)
            }
            findNavController().navigate(
                R.id.action_animeListFragment_to_animeFragment,
                bundle
            )
        }
        binding.rvAnimeList.adapter = adapter
    }

dependencies

The FragmentArgs was not generated after reBuilding the app. So, i added these dependencies and then after that FragmentArgs class was generated but got the java.lang.reflect.InvocationTargetException error

buildscript {
repositories {
    google()
}
dependencies {
    def nav_version = "2.5.2"
    classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
}

} id 'androidx.navigation.safeargs'

Logcat

`

FATAL EXCEPTION: main
                                                                                                Process: com.example.watchlist, PID: 16344
                                                                                                java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
                                                                                                    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:558)
                                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
                                                                                                Caused by: java.lang.reflect.InvocationTargetException
                                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                                    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
                                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936) 
                                                                                                Caused by: java.lang.reflect.InvocationTargetException
                                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                                    at androidx.navigation.NavArgsLazy.getValue(NavArgsLazy.kt:54)
                                                                                                    at androidx.navigation.NavArgsLazy.getValue(NavArgsLazy.kt:35)
                                                                                                    at com.example.watchlist.ui.anime.AnimeFragment.getArgs(AnimeFragment.kt:17)
                                                                                                    at com.example.watchlist.ui.anime.AnimeFragment.onViewCreated(AnimeFragment.kt:31)
                                                                                                    at androidx.fragment.app.Fragment.performViewCreated(Fragment.java:3128)
                                                                                                    at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:552)
                                                                                                    at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:261)
                                                                                                    at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1890)
                                                                                                    at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1814)
                                                                                                    at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1751)
                                                                                                    at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:538)
                                                                                                    at android.os.Handler.handleCallback(Handler.java:942)
                                                                                                    at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                                    at android.os.Looper.loopOnce(Looper.java:201)
                                                                                                    at android.os.Looper.loop(Looper.java:288)
                                                                                                    at android.app.ActivityThread.main(ActivityThread.java:7898)
                                                                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                                                                    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) 
                                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936) 
                                                                                                Caused by: java.lang.ClassCastException: java.lang.Integer cannot be cast to com.example.watchlist.api.apiResponse.Anime
                                                                                                    at com.example.watchlist.ui.anime.AnimeFragmentArgs.fromBundle(AnimeFragmentArgs.java:39)
                                                                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                                                                    at androidx.navigation.NavArgsLazy.getValue(NavArgsLazy.kt:54) 
                                                                                                    at androidx.navigation.NavArgsLazy.getValue(NavArgsLazy.kt:35) 
                                                                                                    at com.example.watchlist.ui.anime.AnimeFragment.getArgs(AnimeFragment.kt:17) 
                                                                                                    at com.example.watchlist.ui.anime.AnimeFragment.onViewCreated(AnimeFragment.kt:31) 
                                                                                                    at androidx.fragment.app.Fragment.performViewCreated(Fragment.java:3128) 
                                                                                                    at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:552) 
                                                                                                    at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:261) 
                                                                                                    at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1890) 
                                                                                                    at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1814) 
                                                                                                    at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1751) 
                                                                                                    at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:538) 
                                                                                                    at android.os.Handler.handleCallback(Handler.java:942) 
                                                                                                    at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                                                    at android.os.Looper.loopOnce(Looper.java:201) 
                                                                                                    at android.os.Looper.loop(Looper.java:288) 
                                                                                                    at android.app.ActivityThread.main(ActivityThread.java:7898) 
                                                                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                                                                    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) 
                                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936) 

`


Solution

  • Nvm i resolve it, the reason was i used the <fragment /> tag for my navHost file inside my main activity xml file. Which for some reason, gives this error. So, it replace it with

    <androidx.fragment.app.FragmentContainerView />
    

    and it works. Still don't know why <fragment /> tag gives error.