Trouble implementing Material Theme

余生长醉 提交于 2019-11-30 16:20:27

问题


I am using this as a tutorial to implement the Material Theme to an existing app on pre Android 5.0 devices. My problem is that I am getting a Null Pointer Exception every-time I call getActionBar().something or getSupportActionBar().something.

Here is all I have done to implement the Material Theme.

In 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>

In my 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>

As I have set the windowActionBar to false, I have a toolbar layout.

toolbar.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"/>

And finally on to the 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
    }
}

I am adding my theme in the Manifest:

android:theme="@style/AppTheme"

My Logcat:

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

My questions:

  • Why am I getting a Null Pointer Exception for the getActionBar() when I am adding my toolbar layout to the MainActivity?
  • How do I add Material design theme to my app.

Cheers, Rakshak


回答1:


UPDATE: your actual problem is that you have the <android.support.v7.widget.Toolbar code in an own toolbar.xml file. this has to be in activity_main.xml and all other layouts that you want to use with an actionbar.


The Toolbar allows more customization than the default ActionBar (mostly appearance related). If you want or need to customize the actionbar, you need to set a Toolbar as ActionBar. If not. you dont need to do that and you can simply use the default ActionBar provided by the Theme (you have to remove the windowActionBar false part from your theme).

if you want to use a toolbar, you have to ensure that there is a Toolbar view in your layouts.

<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"/>

then you can use your code.

but you should also ensure that getSupportActionBar() does not return null before you use it. in case that the layout is missing the toolbar.


OLD ANSWER BELOW


Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
    setSupportActionBar(toolbar);
}
getSupportActionBar().setDisplayHomeAsUpEnabled(true); // NULL POINTER EXCEPTION here
getSupportActionBar().setHomeButtonEnabled(true);

you only set the toolbar if the view has been found. it seems that the view has not been found.

after the if you access the actionbar that has not been set and is therefore null.

also in your them you have <item name="windowActionBar">false</item> that disables the default actionbar.

you can do following:

  • set windowActionbar to true in your theme then you have the default actionbar and you wont need the Toolbar.
  • if you want to use your toolbar on a non default location you have to ensure that findViewById(R.id.toolbar) actually returns a toolbar (check your layouts that there is a Toolbar with that id)
  • or do not use getSupportActionBar without testing it for null.


来源:https://stackoverflow.com/questions/26859339/trouble-implementing-material-theme

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!