I'm trying to do this tutorial : http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/

It is to make a sliding menu inside an app.

Infortunately, the

getActionBar().setDisplayHomeAsUpEnabled(true);

is throwing a null pointer exception.

I'm a beginner in android and I've read a couple of solutions but no one seems to work correctly for me and the application still crashes when it gets to this part of the code.

There are the questions I read : getActionBar().setDisplayHomeAsUpEnabled(true) throws NullPointerException, getActionBar().setDisplayHomeAsUpEnabled(true); throws NullPointerException on new activity creation (Google - Basic Tutorial), getActionBar().setDisplayHomeAsUpEnabled(true); nullpointer in preferenceActivity

Can someone help me with this ? Apparently it deals with the Action Bar but I don't know what exactly ...

It will be really helpful, thanks!

Here is the code of the MainActivity that diplays the menu, and below it I provide you the error log :

package com.example.invite.myapplication.activities;

import com.example.invite.myapplication.adapter.NavDrawerListAdapter;
import com.example.invite.myapplication.model.NavDrawerItem;
import com.example.invite.myapplication.R;

import com.example.invite.myapplication.frags.*;


import java.util.ArrayList;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

public class MainActivity extends Activity {
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;

    // nav drawer title
    private CharSequence mDrawerTitle;×

    // used to store app title
    private CharSequence mTitle;

    // slide menu items
    private String[] navMenuTitles;
    private TypedArray navMenuIcons;

    private ArrayList<NavDrawerItem> navDrawerItems;
    private NavDrawerListAdapter adapter;

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

        mTitle = mDrawerTitle = getTitle();

        // load slide menu items
        navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);

        // nav drawer icons from resources
        navMenuIcons = getResources()
                .obtainTypedArray(R.array.nav_drawer_icons);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.list_slidermenu);

        navDrawerItems = new ArrayList<NavDrawerItem>();

        // adding nav drawer items to array
        // Home
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
        // Find People
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
        // Photos
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
        // Communities, Will add a counter here
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1), true, "22"));
        // Pages
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));
        // What's hot, We  will add a counter here
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1), true, "50+"));


        // Recycle the typed array
        navMenuIcons.recycle();

        mDrawerList.setOnItemClickListener(new SlideMenuClickListener());

        // setting the nav drawer list adapter
        adapter = new NavDrawerListAdapter(getApplicationContext(),
                navDrawerItems);
        mDrawerList.setAdapter(adapter);

        // enabling action bar app icon and behaving it as toggle button
        **getActionBar().setDisplayHomeAsUpEnabled(true);**
        getActionBar().setHomeButtonEnabled(true);

        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.drawable.ic_drawer, //nav menu toggle icon
                R.string.app_name, // nav drawer open - description for accessibility
                R.string.app_name // nav drawer close - description for accessibility
        ) {
            public void onDrawerClosed(View view) {
                getActionBar().setTitle(mTitle);
                // calling onPrepareOptionsMenu() to show action bar icons
                invalidateOptionsMenu();
            }

            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle(mDrawerTitle);
                // calling onPrepareOptionsMenu() to hide action bar icons
                invalidateOptionsMenu();
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        if (savedInstanceState == null) {
            // on first time display view for first nav item
            displayView(0);
        }
    }

Here the error log :

06-11 02:12:24.364 25134-25134/com.example.invite.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.invite.myapplication/com.example.invite.myapplication.activities.MainActivity}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349) at android.app.ActivityThread.access$700(ActivityThread.java:159) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5419) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:525) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at com.example.invite.myapplication.activities.MainActivity.onCreate(MainActivity.java:91) at android.app.Activity.performCreate(Activity.java:5372) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)             at android.app.ActivityThread.access$700(ActivityThread.java:159)             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)             at android.os.Handler.dispatchMessage(Handler.java:99)             at android.os.Looper.loop(Looper.java:137)             at android.app.ActivityThread.main(ActivityThread.java:5419)             at java.lang.reflect.Method.invokeNative(Native Method)             at java.lang.reflect.Method.invoke(Method.java:525)             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)             at dalvik.system.NativeStart.main(Native Method)

And here is the "styles.xml"

<!--
    Base application theme, dependent on API level. This theme is replaced
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
    <!--
        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.
    -->
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>

<style name = "NoActionBar" parent = "@android:style/Theme.Holo.Light">
    <!-- <item name = "android:windowActionBar">false</item> -->
    <!-- <item name = "android:windowNoTitle">true</item> -->
</style>

SOLUTION : # (hope it works for you too).

It was simple finally.

Just add :

requestWindowFeature(Window.FEATURE_ACTION_BAR);

before SetContentView(View ..).

As far as I understood, this kind of forces the Activity to request for an Action Bar "Action Bar please ! (?)".

And set the min sdk 11 and target 17 in your manifest

Here the answer that helped me finally : https://stackoverflow.com/a/10031400

3

There are 3 answers

0
Brexx Galangue On

After you've declared this code getActionBar().setDisplayHomeAsUpEnabled(true); inside your OnCreate(), you must declare some codes too in your OnOptionItemSelected() like this:

        public boolean onOptionsItemSelected(MenuItem item) {
            int id = item.getItemId();

            if (id == android.R.id.home) {

                Intent i = new Intent(MainListActivity.this, MainActivity.class);
                startActivity(i);
                finish();
            }
            return super.onOptionsItemSelected(item);
        }

Hope it helps :D

0
Ankit Khare On

Try to run like this

private ActionBar actionBar;

 @Override
    protected void onCreate(Bundle savedInstanceState) {

  actionBar = getActionBar();
        if(actionBar!=null) {
           actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeButtonEnabled(true);
        }

}
0
rBenks93 On

I just found out the solution.

It was simple finally.

Just add :

requestWindowFeature(Window.FEATURE_ACTION_BAR);

before SetContentView(View ..).

And set the min sdk 11 and target 17 in your manifest

Here the answer that helped me finally : https://stackoverflow.com/a/10031400