我将其用作教程,在 Android 5.0 之前的设备上为现有应用程序实现 Material Theme。我的问题是我Null Pointer Exception
每次打电话 getActionBar().something
或getSupportActionBar().something
.
这是我为实现 Material Theme 所做的一切。
在值/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>
在我的 values-v21/styles.xml
<?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>
由于我已将 windowActionBar 设置为 false,因此我有一个工具栏布局。
工具栏.xml
<?xml version="1.0" encoding="utf-8"?>
<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/colorPrimary"/>
最后到 MainActivity.java
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
}
getSupportActionBar().setDisplayHomeAsUpEnabled(true); // NULL POINTER EXCEPTION here
getSupportActionBar().setHomeButtonEnabled(true);
// rest of my code
}
}
我在清单中添加我的主题:
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() 会出现空指针异常?
- 如何将 Material Design 主题添加到我的应用程序中。
干杯,拉克沙克