Reputation: 53
I am having troubles with configuring my application so that it does not crash on start-up. I created a few activities and made a navigation drawer fragment whose layout I stored in a separate .xml file and included it in the layouts for each activity.
This was working, until I played around with a different .xml file for one the activities which is not the launcher activities. There was a bug in the gradle build such that the app which was loaded onto the phone was a previous version (the changes I made weren't included in the build for some reason). This happens often for me (in Android Studio) and my work around is to uninstall the app from my phone and reinstall it by running it in Android Studio. However after doing this, I had some weird results. Now I can't even start my android app.
I'm getting a null pointer error, when the code attempts to
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams android.view.View.getLayoutParams()' on a null object reference
The line in which it crashes on is the one where it attempts to open the drawer if the user is opening the app for the first time, it seems to be the containerView which is becoming null:
public void setUp(int fragmentID, final DrawerLayout drawerLayout, Toolbar toolbar){
mDrawerLayout = drawerLayout;
containerView = getActivity().findViewById(fragmentID);
if(!mUserLearnedDrawer && !mFromSavedInstanceState){
mDrawerLayout.openDrawer(containerView);
}
}
This setup function is called in my MainActivity (in the OnCreate method):
NavigationDrawerFragment drawerFragment = (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.drawer_layout_declared);
drawerFragment.setUp(R.id.drawer_layout_declared, (DrawerLayout) findViewById(R.id.drawer_layout_home), toolbar);
Now I included the navigation drawer in my layout file like so.
<include
android:id="@+id/nav_drawer_frag"
layout="@layout/navigation_drawer_declaration"/>
Where it is the 2nd child of a android.support.v4.widget.DrawerLayout element This is the contents of the navigation_drawer_declaration.xml file.
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout_declared"
android:name="helloworld.vault101payroll.NavigationDrawerFragment"
android:layout_width="@dimen/nav_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="@layout/fragment_navigation_drawer"
tools:layout="@layout/fragment_navigation_drawer"/>
I believe this is the the thing that should be referenced when adding the navigation drawer for the fragmentID.
I've logged the value of the resource file which came back as 2131492942, is this a value integer for a non-null layout?
Anyway, I thought it was strange how this error came about, since I was working on a completely unrelated piece of code to where the error was occurring. I removed the files that I was working on since the last time the app ran and it made no difference.
Any help is greatly appreciated.
EDIT:
Please note, I have added the ActionBarDrawerToggle object to my drawerToggle: This is what is below my
containerView = getActivity().findViewById(fragmentID)
Line
mDrawerToggle = new ActionBarDrawerToggle(getActivity(), drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_closed){
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
if(!mUserLearnedDrawer){
mUserLearnedDrawer = true;
saveToPreferences(getActivity(), KEY_USER_LEARNED_DRAWER, mUserLearnedDrawer + "");
}
getActivity().invalidateOptionsMenu();
}
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
getActivity().invalidateOptionsMenu();
}
};
if(!mUserLearnedDrawer && !mFromSavedInstanceState){
mDrawerLayout.openDrawer(containerView);
}
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerLayout.post(new Runnable() {
@Override
public void run() {
mDrawerToggle.syncState();
}
});
Upvotes: 1
Views: 869
Reputation: 421
add thid code below "containerView = getActivity().findViewById(fragmentID);" that will define the behavior of drawerLayout.
mDrawerToggle = new ActionBarDrawerToggle(getActivity(), drawerLayout, toolbar,
R.string.drawer_open, R.string.drawer_close) {
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
if (!mUserLearnedDrawer) {
mUserLearnedDrawer = true;
saveToPreferences(getActivity(), KEY_USER_LEARNED_DRAWER, mUserLearnedDrawer + "");
}
getActivity().invalidateOptionsMenu();
}
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
getActivity().invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerLayout.post(new Runnable() {
@Override
public void run() {
mDrawerToggle.syncState();
}
});
Upvotes: 1