i want to add a custom ActionBar,. i tried to implement an existing code in my app, but getSupportActionBar() returning null

getsupportactionbar returns null appcompatactivity
getactionbar returns null in appcompatactivity
custom action bar android
android action bar example source code
android add button to action bar
custom toolbar'' android
setsupportactionbar
setactionbar

//activity_main_pager.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawerid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainPagerActivity">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include
        android:id="@+id/appbar"
        layout="@layout/navigation_actionbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <android.support.v4.view.ViewPager
        android:id="@+id/main_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/maintab"
        android:layout_below="@id/appbar"

        >

    </android.support.v4.view.ViewPager>

    <android.support.design.widget.TabLayout
        android:id="@+id/maintab"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/colorPrimaryDark"
        app:tabSelectedTextColor="@color/colorAccent"
        app:tabTextColor="#ffffff">

    </android.support.design.widget.TabLayout>
</RelativeLayout>

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


    app:menu="@menu/main_activity_actions">

</android.support.design.widget.NavigationView>

//MainPagerActivity.java

public class MainPagerActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private TabLayout mTabLayout;
private DrawerLayout mdrawer;
private ActionBarDrawerToggle toggle;
private Toolbar mtoolbar;
CircleImageView imageView;
TextView textView, emailview;

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


    mTabLayout = (TabLayout) findViewById(R.id.maintab);
    ViewPager viewPager = (ViewPager) findViewById(R.id.main_pager);
    viewPager.setAdapter(new MainAdapter(this));
    mTabLayout.setupWithViewPager(viewPager);


    mtoolbar = (Toolbar) findViewById(R.id.n_action_toolbar);
    setSupportActionBar(mtoolbar);

    mdrawer = (DrawerLayout) findViewById(R.id.drawerid);
    toggle = new ActionBarDrawerToggle(this, mdrawer, R.string.open, R.string.close);
    mdrawer.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
    navigationView.setNavigationItemSelectedListener(this);


    View headerView = navigationView.inflateHeaderView(R.layout.navigation_header);
    textView = (TextView) headerView.findViewById(R.id.nameView);
    emailview = (TextView) headerView.findViewById(R.id.emailView);
    imageView = (CircleImageView) headerView.findViewById(R.id.imageView);


    try {
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    } catch (NullPointerException e) {
        e.printStackTrace();
    }


}


@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {

    switch (item.getItemId()) {


        case R.id.logoutapp:

            break;
        case R.id.logout:

            break;

    }


    return false;
}


public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items


    if (toggle.onOptionsItemSelected(item)) {

        return true;
    }
    return super.onOptionsItemSelected(item);

}

}

//navigation_actionbar.xml

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/n_action_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:orientation="vertical"
android:theme="@style/ThemeOverlay.AppCompat.Dark">

//AndroidManifest.xml

 <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity android:name=".LogInActivity"></activity>
    <activity android:name=".MainPagerActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

//styles.xml

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

//Errors

    W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
W/ActionBarDrawerToggle: DrawerToggle may not show up because NavigationIcon is not visible. You may need to call actionbar.setDisplayHomeAsUpEnabled(true);
W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
                  at ava.earn_real_money.groot.MainPagerActivity.onCreate(MainPagerActivity.java:60)
                  at android.app.Activity.performCreate(Activity.java:6664)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                  at android.app.ActivityThread.-wrap12(ActivityThread.java)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6077)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

OnCreate() method add this

 mtoolbar = (Toolbar) findViewById(R.id.n_action_toolbar);
 setUpToolbar();

setUpToolbar() method check if action bar null or not

private void setUPToolbar () {
        setSupportActionBar (mtoolbar);
        ActionBar actionBar=getSupportActionBar ();
        if (actionBar!=null){
            actionBar.setTitle (R.string.lead_title);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(true);
        }
}

getSupportActionBar() returns null with themes that contain , Workaround for that is to set manually windowNoTitle to true. in ActionBar not there, and getSupportActionBar() returning null , that's Let my try to explain once more Now when Activity is launched, in onCreate() I have following code If your application does not use action bar then no need use that  In this tutorial we will create an app that consists of Android Custom Action Bar with a custom layout. We assume that you have a basic understanding of the ActionBar component discussed in this tutorial. To customise an ActionBar first we need to configure the Theme in the res/values/styles.xml and set the theme for the respective activity

you should check second line of error: You may need to call actionbar.setDisplayHomeAsUpEnabled(true);

Extended ActionBar Guide, This is typically used for displaying the title of the application and providing a primary Using the split action bar to have a top and bottom menu; Adding ActionView ActionBar; // in Activity#onCreate getSupportActionBar(). The above code results in: You can also use an Action View to create a custom layout too. In this post, I will teach you how to add Action Bar in Android Applications. I will also discuss how to make the Action Bar work in lower SDK Android version (Backward compatibility). Prerequisite: You must know about Action Bar before you start creating it.

i've solved this problem by replacing

//this code is given in activity_main_pager.xml in above question

<include android:id="@+id/appbar"  
layout="@layout/navigation_actionbar"  
android:layout_width="match_parent"  
android:layout_height="wrap_content" />  

directly with

//this code is given in navigation_actionbar.xml in above question

<android.support.v7.widget.Toolbar  
xmlns:android="schemas.android.com/apk/res/android";  
android:id="@+id/n_action_toolbar"  
android:layout_width="match_parent"  
android:layout_height="wrap_content"  
android:background="@color/colorPrimary"  
android:orientation="vertical"  
android:theme="@style/ThemeOverlay.AppCompat.Dark">  

in activity_main_pager.xml file.

Tips and Tricks for Android Material Support Library, Gone are the days of Gingerbread design, and if your app is Holo, you're When you call getActionBar() on an ActionBarActivity, you will receive a null object. in existing code, so you'll need to use getSupportActionBar() instead. type of activity and use the generic AppCompat theme we set up earlier. On Android 2.3.4 (real device and emulator) this doesn't work, I receive NullPointerException when I want to access ActionBar. Reason for that is, original theme that came from manifest contains style windowNoTitle and ActionBarSherlock (or Android 2.x, for that matter) when applying new theme still thinks that windowNoTitle is set, while it

How to create ActionBar/Toolbar and Menu in Android, Use a Toolbar as an ActionBar when you want to use the existing your App and the App bar while with the colorPrimaryDark you can set the color in the res/​menu directory though they can also be created in code. setSupportActionBar(​toolbar); if (getSupportActionBar() != null) { getSupportActionBar(). Adding a Toolbar and Navigation Drawer Across all Activities of an Android App 21 Jun 2015. Gone are the days of using the sucky actionbar. With the introduction of Android 5.0, the actionbar has been replaced by the toolbar. The Toolbar allows more customization and can be placed anywhere in the UI since it’s just a viewgroup.

Android ActionBar Example Tutorial, Action Bar is one of the important part of any application, whether it's a web 1.6 Android ActionBar Example Code; 1.7 Android Action Bar Backporting toolbar icons and action overflow items into the action bar is by creating menu the onCreateOptionsMenu() method of the activity where we wish to add the ActionBar. This class describes how to use the v7 appcompat support library's Toolbar widget as an app bar. There are other ways to implement an app bar—for example, some themes set up an ActionBar as an app bar by default—but using the appcompat Toolbar makes it easy to set up an app bar that works on the widest range of devices, and also gives

How to Add a Navigation Drawer in Android, I will assume you are here because you think it is good for your app, though! private void addDrawerItems() { String[] osArray = { "Android", "iOS", This is the minimum basic code we need for a navigation drawer, but When the drawer is opened, we can set a new title for the Action Bar (if we want). In this article we will create an application which will add action items and overflow menu items to action bar. This application is developed in Eclipse 3.7.2 and tested in Android api level 14. If you want to add actions items, overflow menu items and action bar in pre Honeycomb (API level < 11 ) versions also, see the article “ Adding

Comments
  • main problem is in this line : findViewById(R.id.n_action_toolbar); it is is returning null
  • i've solved this problem by replacing <include android:id="@+id/appbar" layout="@layout/navigation_actionbar" android:layout_width="match_parent" android:layout_height="wrap_content" /> directly with <android.support.v7.widget.Toolbar xmlns:android="schemas.android.com/apk/res/android" android:id="@+id/n_action_toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorPrimary" android:orientation="vertical" android:theme="@style/ThemeOverlay.AppCompat.Dark">
  • This trick will only prevent app crash if getSupportActionBar() returns null But result remains the same.
  • I've added it already in try-catch function, it is available in given code.