0

I'm a beginner in Kotlin, and I'm trying to make an application. Following a tutorial on YouTube on making an application using fragments and a navigation component. The only activity I have is the main activity. I'll post a screenshot of all the files I have thus far:

My project

So, I think I've done literally everything yet nothing still seems to work. I've added all the navigation components in my Gradle Module:

    //navigation component for nav_graph
    def nav_version = "2.5.3"
    // Kotlin
    implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
    implementation "androidx.navigation:navigation-ui-ktx:$nav_version"

I've added the NavHost into my activity_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"
    tools:context=".MainActivity">

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/nav_graph">
    </fragment>

</androidx.constraintlayout.widget.ConstraintLayout>

And all I'm actually trying to do is open up my splash screen, the login and sign up pages.

package com.example.qpayapp.fragments

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.navigation.NavController
import androidx.navigation.Navigation
import com.example.qpayapp.R
import com.google.firebase.auth.FirebaseAuth

class SplasshFragment : Fragment() {

    private lateinit var  auth: FirebaseAuth
    private lateinit var navController: NavController

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_splassh, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        auth = FirebaseAuth.getInstance()
        navController = Navigation.findNavController(view)
        Handler(Looper.myLooper()!!).postDelayed(Runnable {
           if (auth.currentUser != null){
               navController.navigate(R.id.action_splasshFragment_to_dashboardFragment)
           }else{
               navController.navigate(R.id.action_splasshFragment_to_loginFragment)
           }
        } ,2000)

    }
}

I've literally tried everything I could and have been sitting at this for about 3 days now, restarting full projects and even making some unusable at this point because of how much I've torn everything down. My hunch was that something was wrong with the navigation component, but I've tried several times to fix it but nothing really works. I downloaded the tutorial code from GitHub and it seems like theirs is working just fine, and when comparing codes I find literally no difference. Was hoping if I could get some guidance on this is possible. Will be happy to share more snippets of the code if they are lacking (I just included the ones I thought were the most important).

  • 1
    I think your first order of business would be to learn about stack traces and how to use them to debug code. The stack trace will tell you exactly why your code is crashing and then you just need to work backwards through your logic to figure out how it got into that state you weren't expecting. You don't need to use trial and error or have hunches. Read here: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this If after this you still can't figure it out, you can edit your question to include the stack trace. No one will help without seeing it. – Tenfour04 May 17 '23 at 01:28
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community May 18 '23 at 16:57

0 Answers0