Android 执行材料主题时遇到问题

Android 执行材料主题时遇到问题,android,android-actionbar,material-design,material-theme,Android,Android Actionbar,Material Design,Material Theme,我用它作为一个教程,在安卓5.0之前的设备上实现现有应用程序的材质主题。我的问题是每次调用getActionBar().something或getSupportActionBar().something时,都会出现Null指针异常 以下是我为实现材料主题所做的一切 在values/styles.xml中 <resources xmlns:android="http://schemas.android.com/apk/res/android"> <!-- Base ap

我用它作为一个教程,在安卓5.0之前的设备上实现现有应用程序的材质主题。我的问题是每次调用
getActionBar().something
getSupportActionBar().something
时,都会出现
Null指针异常

以下是我为实现材料主题所做的一切

在values/styles.xml中

<resources xmlns:android="http://schemas.android.com/apk/res/android">

<!--
    Base application theme, dependent on API level. This theme is replaced
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppTheme" parent="AppTheme.Base"/>

<style name="AppTheme.Base" parent="Theme.AppCompat.Light">
    <item name="colorPrimary">#1A7E99</item>
    <item name="colorPrimaryDark">#16657A</item>   
    <item name="android:windowNoTitle">true</item>
    <item name="windowActionBar">false</item>   
</style>

<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
    <item name="spinBars">true</item>
    <item name="color">@android:color/white</item>
</style>

<!-- View pager progress indecator theme -->
 <style name="StyledIndicators" parent="AppBaseTheme">
    <item name="vpiCirclePageIndicatorStyle">@style/CustomCirclePageIndicator</item>
 </style>

 <style name="CustomCirclePageIndicator">
    <item name="fillColor">#ffffff</item>
    <item name ="strokeWidth">2dp</item> 
    <item name ="strokeColor">#cfd3d4</item>              
    <item name="radius">8dp</item>
    <item name="centered">true</item>
</style>

<style name="Widget"></style>

<style name="Widget.FloatingHintEditText" parent="@android:style/Widget.EditText">
    <item name="android:paddingTop">0dp</item>
</style>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="AppTheme.Base">
    <item name="android:windowContentTransitions">true</item>
    <item name="android:windowAllowEnterTransitionOverlap">true</item>
    <item name="android:windowAllowReturnTransitionOverlap">true</item>
    <item name="android:windowSharedElementEnterTransition">@android:transition/move</item>
    <item name="android:windowSharedElementExitTransition">@android:transition/move</item>
</style>
</resources>
我正在清单中添加我的主题:

android:theme="@style/AppTheme"
我的日志:

11-11 12:40:54.798: E/ResourceType(32738): Style contains key with bad entry: 0x01010479
11-11 12:40:55.349: E/AndroidRuntime(32738): FATAL EXCEPTION: main
11-11 12:40:55.349: E/AndroidRuntime(32738): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.driverdesignstudio.drvr/com.driverdesignstudio.drvr.MainActivity}: java.lang.NullPointerException
11-11 12:40:55.349: E/AndroidRuntime(32738):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
11-11 12:40:55.349: E/AndroidRuntime(32738):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
11-11 12:40:55.349: E/AndroidRuntime(32738):    at android.app.ActivityThread.access$600(ActivityThread.java:162)
11-11 12:40:55.349: E/AndroidRuntime(32738):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
11-11 12:40:55.349: E/AndroidRuntime(32738):    at android.os.Handler.dispatchMessage(Handler.java:107)
11-11 12:40:55.349: E/AndroidRuntime(32738):    at android.os.Looper.loop(Looper.java:194)
11-11 12:40:55.349: E/AndroidRuntime(32738):    at android.app.ActivityThread.main(ActivityThread.java:5371)
11-11 12:40:55.349: E/AndroidRuntime(32738):    at java.lang.reflect.Method.invokeNative(Native Method)
11-11 12:40:55.349: E/AndroidRuntime(32738):    at java.lang.reflect.Method.invoke(Method.java:525)
11-11 12:40:55.349: E/AndroidRuntime(32738):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
11-11 12:40:55.349: E/AndroidRuntime(32738):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
11-11 12:40:55.349: E/AndroidRuntime(32738):    at dalvik.system.NativeStart.main(Native Method)
11-11 12:40:55.349: E/AndroidRuntime(32738): Caused by: java.lang.NullPointerException
11-11 12:40:55.349: E/AndroidRuntime(32738):    at com.driverdesignstudio.drvr.MainActivity.onCreate(MainActivity.java:123)
11-11 12:40:55.349: E/AndroidRuntime(32738):    at android.app.Activity.performCreate(Activity.java:5122)
11-11 12:40:55.349: E/AndroidRuntime(32738):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
11-11 12:40:55.349: E/AndroidRuntime(32738):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
11-11 12:40:55.349: E/AndroidRuntime(32738):    ... 11 more
我的问题是:

  • 为什么在将工具栏布局添加到MainActivity时,getActionBar()会出现空指针异常
  • 如何将材质设计主题添加到我的应用程序中
干杯,

Rakshak更新:你的实际问题是你有
更新:你的实际问题是你有
你是在Android Studio中使用Gradle Build还是Eclipse ADT?Eclipse ADT和我都包含了AppCompacy库。是的,我非常确定。我在我的主题中将“windowActionBar”设置为true,并注释掉了工具栏上的内容,应用程序运行良好。我甚至将菜单栏设置为箭头动画,这是AppCompact库的一部分@道奇似乎在正确的道路上解决了你的问题。你是使用Android Studio中的Gradle Build还是Eclipse ADT?Eclipse ADT和我已经包括了AppCompacy库。是的,我非常确定。我在我的主题中将“windowActionBar”设置为true,并注释掉了工具栏上的内容,应用程序运行良好。我甚至将菜单栏设置为箭头动画,这是AppCompact库的一部分@道奇似乎是在正确的道路上解决你的答案。我不知道为什么教程状态删除windowActionBar。如果要自定义位置或大小,只需执行此操作。如果你想像5.0之前那样将它用作简单的actionbar,你不需要删除它。同样,要删除它,可以将Theme.AppCompat.NoActionBar主题用作parent.well。我认为教程是这样说的,因为工具栏允许更多的定制,如果需要的话。事实是。如果要将工具栏实例用作操作栏,只需禁用windowActionBar。但是再一次。如果op想要禁用actionbar。他必须确保工具栏视图出现在他的布局中。如果NPe不存在,为了避免NPe,他必须在使用前检查getSupportActionBar。我将主题中的“windowActionBar”元素设置为true,并从我的代码中注释掉工具栏内容,它工作正常。Yayayayayaya:D你们能想出我的应用程序找不到工具栏的原因吗?我肯定有它的布局。抱歉太晚了,我删除了它们。考虑一下验证@Dodge的答案是否为可接受的答案,并将其+1。我不知道教程中为什么要删除windowActionBar。如果要自定义位置或大小,只需执行此操作。如果你想像5.0之前那样将它用作简单的actionbar,你不需要删除它。同样,要删除它,可以将Theme.AppCompat.NoActionBar主题用作parent.well。我认为教程是这样说的,因为工具栏允许更多的定制,如果需要的话。事实是。如果要将工具栏实例用作操作栏,只需禁用windowActionBar。但是再一次。如果op想要禁用actionbar。他必须确保工具栏视图出现在他的布局中。如果NPe不存在,为了避免NPe,他必须在使用前检查getSupportActionBar。我将主题中的“windowActionBar”元素设置为true,并从我的代码中注释掉工具栏内容,它工作正常。Yayayayayaya:D你们能想出我的应用程序找不到工具栏的原因吗?我肯定有它的布局。抱歉太晚了,我删除了它们。考虑确认@Dodge的答案为已接受答案,并将其+1。
android:theme="@style/AppTheme"
11-11 12:40:54.798: E/ResourceType(32738): Style contains key with bad entry: 0x01010479
11-11 12:40:55.349: E/AndroidRuntime(32738): FATAL EXCEPTION: main
11-11 12:40:55.349: E/AndroidRuntime(32738): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.driverdesignstudio.drvr/com.driverdesignstudio.drvr.MainActivity}: java.lang.NullPointerException
11-11 12:40:55.349: E/AndroidRuntime(32738):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
11-11 12:40:55.349: E/AndroidRuntime(32738):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
11-11 12:40:55.349: E/AndroidRuntime(32738):    at android.app.ActivityThread.access$600(ActivityThread.java:162)
11-11 12:40:55.349: E/AndroidRuntime(32738):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
11-11 12:40:55.349: E/AndroidRuntime(32738):    at android.os.Handler.dispatchMessage(Handler.java:107)
11-11 12:40:55.349: E/AndroidRuntime(32738):    at android.os.Looper.loop(Looper.java:194)
11-11 12:40:55.349: E/AndroidRuntime(32738):    at android.app.ActivityThread.main(ActivityThread.java:5371)
11-11 12:40:55.349: E/AndroidRuntime(32738):    at java.lang.reflect.Method.invokeNative(Native Method)
11-11 12:40:55.349: E/AndroidRuntime(32738):    at java.lang.reflect.Method.invoke(Method.java:525)
11-11 12:40:55.349: E/AndroidRuntime(32738):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
11-11 12:40:55.349: E/AndroidRuntime(32738):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
11-11 12:40:55.349: E/AndroidRuntime(32738):    at dalvik.system.NativeStart.main(Native Method)
11-11 12:40:55.349: E/AndroidRuntime(32738): Caused by: java.lang.NullPointerException
11-11 12:40:55.349: E/AndroidRuntime(32738):    at com.driverdesignstudio.drvr.MainActivity.onCreate(MainActivity.java:123)
11-11 12:40:55.349: E/AndroidRuntime(32738):    at android.app.Activity.performCreate(Activity.java:5122)
11-11 12:40:55.349: E/AndroidRuntime(32738):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
11-11 12:40:55.349: E/AndroidRuntime(32738):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
11-11 12:40:55.349: E/AndroidRuntime(32738):    ... 11 more
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimaryDark"/>
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
    setSupportActionBar(toolbar);
}
getSupportActionBar().setDisplayHomeAsUpEnabled(true); // NULL POINTER EXCEPTION here
getSupportActionBar().setHomeButtonEnabled(true);