简体   繁体   中英

Trying to setDisplayHomeAsUpEnabled(true) in my Android app and it's giving me a NullPointerException

I have my app as a single Activity with different fragments. I navigate the fragments via the Navigation Drawer on the left. The thing is that I need to have different toolbars for each fragment.

What I did was set the toolbar in the layout of the fragment like so:

<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" />
...

And set it as SupportActionBar on my fragment with with:

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

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

    act.setSupportActionBar(mToolbar);
}

It works correctly, the thing is, it doesn't show the hamburger icon on the left of the action bar. It shows me a warning:

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

So I tried different ways to do this after googling a lot. The last one I tried is:

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

And no matter what I try, it always gives me a 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)

Please help!!

try with this

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

refer this link will help you.

doesn't show the hamburger icon? you need to add ActionBarDrawerToggle object

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM