Home > OS >  My Android Application keeps crashing. Kotlin and Android Studio
My Android Application keeps crashing. Kotlin and Android Studio

Time:06-05

I am new to Kotlin and Android Studio. I tried implementing Fragments and Navigation in my application. Following is my code. When the Application is run, it keeps on crashing. I don't understand where I ve made mistake. The application does not show any compilation error. MainActivity.kt : package com.example.firstandroid

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.example.firstandroid.databinding.ActivityMainBinding
import androidx.navigation.fragment.NavHostFragment
class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
    }

    // Get the navigation host fragment from this Activity
    val navHostFragment = supportFragmentManager
        .findFragmentById(R.id.nav_host_fragment) as NavHostFragment
    // Instantiate the navController using the NavHostFragment
    val navController = navHostFragment.navController
    // Make sure actions in the ActionBar get propagated to the NavController
}

StartOrder.kt :

package com.example.firstandroid

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.navigation.fragment.findNavController
import com.example.firstandroid.databinding.FragmentStartOrderBinding


class StartOrder : Fragment() {

 private var binding: FragmentStartOrderBinding?=null
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
       binding=FragmentStartOrderBinding.inflate(inflater, container, false)

        return binding?.root

    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding?.StartOrderButton?.setOnClickListener{orderDish()}
    }

    override fun onDestroyView() {
        super.onDestroyView()
        binding = null
    }
    fun orderDish(){
        findNavController().navigate(R.id.action_startOrder_to_chooseDish)
    }
}


chooseDish.kt :
package com.example.firstandroid

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import com.example.firstandroid.databinding.FragmentChooseDishBinding


class ChooseDish : Fragment() {
    private var binding: FragmentChooseDishBinding?=null
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding= FragmentChooseDishBinding.inflate(inflater, container, false)

        return binding?.root

    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding?.chooseDishNext?.setOnClickListener{orderSidedish()}
    }

    override fun onDestroyView() {
        super.onDestroyView()
        binding = null
    }

    fun orderSidedish(){
        findNavController().navigate(R.id.action_chooseDish_to_sidedish)
    }


}

CodePudding user response:

Try this Solution:

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.example.firstandroid.databinding.ActivityMainBinding
import androidx.navigation.fragment.NavHostFragment
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = ActivityMainBinding.inflate(layoutInflater)
    setContentView(binding.root)

val navHostFragment = supportFragmentManager
    .findFragmentById(R.id.nav_host_fragment) as NavHostFragment
val navController = navHostFragment.navController

}    
}
  • Related