Issue
My Problem:
I have a MainActivity along with its XML file activity_main.xml , which contains a NavigationDrawer view and that view calls a header.xml file to be displayed in my drawer.
The issue is that my header file contains a button so that when I click the header it opens a site. However since upgrading my Android studio build tools to 23.0.2 the onClick() for tht button inside my MainActivity crashes the app, giving me a NullPointerException.
I figure that its crashing because the header file is a totally different file from my activity_main.xml so my MainActivity would not have direct access to views inside of the header file.
How can I get access to the button that is inside the header.xml file with and also make it clickable?
Solution
Solved:
The NullPointerException was due to the fact that the layout(Header.xml) containing my Button was not readily available for the MainActivity's xml layout, therefore findViewById would always be null because to it, the button doesn't exist. This question was solved by including the header.xml with all its contents(specifically the button) into the NavigationView of the MainActivity.xml
Like so:
<android.support.v4.widget.DrawerLayout ....>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="@+id/toolbar"
layout="@layout/tool_bar" />
....
</RelativeLayout>
<android.support.design.widget.NavigationView
...
<include
layout="@layout/header" />
</android.support.v4.widget.DrawerLayout>
Answered By - Equivocal
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.