The problem with the implementation of the material theme

I use this as a tutorial on introducing a material theme into an existing application on devices prior to Android 5.0. My problem is that I get a Null Pointer Exception every time I call getActionBar().something getSupportActionBar().something or getSupportActionBar().something .

That’s all I’ve done to implement the material theme.

In the 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 set 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 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 add my theme to 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 do I get a Null Pointer exception for getActionBar () when I add my toolbar layout to MainActivity?
  • How to add a material theme to my application.

Cheers, Rakshak

+1
source share
1 answer

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


The toolbar allows you to customize more than the default ActionBar (mostly related to appearance). If you need or need to customize the action bar, you need to set the toolbar as an ActionBar. If not. you don't need to do this, and you can just use the default ActionBar provided by Theme (you need to remove the fake portion of the windowActionBar from your theme).

if you want to use the toolbar, you have to make sure that your layout has a toolbar view.

 <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 must also ensure that getSupportActionBar() does not return null before using it. if there is no layout of 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 install the toolbar if the view was found. it seems that the point of view was not found.

after you gain access to the action bar, which has not been installed, and therefore is null.

also at your disposal you have <item name="windowActionBar">false</item> , which disables the default action bar.

you can do the following:

  • set windowActionbar to true in your theme, then you have a default action bar and you don't need a toolbar.
  • If you want to use your toolbar in a non-standard location, you must make sure that findViewById(R.id.toolbar) actually returns the toolbar (check your layouts that there is a toolbar with this identifier)
  • or don't use getSupportActionBar without testing it for null.
+4
source

Source: https://habr.com/ru/post/1206691/


All Articles