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

Alexandr
Hi everyone! There is an error on the page 71 in the book “Programming machine learning from coding to depp learning” P. Perrotta. You c...
New
JohnS
I can’t setup the Rails source code. This happens in a working directory containing multiple (postgres) Rails apps. With: ruby-3.0.0 s...
New
adamwoolhether
When trying to generate the protobuf .go file, I receive this error: Unknown flag: --go_opt libprotoc 3.12.3 MacOS 11.3.1 Googling ...
New
brian-m-ops
#book-python-testing-with-pytest-second-edition Hi. Thanks for writing the book. I am just learning so this might just of been an issue ...
New
curtosis
Running mix deps.get in the sensor_hub directory fails with the following error: ** (Mix) No SSH public keys found in ~/.ssh. An ssh aut...
New
jskubick
I’m under the impression that when the reader gets to page 136 (“View Data with the Database Inspector”), the code SHOULD be able to buil...
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
digitalbias
Title: Build a Weather Station with Elixir and Nerves: Problem connecting to Postgres with Grafana on (page 64) If you follow the defau...
New
brunogirin
When I run the coverage example to report on missing lines, I get: pytest --cov=cards --report=term-missing ch7 ERROR: usage: pytest [op...
New
mert
AWDWR 7, page 152, page 153: Hello everyone, I’m a little bit lost on the hotwire part. I didn’t fully understand it. On page 152 @rub...
New

Other popular topics Top

Exadra37
I am thinking in building or buy a desktop computer for programing, both professionally and on my free time, and my choice of OS is Linux...
New
New
PragmaticBookshelf
“Finding the Boundaries” Hero’s Journey with Noel Rappin @noelrappin Even when you’re ultimately right about what the future ho...
New
Margaret
Hello content creators! Happy new year. What tech topics do you think will be the focus of 2021? My vote for one topic is ethics in tech...
New
AstonJ
If you are experiencing Rails console using 100% CPU on your dev machine, then updating your development and test gems might fix the issu...
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
wmnnd
Here’s the story how one of the world’s first production deployments of LiveView came to be - and how trying to improve it almost caused ...
New
First poster: joeb
The File System Access API with Origin Private File System. WebKit supports new API that makes it possible for web apps to create, open,...
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: VM Brasseur @vmbrasseur We have a treat for you today! We turn the spotlight onto Open Source as we sit down with V...
New

Sub Categories: