Android:如何/将ActionBar更改为ActionBarCompat(工具栏)的教程? [英] Android: How to/Tutorial for changing ActionBar to ActionBarCompat (Toolbar)?

查看:78
本文介绍了Android:如何/将ActionBar更改为ActionBarCompat(工具栏)的教程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直坚持下去,查看了官方指南,等等.任何教程/从ActionBar更改为ActionBarCompat的步骤是什么(对于工具栏和较旧版本的支持?我已经导入了appcompat-v7:21.0 .+,尝试过

I've been stuck on this, checked the official guidance, etc. Any tutorials/what are the steps to change from ActionBar to ActionBarCompat (for the Toolbar and support of older versions? I've imported appcompat-v7:21.0.+, tried

getSupportActionBar();

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);

setSupportActionBar(toolbar);

将主题样式更改为适用于主题的样式...有什么常见的错误要注意或想法吗?

changed theme to appcompat theme in styles... Any common errors to note or ideas?

继续出现这种错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.battery.plusfree/com.battery.plusfree.MainCollectionActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2212)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
        at android.app.ActivityThread.access$800(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5146)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
        at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.battery.plusfree.MainCollectionActivity.onCreate(MainCollectionActivity.java:133)
        at android.app.Activity.performCreate(Activity.java:5231)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5146)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
            at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
            at dalvik.system.NativeStart.main(Native Method)

推荐答案

我在应用程序中了解到,不能同时使用工具栏和操作栏.这样可能就是问题所在,但是我不确定,因为您没有发布代码,但是必须使用styles.xml文件指定一个或类似的内容:

I've learned with my application is that you can't use the toolbar and the actionbar at the same time. So that might be where your issue lies, but I'm not sure because you didn't post your code, but using the styles.xml file you have to specify either or like so:

<style name="AppTheme.Base" parent="Theme.AppCompat">
    <item name="colorPrimary">@color/mycolorprimary</item>
    <item name="colorPrimaryDark">@color/mycolorprimarydark</item>
    <item name="android:windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
</style>

Activity.xml:

Activity.xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- dont use this if you only want to use the actionbar instead -->

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:elevation="5dp"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary" />

</LinearLayout>

Activity.java:

Activity.java:

    public class MyActivity extends ActionBarActivity {

       Toolbar mToolbar;

       public void onCreate(Bundle savedInstanceState) {

          // configure toolbar stuff
          setSupportActionBar(mToolbar);

          // or if you don't want to use the toolbar
          // then change the style values accordingly
          // and then you can run getSupportActionBar() instead

       }

    }

参考:此处..希望这会有所帮助!

Reference: here .. hope this helps!

这篇关于Android:如何/将ActionBar更改为ActionBarCompat(工具栏)的教程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆