将 OnClickListener 添加到 headerLayout 中的元素
Add OnClickListener to elements inside headerLayout
我正在尝试制作一个应用程序,您可以在其中通过单击图像视图来切换抽屉,但是每次我在 main activity[的 OnCreate 中设置 onClickListener 时似乎都会遇到严重错误=12=]
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)
已经挖了半个小时没有结果,关于如何做到这一点有什么提示吗?
除此之外其他一切正常。为了以防万一你想知道,这里有一些重要的代码:
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)
}
上面共享的 link 中的 Kotlin 代码:
//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
}
如果这仍然不能解决您的问题,请在评论中分享 XML 代码,然后将在此基础上为您提供解决方案。
我正在尝试制作一个应用程序,您可以在其中通过单击图像视图来切换抽屉,但是每次我在 main activity[的 OnCreate 中设置 onClickListener 时似乎都会遇到严重错误=12=]
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)
已经挖了半个小时没有结果,关于如何做到这一点有什么提示吗?
除此之外其他一切正常。为了以防万一你想知道,这里有一些重要的代码:
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)
}
上面共享的 link 中的 Kotlin 代码:
//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
}
如果这仍然不能解决您的问题,请在评论中分享 XML 代码,然后将在此基础上为您提供解决方案。