getSupportActionBar()对空对象引用?

时间:2017-12-04 19:45:02

标签: android android-actionbar navigation-drawer

以下是我项目的一部分。错误在getSupportActionBar().setDisplayHomeAsUpEnabled(true);行。我检查了任何主题不匹配但我的知识没有主题不匹配,活动扩展了&appcompatActivity'我使用了' getSupportActionBar()'但仍然得到nullpointer异常(下面发布错误)..

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 {
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>

错误

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)

希望我足够清楚!

修改

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>

编辑2

我甚至删除了提供给include标记的id并再次尝试但仍无效。

1 个答案:

答案 0 :(得分:0)

那是因为您的布局不包含id =“toolbar_cust”的视图。

因此findViewById(R.id.toolbar_cust)会返回null,因此您setSupportActionBar(null)getSupportActionBar()也会返回null