-1

The following are a part of my project. The error is on the line getSupportActionBar().setDisplayHomeAsUpEnabled(true);. I checked for any theme mismatch but there were no theme mismatch to my knowledge, the activity extends 'appcompatActivity' and i used the 'getSupportActionBar()' but still getting the nullpointer exception (Error posted Below)..

Home_Activity.java

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
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.support.v7.widget.Toolbar;
import android.view.MenuItem;


public class Home_Activity extends AppCompatActivity {

DrawerLayout drawerLayout;
ActionBarDrawerToggle mToggle;
android.support.v4.app.FragmentTransaction fragmentTransaction;
Toolbar mToolbar;
NavigationView navigationView;

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

    mToolbar = findViewById(R.id.toolbar_cust);
    setSupportActionBar(mToolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); //ERROR
}

nav_page.xml

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<RelativeLayout
    android:id="@+id/relative_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <include
        android:id="@+id/inc"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        layout="@layout/toolbar_layout"
        />
    <FrameLayout
        android:id="@+id/maincontainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/inc">
    </FrameLayout>

</RelativeLayout>
<!-- The navigation drawer -->
<android.support.design.widget.NavigationView
    android:id="@+id/navigation"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:menu="@menu/menu_list_items"
    app:headerLayout="@layout/navigation_header"
    android:background="#ffffff">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>

Gradle dependencies

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
compile 'com.android.support:design:26.1.0'
}

styles.xml

<resources>

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

</resources>

ERROR

FATAL EXCEPTION: main
                                                                 Process: com.app.ce.cybereye, PID: 30688
                                                                 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.ce.cybereye/com.app.ce.cybereye.Home_Activity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2696)
                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2757)
                                                                     at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1504)
                                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                     at android.os.Looper.loop(Looper.java:163)
                                                                     at android.app.ActivityThread.main(ActivityThread.java:6237)
                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:877)
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
                                                                  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
                                                                     at com.app.ce.cybereye.Home_Activity.onCreate(Home_Activity.java:30)
                                                                     at android.app.Activity.performCreate(Activity.java:6847)
                                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2649)

Hope i am clear enough!

EDIT

toolbar_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/toolbar_cust"
android:background="?attr/colorPrimaryDark"
android:minHeight="?attr/actionBarSize"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
>

</android.support.v7.widget.Toolbar>

EDIT 2

I even removed the id provided to the include tag and tried again but still not working.

7
  • Are you using android:windowNoTitle with true? Commented Dec 4, 2017 at 19:50
  • Please post the toolbar_layout as well.
    – Ridcully
    Commented Dec 4, 2017 at 19:52
  • The id on the <include> tag is overriding the id on the Toolbar. Either remove it from the <include>, or change the R.id in the findViewById() call to R.id.inc.
    – Mike M.
    Commented Dec 4, 2017 at 19:58
  • Clean/rebuild your project.
    – Mike M.
    Commented Dec 4, 2017 at 20:10
  • 1
    I am sorry. it was the id that was messing the whole thing up but i did not notice that because there was another error very similar. I was a bit too careless. Commented Dec 4, 2017 at 20:27

1 Answer 1

0

That's because your layout does not contain a View with id = "toolbar_cust".

So the findViewById(R.id.toolbar_cust) returns null, so you're setSupportActionBar(null) and getSupportActionBar() will then also return null.

3
  • He includes a toolbar, maybe the id is in there. Commented Dec 4, 2017 at 19:51
  • Maybe it is, maybe not.
    – Ridcully
    Commented Dec 4, 2017 at 19:51
  • The toolbar is in another layout and I have assigned the id = toolbar_cust. Commented Dec 4, 2017 at 19:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.