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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我想添加一个自定义ActionBar。我试图在我的应用程序中实现现有代码,但getSupportActionBar()返回null相关的知识,希望对你有一定的参考价值。

//activity_卖弄_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>

//main pager activity.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_action bar.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">

//Android manifest.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)

以上是关于我想添加一个自定义ActionBar。我试图在我的应用程序中实现现有代码,但getSupportActionBar()返回null的主要内容,如果未能解决你的问题,请参考以下文章

在 XML 中为 ActionBar 定义自定义徽标(不同于徽标)?

从 Fragment 设置自定义 ActionBar 标题

如何在 ActionBar 中显示自定义视图?

无法单击自定义操作视图

如何在 ActionBar 标题中设置自定义字体?

如何创建自定义 ActionBar?