【问题标题】:Trying to setDisplayHomeAsUpEnabled(true) in my Android app and it's giving me a NullPointerException试图在我的 Android 应用程序中 setDisplayHomeAsUpEnabled(true) 并且它给了我一个 NullPointerException
【发布时间】:2018-07-06 10:57:53
【问题描述】:

我将我的应用程序作为具有不同片段的单个 Activity。我通过左侧的导航抽屉导航片段。问题是我需要为每个片段设置不同的工具栏。

我所做的是在片段布局中设置工具栏,如下所示:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.hectorviov.###.activity.HomeFragment">

    <include
        android:id="@+id/toolbar_logo"
        layout="@layout/toolbar_logo" />
...

并在我的片段上将其设置为 SupportActionBar:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    AppCompatActivity act = (AppCompatActivity)getActivity();

    Toolbar mToolbar = act.findViewById(R.id.toolbar_logo);

    act.setSupportActionBar(mToolbar);
}

它工作正常,问题是,它没有在操作栏的左侧显示汉堡图标。它向我显示了一个警告:

W/ActionBarDrawerToggle: DrawerToggle may not show up because NavigationIcon is not visible. You may need to call actionbar.setDisplayHomeAsUpEnabled(true);

所以我在谷歌上搜索了很多之后尝试了不同的方法来做到这一点。我尝试的最后一个是:

((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(true);

无论我尝试什么,它总是给我一个 NullPointerException:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hectorviov.###/com.hectorviov.###.activity.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayShowHomeEnabled(boolean)' on a null object reference
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)

请帮忙!!

【问题讨论】:

标签: java android android-fragments nullpointerexception android-actionbar


【解决方案1】:

试试这个

AppCompatActivity activity = (AppCompatActivity) getActivity();
if (activity != null) {
    ActionBar ab = activity.getSupportActionBar();
    if (ab != null) {
        ab.setDisplayHomeAsUpEnabled(true);
    }
}

推荐this link会帮助你。

不显示汉堡图标? 你需要添加 ActionBarDrawerToggle 对象

public class DrawerActivity extends AppCompatActivity {

private DrawerLayout drawerLayout;
private ActionBarDrawerToggle drawerToggle;



private void setupDrawerLayout() {
     drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
     drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open, R.string.close);
     drawerLayout.setDrawerListener(drawerToggle);
}



@Override
protected void onPostCreate(Bundle savedInstanceState) {
     super.onPostCreate(savedInstanceState);
     drawerToggle.syncState();
}

【讨论】:

  • 该应用运行正常,我可以从左侧滑动以显示导航抽屉,但仍不显示汉堡图标。
  • 什么时候应该调用 setupDrawerLayout()?我现在收到NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBarDrawerToggle.syncState()' on a null object reference
  • 什么时候应该调用 setupDrawerLayout()? ---不,你能分享你的整个 xml 代码 DrawerLayout
猜你喜欢
  • 2019-09-09
  • 2023-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-11
  • 1970-01-01
  • 1970-01-01
  • 2016-03-26
相关资源
最近更新 更多