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

abtin
page 20: … protoc command… I had to additionally run the following go get commands in order to be able to compile protobuf code using go...
New
jon
Some minor things in the paper edition that says “3 2020” on the title page verso, not mentioned in the book’s errata online: p. 186 But...
New
iPaul
page 37 ANTLRInputStream input = new ANTLRInputStream(is); as of ANTLR 4 .8 should be: CharStream stream = CharStreams.fromStream(i...
New
brianokken
Many tasks_proj/tests directories exist in chapters 2, 3, 5 that have tests that use the custom markers smoke and get, which are not decl...
New
yulkin
your book suggests to use Image.toByteData() to convert image to bytes, however I get the following error: "the getter ‘toByteData’ isn’t...
New
mikecargal
Title: Hands-on Rust: question about get_component (page 295) (feel free to respond. “You dug you’re own hole… good luck”) I have somet...
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
Keton
When running the program in chapter 8, “Implementing Combat”, the printout Health before attack was never printed so I assumed something ...
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
dachristenson
I just bought this book to learn about Android development, and I’m already running into a major issue in Ch. 1, p. 20: “Update activity...
New

Other popular topics Top

brentjanderson
Bought the Moonlander mechanical keyboard. Cherry Brown MX switches. Arms and wrists have been hurting enough that it’s time I did someth...
New
AstonJ
poll poll Be sure to check out @Dusty’s article posted here: An Introduction to Alternative Keyboard Layouts It’s one of the best write-...
New
New
AstonJ
I ended up cancelling my Moonlander order as I think it’s just going to be a bit too bulky for me. I think the Planck and the Preonic (o...
New
AstonJ
I have seen the keycaps I want - they are due for a group-buy this week but won’t be delivered until October next year!!! :rofl: The Ser...
New
Rainer
Not sure if following fits exactly this thread, or if we should have a hobby thread… For many years I’m designing and building model air...
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
PragmaticBookshelf
Author Spotlight James Stanier @jstanier James Stanier, author of Effective Remote Work , discusses how to rethink the office as we e...
New
PragmaticBookshelf
Author Spotlight: Karl Stolley @karlstolley Logic! Rhetoric! Prag! Wow, what a combination. In this spotlight, we sit down with Karl ...
New
AstonJ
If you’re getting errors like this: psql: error: connection to server on socket “/tmp/.s.PGSQL.5432” failed: No such file or directory ...
New

Sub Categories: