dachristenson

dachristenson

Kotlin and Android Development featuring Jetpack: End Ch. 11 app runs but consistently crashes

I’ve got to the end of Ch. 11, and the app runs, with all tabs displaying what they should – at first. After switching around between Standings, Players, Leaders, etc., the app always eventually has problems reloading, for instance, Standings. Then, I’ll try to load Leaders, and it crashes with this error (as far as I can tell, it’s always mentioning the LeadersListFragment). Is this due to the “experimental” status of some of the packages imported, or is it something else? It happens on both my Samsung tablet and on the virtual Pixel 3a.

FATAL EXCEPTION: main
                                                                                                    Process: com.example.abl, PID: 13592
                                                                                                    androidx.fragment.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.abl.leaders.LeadersListFragment: could not find Fragment constructor
                                                                                                    	at androidx.fragment.app.Fragment.instantiate(Fragment.java:687)
                                                                                                    	at androidx.fragment.app.FragmentContainer.instantiate(FragmentContainer.java:57)
                                                                                                    	at androidx.fragment.app.FragmentManager$3.instantiate(FragmentManager.java:525)
                                                                                                    	at androidx.fragment.app.FragmentState.instantiate(FragmentState.java:84)
                                                                                                    	at androidx.fragment.app.FragmentStateManager.<init>(FragmentStateManager.java:91)
                                                                                                    	at androidx.fragment.app.FragmentManager.restoreSaveStateInternal(FragmentManager.java:2562)
                                                                                                    	at androidx.fragment.app.Fragment.restoreChildFragmentState(Fragment.java:1988)
                                                                                                    	at androidx.fragment.app.Fragment.onCreate(Fragment.java:1967)
                                                                                                    	at androidx.fragment.app.Fragment.performCreate(Fragment.java:3094)
                                                                                                    	at androidx.fragment.app.FragmentStateManager.create(FragmentStateManager.java:504)
                                                                                                    	at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:268)
                                                                                                    	at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1943)
                                                                                                    	at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1845)
                                                                                                    	at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1782)
                                                                                                    	at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:565)
                                                                                                    	at android.os.Handler.handleCallback(Handler.java:938)
                                                                                                    	at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                                    	at android.os.Looper.loop(Looper.java:246)
                                                                                                    	at android.app.ActivityThread.main(ActivityThread.java:8653)
                                                                                                    	at java.lang.reflect.Method.invoke(Native Method)
                                                                                                    	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
                                                                                                    	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
                                                                                                    Caused by: java.lang.NoSuchMethodException: com.example.abl.leaders.LeadersListFragment.<init> []
                                                                                                    	at java.lang.Class.getConstructor0(Class.java:2332)
                                                                                                    	at java.lang.Class.getConstructor(Class.java:1728)
                                                                                                    	at androidx.fragment.app.Fragment.instantiate(Fragment.java:672)
                                                                                                    	at androidx.fragment.app.FragmentContainer.instantiate(FragmentContainer.java:57) 
                                                                                                    	at androidx.fragment.app.FragmentManager$3.instantiate(FragmentManager.java:525) 
                                                                                                    	at androidx.fragment.app.FragmentState.instantiate(FragmentState.java:84) 
                                                                                                    	at androidx.fragment.app.FragmentStateManager.<init>(FragmentStateManager.java:91) 
                                                                                                    	at androidx.fragment.app.FragmentManager.restoreSaveStateInternal(FragmentManager.java:2562) 
                                                                                                    	at androidx.fragment.app.Fragment.restoreChildFragmentState(Fragment.java:1988) 
                                                                                                    	at androidx.fragment.app.Fragment.onCreate(Fragment.java:1967) 
                                                                                                    	at androidx.fragment.app.Fragment.performCreate(Fragment.java:3094) 
                                                                                                    	at androidx.fragment.app.FragmentStateManager.create(FragmentStateManager.java:504) 
                                                                                                    	at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:268) 
                                                                                                    	at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1943) 
                                                                                                    	at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1845) 
                                                                                                    	at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1782) 
                                                                                                    	at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:565) 
                                                                                                    	at android.os.Handler.handleCallback(Handler.java:938) 
                                                                                                    	at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                                                    	at android.os.Looper.loop(Looper.java:246) 
                                                                                                    	at android.app.ActivityThread.main(ActivityThread.java:8653) 
                                                                                                    	at java.lang.reflect.Method.invoke(Native Method) 
                                                                                                    	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602) 
                                                                                                    	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130) 
2023-12-06 14:58:29.915 13592-13592 Process                 com.example.abl                      I  Sending signal. PID: 13592 SIG: 9

Marked As Solved

mfazio23

mfazio23

Author of Kotlin and Android Development featuring Jetpack

Alright, this took me a bit since my original app still works as written, so I’m thinking this is due to a change in one of the navigation library. It’s seems like it’s trying to be helpful and re-create your Fragment automatically, but since it doesn’t know the leaderType it can’t.

I’d say the proper approach here is to send the leaderType as an argument to the LeadersListFragment rather than as a constructor parameter:

val leaderType = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
    arguments?.getSerializable(LEADER_TYPE_KEY, LeaderType::class.java)
} else {
    arguments?.getSerializable(LEADER_TYPE_KEY) as? LeaderType
} ?: LeaderType.Batting

You can then remove leaderType from the constructor. Note that the extra version check logic is due to arguments?.getSerializable(LEADER_TYPE_KEY) being deprecated for API level 33, but our app supports lower versions.

You can also do this the easy way and make the constructor parameter nullable:

class LeadersListFragment(private val leaderType: LeaderType? = LeaderType.Batting)

Sorry this took a bit for me to get back to you!

Where Next?

Popular Pragmatic Bookshelf topics Top

New
johnp
Running the examples in chapter 5 c under pytest 5.4.1 causes an AttributeError: ‘module’ object has no attribute ‘config’. In particula...
New
jamis
The following is cross-posted from the original Ray Tracer Challenge forum, from a post by garfieldnate. I’m cross-posting it so that the...
New
swlaschin
The book has the same “Problem space/Solution space” diagram on page 18 as is on page 17. The correct Problem/Solution space diagrams ar...
New
nicoatridge
Hi, I have just acquired Michael Fazio’s “Kotlin and Android Development” to learn about game programming for Android. I have a game in p...
New
adamwoolhether
Is there any place where we can discuss the solutions to some of the exercises? I can figure most of them out, but am having trouble with...
New
AufHe
I’m a newbie to Rails 7 and have hit an issue with the bin/Dev script mentioned on pages 112-113. Iteration A1 - Seeing the list of prod...
New
mcpierce
@mfazio23 I’ve applied the changes from Chapter 5 of the book and everything builds correctly and runs. But, when I try to start a game,...
New
SlowburnAZ
Getting an error when installing the dependencies at the start of this chapter: could not compile dependency :exla, "mix compile" failed...
New
dachristenson
@mfazio23 Android Studio will not accept anything I do when trying to use the Transformations class, as described on pp. 140-141. Googl...
New

Other popular topics Top

siddhant3030
I’m thinking of buying a monitor that I can rotate to use as a vertical monitor? Also, I want to know if someone is using it for program...
New
AstonJ
We have a thread about the keyboards we have, but what about nice keyboards we come across that we want? If you have seen any that look n...
New
PragmaticBookshelf
Build highly interactive applications without ever leaving Elixir, the way the experts do. Let LiveView take care of performance, scalabi...
New
AstonJ
Seems like a lot of people caught it - just wondered whether any of you did? As far as I know I didn’t, but it wouldn’t surprise me if I...
New
rustkas
Intensively researching Erlang books and additional resources on it, I have found that the topic of using Regular Expressions is either c...
New
PragmaticBookshelf
Author Spotlight Jamis Buck @jamis This month, we have the pleasure of spotlighting author Jamis Buck, who has written Mazes for Prog...
New
PragmaticBookshelf
Author Spotlight Mike Riley @mriley This month, we turn the spotlight on Mike Riley, author of Portable Python Projects. Mike’s book ...
New
PragmaticBookshelf
Author Spotlight Rebecca Skinner @RebeccaSkinner Welcome to our latest author spotlight, where we sit down with Rebecca Skinner, auth...
New
PragmaticBookshelf
Author Spotlight: Tammy Coron @Paradox927 Gaming, and writing games in particular, is about passion, vision, experience, and immersio...
New
AstonJ
This is cool! DEEPSEEK-V3 ON M4 MAC: BLAZING FAST INFERENCE ON APPLE SILICON We just witnessed something incredible: the largest open-s...
New

Sub Categories: