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!

Popular Prag Prog topics Top

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
gilesdotcodes
In case this helps anyone, I’ve had issues setting up the rails source code. Here were the solutions: In Gemfile, change gem 'rails' t...
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
fynn
This is as much a suggestion as a question, as a note for others. Locally the SGP30 wasn’t available, so I ordered a SGP40. On page 53, ...
New
jskubick
I think I might have found a problem involving SwitchCompat, thumbTint, and trackTint. As entered, the SwitchCompat changes color to hol...
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
oaklandgit
Hi, I completed chapter 6 but am getting the following error when running: thread 'main' panicked at 'Failed to load texture: IoError(O...
New
kolossal
Hi, I need some help, I’m new to rust and was learning through your book. but I got stuck at the last stage of distribution. Whenever I t...
New
Henrai
Hi, I’m working on the Chapter 8 of the book. After I add add the point_offset, I’m still able to see acne: In the image above, I re...
New
davetron5000
Hello faithful readers! If you have tried to follow along in the book, you are asked to start up the dev environment via dx/build and ar...
New

Other popular topics Top

foxtrottwist
Here’s our thread for the Keyboardio Atreus. It is a mechanical keyboard based on and a slight update of the original Atreus (Keyboardio ...
New
PragmaticBookshelf
Rust is an exciting new programming language combining the power of C with memory safety, fearless concurrency, and productivity boosters...
New
AstonJ
Thanks to @foxtrottwist’s and @Tomas’s posts in this thread: Poll: Which code editor do you use? I bought Onivim! :nerd_face: https://on...
New
Exadra37
On modern versions of macOS, you simply can’t power on your computer, launch a text editor or eBook reader, and write or read, without a ...
New
PragmaticBookshelf
“Finding the Boundaries” Hero’s Journey with Noel Rappin @noelrappin Even when you’re ultimately right about what the future ho...
New
New
foxtrottwist
A few weeks ago I started using Warp a terminal written in rust. Though in it’s current state of development there are a few caveats (tab...
New
Help
I am trying to crate a game for the Nintendo switch, I wanted to use Java as I am comfortable with that programming language. Can you use...
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: Karl Stolley @karlstolley Logic! Rhetoric! Prag! Wow, what a combination. In this spotlight, we sit down with Karl ...
New

Latest in PragProg

View all threads ❯