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

sdmoralesma
Title: Web Development with Clojure, Third Edition - migrations/create not working: p159 When I execute the command: user=&gt; (create-...
New
raul
Hi Travis! Thank you for the cool book! :slight_smile: I made a list of issues and thought I could post them chapter by chapter. I’m rev...
New
Mmm
Hi, build fails on: bracket-lib = “~0.8.1” when running on Mac Mini M1 Rust version 1.5.0: Compiling winit v0.22.2 error[E0308]: mi...
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
conradwt
First, the code resources: Page 237: rumbl_umbrella/apps/rumbl/mix.exs Note: That this file is missing. Page 238: rumbl_umbrella/app...
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
taguniversalmachine
Hi, I am getting an error I cannot figure out on my test. I have what I think is the exact code from the book, other than I changed “us...
New
EdBorn
Title: Agile Web Development with Rails 7: (page 70) I am running windows 11 pro with rails 7.0.3 and ruby 3.1.2p20 (2022-04-12 revision...
New
andreheijstek
After running /bin/setup, the first error was: The foreman' command exists in these Ruby versions: That was easy to fix: gem install fore...
New
ggerico
I got this error when executing the plot files on macOS Ventura 13.0.1 with Python 3.10.8 and matplotlib 3.6.1: programming_ML/code/03_...
New

Other popular topics Top

Rainer
My first contact with Erlang was about 2 years ago when I used RabbitMQ, which is written in Erlang, for my job. This made me curious and...
New
AstonJ
I’ve been hearing quite a lot of comments relating to the sound of a keyboard, with one of the most desirable of these called ‘thock’, he...
New
PragmaticBookshelf
Rust is an exciting new programming language combining the power of C with memory safety, fearless concurrency, and productivity boosters...
New
New
AstonJ
Do the test and post your score :nerd_face: :keyboard: If possible, please add info such as the keyboard you’re using, the layout (Qw...
New
Exadra37
Oh just spent so much time on this to discover now that RancherOS is in end of life but Rancher is refusing to mark the Github repo as su...
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
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
AstonJ
If you want a quick and easy way to block any website on your Mac using Little Snitch simply… File &gt; New Rule: And select Deny, O...
New
PragmaticBookshelf
Author Spotlight: Tammy Coron @Paradox927 Gaming, and writing games in particular, is about passion, vision, experience, and immersio...
New

Sub Categories: