-1

I'm getting a runtime Error:

FATAL EXCEPTION: main
Process: com.example.home.stobun, PID: 18938
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.home.stobun/com.example.home.stobun.drawer}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)

when I'm trying to implement the following piece of code:

Here's my java class:


package com.example.home.stobun;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.Toast;


public class drawer extends AppCompatActivity implements 
NavigationView.OnNavigationItemSelectedListener{
private DrawerLayout mdrawerlayout;
private ActionBarDrawerToggle mdrawertoggle;

protected void onCreate (Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.drawer);
    mdrawerlayout=(DrawerLayout)findViewById(R.id.drawer);
    mdrawertoggle=new ActionBarDrawerToggle(this,mdrawerlayout,R.string.open,R.string.close);
    mdrawerlayout.addDrawerListener(mdrawertoggle);
    mdrawertoggle.syncState();
    getActionBar().setDisplayHomeAsUpEnabled(true); ***//geting an error cuz of this***
    NavigationView navigationView=(NavigationView)findViewById(R.id.navigation_view);
    navigationView.setNavigationItemSelectedListener(this);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if(mdrawertoggle.onOptionsItemSelected(item))
    {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    int id=item.getItemId();

    if(id==R.id.home)
    {
        Toast.makeText(this,"This is home",Toast.LENGTH_SHORT);
    }
    if(id==R.id.notices)
    {
        Toast.makeText(this,"This is Notices",Toast.LENGTH_SHORT);
    }
    if(id==R.id.assignments)
    {
        Toast.makeText(this,"This is assignments",Toast.LENGTH_SHORT);
    }

    return false;
}

}


Here's the corresponding XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@style/Widget.Design.AppBarLayout"
android:id="@+id/drawer"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</LinearLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:menu="@menu/nav_menu"
    android:layout_gravity="start">
     </android.support.design.widget.NavigationView>

</android.support.v4.widget.DrawerLayout>

I have followed the steps given on this YouTube video to make an App Navigation Drawer, but the navigation drawer toggle button ( the three lines ) button is not visible because of the error, which is because the call to the actionbar is returning a nullpointer exception.

https://www.youtube.com/watch?v=NC3dM8qcpEM

The app crashes while running.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38

2 Answers2

1

You are calling getActionBar() call getSupportActionBar()

Sony
  • 7,136
  • 5
  • 45
  • 68
1

You need to use getSupportActionBar() in place of getActionBar().

like this -

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Sachin Rajput
  • 4,326
  • 2
  • 18
  • 29