Search code examples
androidandroid-scrollview

How do I fix scrollview crashing Android app?


New app developer here. I started working on an app a few days ago. It worked completely fine until I replaced the default to scroll view. I'm trying to make a scroll view with a design similar to reddit, the endless boxes with a set size. After changing the code, I expected multiple boxes that are the same size in scrollview. Instead, it crashes immediately on startup.

I tried changing the dimensions on the scrollview and I tried adding child elements but its still not working for me

I get the following error message in logs:

 **2019-11-23 20:41:40.155 10846-10846/com.john.chanapi E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.john.chanapi, PID: 10846
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.john.chanapi/com.john.chanapi.MainActivity}: java.lang.IllegalStateException: Activity com.john.chanapi.MainActivity@1850938 does not have a NavController set on 2131230867
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3047)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3182)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1916)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6898)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.IllegalStateException: Activity com.john.chanapi.MainActivity@1850938 does not have a NavController set on 2131230867
at androidx.navigation.Navigation.findNavController(Navigation.java:61)
at androidx.navigation.ActivityKt.findNavController(Activity.kt:30)
at com.john.chanapi.MainActivity.onCreate(MainActivity.kt:33)
at android.app.Activity.performCreate(Activity.java:7149)
at android.app.Activity.performCreate(Activity.java:7140)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1288)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3027)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3182) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1916) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:6898) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 

My content_main.xml file:

<?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/app_bar_main">
    <ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
    android:id="@+id/scrollview1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:divider="#312F2F"
    android:orientation="vertical" >
    <TextView
    android:id="@+id/textView2"
    android:layout_width="match_parent"
    android:layout_height="124dp"
    android:text="TextView" />
    <Button
    android:id="@+id/button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Button" />
    </LinearLayout>
    </ScrollView>
    </androidx.constraintlayout.widget.ConstraintLayout>**

Mainactivity.kt file:

    package com.john.chanapi

    import android.os.Bundle
    import com.google.android.material.floatingactionbutton.FloatingActionButton
    import com.google.android.material.snackbar.Snackbar
    import androidx.navigation.findNavController
    import androidx.navigation.ui.AppBarConfiguration
    import androidx.navigation.ui.navigateUp
    import androidx.navigation.ui.setupActionBarWithNavController
    import androidx.navigation.ui.setupWithNavController
    import androidx.drawerlayout.widget.DrawerLayout
    import com.google.android.material.navigation.NavigationView
    import androidx.appcompat.app.AppCompatActivity
    import androidx.appcompat.widget.Toolbar
    import android.view.Menu
    class MainActivity : AppCompatActivity() {

    private lateinit var appBarConfiguration: AppBarConfiguration

    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    val toolbar: Toolbar = findViewById(R.id.toolbar)
    setSupportActionBar(toolbar)

    val fab: FloatingActionButton = findViewById(R.id.fab)
    fab.setOnClickListener { view ->
    Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
    .setAction("Action", null).show()
    }
    val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
    val navView: NavigationView = findViewById(R.id.nav_view)
    val navController = findNavController(R.id.scrollview1)
    // Passing each menu ID as a set of Ids because each
    // menu should be considered as top level destinations.
    appBarConfiguration = AppBarConfiguration(
    setOf(
    R.id.nav_home, R.id.nav_new, R.id.nav_rising,
    R.id.nav_top, R.id.nav_inbox, R.id.nav_message
    ), drawerLayout
    )
    setupActionBarWithNavController(navController, appBarConfiguration)
    navView.setupWithNavController(navController)
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
    // Inflate the menu; this adds items to the action bar if it is present.
    menuInflater.inflate(R.menu.main, menu)
    return true
    }

    override fun onSupportNavigateUp(): Boolean {
    val navController = findNavController(R.id.scrollview1)
    return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
    }
    }

Any help or suggestions would help me out a ton. I've been stuck on this for hours. Thanks!


Solution

  • Based on the logs, it is crashing at findNavController which is this line in your MainActivity:
    val navController = findNavController(R.id.scrollview1).

    The activity is just inflating the views in onCreate, when you try to get NavController it will fail.

    One simple way to verify this is to comment/delete the code after this line in onCreate and check if the app crash.

    If this is the issue, you can move these code into onViewCreated where all the views are inflated.

    You can try this:

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val toolbar: Toolbar = findViewById(R.id.toolbar)
        setSupportActionBar(toolbar)
    }
    
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        val fab: FloatingActionButton = findViewById(R.id.fab)
        fab.setOnClickListener { view ->
        Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
            .setAction("Action", null).show()
        }
        val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
        val navView: NavigationView = findViewById(R.id.nav_view)
        val navController = findNavController(R.id.scrollview1)
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        appBarConfiguration = AppBarConfiguration(
            setOf(
                R.id.nav_home, R.id.nav_new, R.id.nav_rising,
                R.id.nav_top, R.id.nav_inbox, R.id.nav_message
            ), drawerLayout
        )
        setupActionBarWithNavController(navController, appBarConfiguration)
        navView.setupWithNavController(navController)
    }