我想添加一个自定义ActionBar。我试图在我的应用程序中实现现有代码,但getSupportActionBar()返回null

时间:2018-09-08 04:17:55

标签: android android-actionbar

// activity_main_pager.xml

<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/drawerid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainPagerActivity">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include
        android:id="@+id/appbar"
        layout="@layout/navigation_actionbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <android.support.v4.view.ViewPager
        android:id="@+id/main_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/maintab"
        android:layout_below="@id/appbar"

        >

    </android.support.v4.view.ViewPager>

    <android.support.design.widget.TabLayout
        android:id="@+id/maintab"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/colorPrimaryDark"
        app:tabSelectedTextColor="@color/colorAccent"
        app:tabTextColor="#ffffff">

    </android.support.design.widget.TabLayout>
</RelativeLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"


    app:menu="@menu/main_activity_actions">

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

// MainPagerActivity.java

public class MainPagerActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private TabLayout mTabLayout;
private DrawerLayout mdrawer;
private ActionBarDrawerToggle toggle;
private Toolbar mtoolbar;
CircleImageView imageView;
TextView textView, emailview;

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


    mTabLayout = (TabLayout) findViewById(R.id.maintab);
    ViewPager viewPager = (ViewPager) findViewById(R.id.main_pager);
    viewPager.setAdapter(new MainAdapter(this));
    mTabLayout.setupWithViewPager(viewPager);


    mtoolbar = (Toolbar) findViewById(R.id.n_action_toolbar);
    setSupportActionBar(mtoolbar);

    mdrawer = (DrawerLayout) findViewById(R.id.drawerid);
    toggle = new ActionBarDrawerToggle(this, mdrawer, R.string.open, R.string.close);
    mdrawer.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
    navigationView.setNavigationItemSelectedListener(this);


    View headerView = navigationView.inflateHeaderView(R.layout.navigation_header);
    textView = (TextView) headerView.findViewById(R.id.nameView);
    emailview = (TextView) headerView.findViewById(R.id.emailView);
    imageView = (CircleImageView) headerView.findViewById(R.id.imageView);


    try {
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    } catch (NullPointerException e) {
        e.printStackTrace();
    }


}


@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {

    switch (item.getItemId()) {


        case R.id.logoutapp:

            break;
        case R.id.logout:

            break;

    }


    return false;
}


public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items


    if (toggle.onOptionsItemSelected(item)) {

        return true;
    }
    return super.onOptionsItemSelected(item);

}

}

// navigation_actionbar.xml

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/n_action_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:orientation="vertical"
android:theme="@style/ThemeOverlay.AppCompat.Dark">

// AndroidManifest.xml

 <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity android:name=".LogInActivity"></activity>
    <activity android:name=".MainPagerActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

// styles.xml

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

//错误

    W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
W/ActionBarDrawerToggle: DrawerToggle may not show up because NavigationIcon is not visible. You may need to call actionbar.setDisplayHomeAsUpEnabled(true);
W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
                  at ava.earn_real_money.groot.MainPagerActivity.onCreate(MainPagerActivity.java:60)
                  at android.app.Activity.performCreate(Activity.java:6664)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                  at android.app.ActivityThread.-wrap12(ActivityThread.java)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6077)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

3 个答案:

答案 0 :(得分:0)

OnCreate()方法添加

 mtoolbar = (Toolbar) findViewById(R.id.n_action_toolbar);
 setUpToolbar();

setUpToolbar()方法检查操作栏null是否

private void setUPToolbar () {
        setSupportActionBar (mtoolbar);
        ActionBar actionBar=getSupportActionBar ();
        if (actionBar!=null){
            actionBar.setTitle (R.string.lead_title);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(true);
        }
}

答案 1 :(得分:0)

您应该检查第二行错误:您可能需要调用actionbar.setDisplayHomeAsUpEnabled(true);

答案 2 :(得分:0)

我已通过替换

解决了此问题
//this code is given in activity_main_pager.xml in above question

<include android:id="@+id/appbar"  
layout="@layout/navigation_actionbar"  
android:layout_width="match_parent"  
android:layout_height="wrap_content" />  

直接与

//this code is given in navigation_actionbar.xml in above question

<android.support.v7.widget.Toolbar  
xmlns:android="schemas.android.com/apk/res/android";  
android:id="@+id/n_action_toolbar"  
android:layout_width="match_parent"  
android:layout_height="wrap_content"  
android:background="@color/colorPrimary"  
android:orientation="vertical"  
android:theme="@style/ThemeOverlay.AppCompat.Dark">  

在activity_main_pager.xml文件中。