I am trying to make an app where you can toggle the drawer using by clicking onto an imageview, however it seems I get a nasty error everytime I set an onClickListener in the OnCreate of the main activity
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.myapplication, PID: 7921 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.activities.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3356) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3500) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2049) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:223) at android.app.ActivityThread.main(ActivityThread.java:7523) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:941) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at com.example.myapplication.activities.MainActivity.onCreate(MainActivity.kt:43) at android.app.Activity.performCreate(Activity.java:7984) at android.app.Activity.performCreate(Activity.java:7973) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3329) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3500) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2049) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:223) at android.app.ActivityThread.main(ActivityThread.java:7523) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:941)
Been digging around for half an hour with no results, any tips on how to do this?
Everything else works except this. Just in case your wondering, here are the important bits of code:
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) setUpActionBar() showProgressDialog(resources.getString(R.string.please_wait)) FirestoreClass().loadUserData(this) nav_view_main.setNavigationItemSelectedListener(this) iv_cancel_icon.setOnClickListener { toggleDrawer() } } private fun toggleDrawer(){ if(drawer_layout_main.isDrawerOpen(GravityCompat.START)){ drawer_layout_main.closeDrawer(GravityCompat.START) } else { drawer_layout_main.openDrawer(GravityCompat.START) } } private fun setUpActionBar(){ setSupportActionBar(toolbar_main_activity) toolbar_main_activity.setNavigationIcon(R.drawable.ic_action_navigation_menu) toolbar_main_activity.setNavigationOnClickListener { toggleDrawer() } getSupportActionBar()!!.setDisplayShowTitleEnabled(false) }
Code in Kotlin from the shared link above :
//Getting navigation view child and modifying its data val navigationView = findViewById(your_navigation_view_id_in_drawer_layout) val headerview = navigationView.getHeaderView(0) val profilename = headerview.findViewById(your_view_id) profilename.text = "text" //Setting on click listener on header child val child = headView.findViewById(child_id) child.setOnClickListener{ view -> //Code to execute on click }
If this still doesn’t resolve your issue, then kindly share the XML code in the comments and then will give you a solution based on that.