【问题标题】:getSupportActionBar().setDisplayHomeAsUpEnabled(true); throws NullPointerExceptiongetSupportActionBar().setDisplayHomeAsUpEnabled(true);抛出 NullPointerException
【发布时间】:2017-04-06 23:27:34
【问题描述】:

首先,我知道之前有人问过这个问题,我花了一个小时查看其他解决方案,主要来自 - getActionBar() returns null ,但没有一个有效,所以另一双眼睛可能会做点什么。

其他类继承自的NavigationDrawer类:

public class NavigationDrawer extends AppCompatActivity {

String[] mDrawerTitles;
DrawerLayout mDrawerLayout;
ListView mDrawerList;
ActionBarDrawerToggle mDrawerToggle;
String activityTitle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.navigation_drawer_layout);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    mDrawerTitles = getResources().getStringArray(R.array.drawer_array);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);
    activityTitle = getTitle().toString();

    getSupportActionBar().setDisplayHomeAsUpEnabled(true); //here is where the error is thrown
    getSupportActionBar().setHomeButtonEnabled(true);

    addDrawerItems();
    navDrawerSetup();

}

MainActiviy 扩展了上述内容:

public class MainActivity extends NavigationDrawer {

int[]images={R.drawable.user_manual,R.drawable.tips_tricks, R.drawable.troubleshooting};
Integer printer;
ListView myListView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View contentView = inflater.inflate(R.layout.activity_main, null, false);
    mDrawerLayout.addView(contentView, 0);
}

导航抽屉.xml

<?xml version="1.0" encoding="utf-8"?>
<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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<ListView
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#999"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp" />


</android.support.v4.widget.DrawerLayout>

styles.xml

<resources>

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

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.AppBarOverlay"  parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>

activity_main

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout     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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.shelleyd.myapplication.MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

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

content_main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.shelleyd.myapplication.MainActivity"
tools:showIn="@layout/activity_main">

<ListView
    android:id="@+id/myList"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_gravity="center_vertical"
    android:layout_weight="1" />

</LinearLayout>

【问题讨论】:

    标签: java android android-actionbar


    【解决方案1】:

    NavigationDrawer 活动中您正在执行setContentView(R.layout.navigation_drawer_layout),而在navigation_drawer_layout 中没有任何Toolbar,这会导致异常。

    【讨论】:

    • 是的,就是这样,谢谢。应用程序现在打开,但没有显示导航抽屉,知道为什么吗?
    • 您严重滥用继承。将这些活动分开。它会让你的代码更简洁,让你的生活更轻松。
    • 啊,好吧,我只是在关注这个 - stackoverflow.com/a/25254794 并认为我也这样做了。我想我现在会在每个活动中恢复到导航抽屉代码。
    • 我实际上设法弄清楚了这一点。我正在使用setContentView(); 以及LayoutInflater。删除前者解决了它。 @azizbekian
    【解决方案2】:

    在 NavigationDrawer.class 中执行以下操作

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); //This is null
     setSupportActionBar(toolbar);
    

    您将 Activity 工具栏设置为 null,因为您的 R.layout.activity_main 中没有具有 R.id.toolbar ID 的视图。因此,当您尝试在上面的代码之后设置工具栏时,您是在尝试在空对象中设置一个属性,这会产生空指针异常。

    在您的活动中删除上述两行或在您的 R.layout.activity_main 布局文件中声明一个带有 R.id.toolbar ID 的工具栏视图。这样做你就会解决问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-07
      • 2019-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多