Home > Software design >  App crashing using onclicklistener , where as onclick is working || Navigation Drawer Activity || Ko
App crashing using onclicklistener , where as onclick is working || Navigation Drawer Activity || Ko

Time:10-09

This is my mainActivity .kt file in Navigation Drawer Activity. when I use onclicklistener it crashes my application , but runs properly when I use onClick outside the oncreate function . Using Onclicklistener my build is not giving any error , but there are some errors in logcat .

package com.example.android.chatapplication

import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.View
import android.widget.Button
import android.widget.ImageButton
import com.google.android.material.snackbar.Snackbar
import com.google.android.material.navigation.NavigationView
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 androidx.appcompat.app.AppCompatActivity
import com.example.android.chatapplication.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

    private lateinit var appBarConfiguration: AppBarConfiguration
    private lateinit var binding: ActivityMainBinding

    val loginbtn : Button = findViewById(R.id.loginButtonNavHeader)
    val addchannelbtn : ImageButton = findViewById(R.id.addChannelButton)
    val sendmessagebtn : ImageButton = findViewById(R.id.sendMessageBtn)

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

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        setSupportActionBar(binding.appBarMain.toolbar)

// when I write the below onclicklisener my app crashes . but simply works properly when a use onClick .

        loginbtn.setOnClickListener {
            val loginIntent = Intent(this, LoginActivity::class.java)
            startActivity(loginIntent)
        }
        val drawerLayout: DrawerLayout = binding.drawerLayout
        val navView: NavigationView = binding.navView
        val navController = findNavController(R.id.nav_host_fragment_content_main)
        appBarConfiguration = AppBarConfiguration(
            setOf(
                R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow
            ), drawerLayout
        )
        setupActionBarWithNavController(navController, appBarConfiguration)}

Logcat Error

Process: com.example.android.chatapplication, PID: 6449
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.android.chatapplication/com.example.android.chatapplication.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3637)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3896)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:140)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:100)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2326)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:263)
        at android.app.ActivityThread.main(ActivityThread.java:8296)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:612)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1006)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
        at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:183)
        at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:174)
        at android.content.Context.obtainStyledAttributes(Context.java:744)
        at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:852)
        at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:819)
        at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:640)
        at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:261)
        at com.example.android.chatapplication.MainActivity.<init>(MainActivity.kt:25)
        at java.lang.Class.newInstance(Native Method)
        at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
        at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1254)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3625)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3896) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:140) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:100) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2326) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:263) 
        at android.app.ActivityThread.main(ActivityThread.java:8296) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:612) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1006) 

When a use the below code , my app runs completely fine .

fun loginbtnclicked(view : View ){
        val loginIntent = Intent(this, LoginActivity::class.java)
        startActivity(loginIntent)
    }

CodePudding user response:

You are calling findViewById() too early in the activity lifecycle. Move these:

val loginbtn : Button = findViewById(R.id.loginButtonNavHeader)
val addchannelbtn : ImageButton = findViewById(R.id.addChannelButton)
val sendmessagebtn : ImageButton = findViewById(R.id.sendMessageBtn)

to onCreate() after the setContentView() call.

  • Related