Home > OS >  Using buttons in Bottom navigation crashes app
Using buttons in Bottom navigation crashes app

Time:12-28

App Crashes when using button, link and .isGone function. It was working before but error occurred when switched to bottom navigation and fragments.

Could anyone explain what is happening.

Error:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.dmat.zerowaste, PID: 14673
    java.lang.NullPointerException
        at com.dmat.zerowaste.OrganicWasteFragment.onCreate(OrganicWasteFragment.kt:41)
        at androidx.fragment.app.Fragment.performCreate(Fragment.java:2949)
        at androidx.fragment.app.FragmentStateManager.create(FragmentStateManager.java:475)
        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:278)
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2100)
        at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
        at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:524)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:245)
        at android.app.ActivityThread.main(ActivityThread.java:8004)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:631)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:978)

File:

package com.dmat.zerowaste

import android.content.Intent
import android.os.Bundle
import android.text.method.LinkMovementMethod
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView

import androidx.core.view.isGone
import androidx.fragment.app.Fragment
import kotlinx.android.synthetic.main.organic_waste_fragment_page.*

class OrganicWasteFragment : Fragment(R.layout.activity_recycle_page) {

    /*override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.organic_waste_fragment_page, container, false)
    }*/

    /*override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?): View? {
        val rootView = inflater.inflate(R.layout.organic_waste_fragment_page, container, false)
        return rootView
    }*/


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)


        val compost_butt: Button = view?.findViewById(R.id.compost_butt)!!
        val animal_feed_butt: Button = view?.findViewById(R.id.animal_feed_butt)!!
        val animal_feed_content: View? = view?.findViewById(R.id.animal_feed_content)
        val rendering_butt: Button = view?.findViewById(R.id.rendering_butt)!!
        val rendering_content: View? = view?.findViewById(R.id.rendering_content)!!
        //val rapid_fermentation_butt: Button = view?.findViewById(R.id.fermentation_butt)!!
        val fermentation_link: TextView = view?.findViewById(R.id.fermentation_link)!!

        fermentation_link.setMovementMethod(LinkMovementMethod.getInstance())

        /*animal_feed_content.isGone = true
        rendering_content!!.isGone = true*/

        /*compost_butt.setOnClickListener {
            val compostIntent = Intent(context, composting_page::class.java)
            startActivity(compostIntent)

            animal_feed_butt.setCompoundDrawablesRelativeWithIntrinsicBounds(
                R.drawable.animal_feed_icon,
                0,
                R.drawable.plus,
                0
            )

            rendering_butt.setCompoundDrawablesRelativeWithIntrinsicBounds(
                R.drawable.rendering_icon,
                0,
                R.drawable.plus,
                0
            )

            animal_feed_content.isGone = true
            rendering_content.isGone = true

        }*/

        /*animal_feed_butt.setOnClickListener {
            animal_feed_butt.setCompoundDrawablesRelativeWithIntrinsicBounds(
                R.drawable.animal_feed_icon,
                0,
                R.drawable.minus_icon,
                0
            )

            animal_feed_content.isGone = false
            rendering_content.isGone = true

            rendering_butt.setCompoundDrawablesRelativeWithIntrinsicBounds(
                R.drawable.rendering_icon,
                0,
                R.drawable.plus,
                0
            )
        }

        rendering_butt.setOnClickListener {
            rendering_butt.setCompoundDrawablesRelativeWithIntrinsicBounds(
                R.drawable.rendering_icon,
                0,
                R.drawable.minus_icon,
                0
            )

            rendering_content.isGone = false
            animal_feed_content.isGone = true

            animal_feed_butt.setCompoundDrawablesRelativeWithIntrinsicBounds(
                R.drawable.animal_feed_icon,
                0,
                R.drawable.plus,
                0
            )
        }*/

    }
}

CodePudding user response:

I solved it.

I literally just copy pasted everything from onCreate() to onCreateView(), then removed onCreate().

Don't know why but in onCreateView() it was able to reference layout file but not in onCreate().

  • Related