1

I am new in android development and I am trying to make navigation drawer. As soon as I put these lines in my java file, my application unfortunately stops. Can somebody tell me what am I doing wrong with the code? It does not say anything while building the project, no syntax errors nothing but when I hover over one of these two lines it says this:

"Method invocation in `getSupportActionBar.setHomeButtonEnabled may produce java.lang.NullPointerException"

getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

This is my MainActivity.java file:

import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.DrawableRes;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.widget.DrawerLayout;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import java.util.ArrayList;



public class MainActivity extends AppCompatActivity  implements AdapterView.OnItemClickListener {

    private ActionBarDrawerToggle actionBarDrawerToggle;
    private DrawerLayout drawerLayout;
    private ListView navList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
        actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,
                R.string.close_drawer,R.string.open_drawer);
        drawerLayout.setDrawerListener(actionBarDrawerToggle);


        //NullPointException in these two lines
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);


        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        navList = (ListView)findViewById(R.id.list_items);
        ArrayList<String> navArray = new ArrayList<String>();
        navArray.add("Home");
        navArray.add("Call Logs");
        navArray.add("Message Logs");
        navArray.add("Location");
        navArray.add("Browsing History");
        navArray.add("Dictionary");
        navArray.add("About Us");
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,navArray);
        navList.setAdapter(adapter);
        navList.setOnItemClickListener(this);

        loadSelection(0);


    }
    private void loadSelection(int i){
        navList.setItemChecked(i, true);
    }

    @Override
    public void onPostCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
        super.onPostCreate(savedInstanceState, persistentState);
        actionBarDrawerToggle.syncState();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        else if(id == android.R.id.home)
        {
            if(drawerLayout.isDrawerOpen(navList))
            {
                drawerLayout.closeDrawer(navList);
            }
            else
            {
                drawerLayout.openDrawer(navList);
            }
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch(position){
            case 0:

                break;
            case 1:

                break;
            case 2:

                break;
            case 3:

                break;
            case 4:

                break;
            case 5:

                break;
            case 6:

                break;
            case 7:

                break;
        }
        drawerLayout.closeDrawer(navList);

    }


    }
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user3149023
  • 49
  • 1
  • 6

1 Answers1

0

This is because you don't know if "getSupportActionBar" will return an "ActionBar" or "null".

You have two options available:

  • You are certain that you will always have a ActionBar, and so you can ignore this warning.
  • You are not sure or you like to have an aesthetic code without warnings, and so you can do this :

    ActionBar actionBar = this.getSupportActionBar();
    if (actionBar != null) actionBar.setDisplayHomeAsUpEnabled(true);
    

By the way, like you can see here, you don't need "setHomeButtonEnabled".

Greelings
  • 4,964
  • 7
  • 34
  • 70
  • hey Denis! now my app does not stop but i can't see hamburger icon on the action bar, can you tell me what's wrong? – user3149023 Dec 05 '15 at 13:07
  • actually when i add the third parameter (image) in the below line it gives me error. @Denis actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout, R.string.close_drawer,R.string.open_drawer); – user3149023 Dec 05 '15 at 15:06