android - Trouble implementing Material Theme -
i using this tutorial implement material theme existing app on pre android 5.0 devices. problem getting null pointer exception
every-time call getactionbar().something
or getsupportactionbar().something
.
here have done implement material theme.
in values/styles.xml
<resources xmlns:android="http://schemas.android.com/apk/res/android"> <!-- base application theme, dependent on api level. theme replaced appbasetheme 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 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 have set windowactionbar false, have 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 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 code } }
i adding theme in manifest:
android:theme="@style/apptheme"
my logcat:
11-11 12:40:54.798: e/resourcetype(32738): style contains key 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 start activity componentinfo{com.driverdesignstudio.drvr/com.driverdesignstudio.drvr.mainactivity}: java.lang.nullpointerexception 11-11 12:40:55.349: e/androidruntime(32738): @ android.app.activitythread.performlaunchactivity(activitythread.java:2343) 11-11 12:40:55.349: e/androidruntime(32738): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2395) 11-11 12:40:55.349: e/androidruntime(32738): @ android.app.activitythread.access$600(activitythread.java:162) 11-11 12:40:55.349: e/androidruntime(32738): @ android.app.activitythread$h.handlemessage(activitythread.java:1364) 11-11 12:40:55.349: e/androidruntime(32738): @ android.os.handler.dispatchmessage(handler.java:107) 11-11 12:40:55.349: e/androidruntime(32738): @ android.os.looper.loop(looper.java:194) 11-11 12:40:55.349: e/androidruntime(32738): @ android.app.activitythread.main(activitythread.java:5371) 11-11 12:40:55.349: e/androidruntime(32738): @ java.lang.reflect.method.invokenative(native method) 11-11 12:40:55.349: e/androidruntime(32738): @ java.lang.reflect.method.invoke(method.java:525) 11-11 12:40:55.349: e/androidruntime(32738): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:833) 11-11 12:40:55.349: e/androidruntime(32738): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:600) 11-11 12:40:55.349: e/androidruntime(32738): @ 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): @ com.driverdesignstudio.drvr.mainactivity.oncreate(mainactivity.java:123) 11-11 12:40:55.349: e/androidruntime(32738): @ android.app.activity.performcreate(activity.java:5122) 11-11 12:40:55.349: e/androidruntime(32738): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1081) 11-11 12:40:55.349: e/androidruntime(32738): @ android.app.activitythread.performlaunchactivity(activitythread.java:2307) 11-11 12:40:55.349: e/androidruntime(32738): ... 11 more
my questions:
- why getting null pointer exception getactionbar() when adding toolbar layout mainactivity?
- how add material design theme app.
cheers, rakshak
update: actual problem have <android.support.v7.widget.toolbar
code in own toolbar.xml
file. has in activity_main.xml
, other layouts want use actionbar.
the toolbar allows more customization default actionbar (mostly appearance related). if want or need customize actionbar, need set toolbar actionbar. if not. dont need , can use default actionbar provided theme (you have remove windowactionbar false part theme).
if want use toolbar, have ensure there toolbar view in 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 can use code.
but should ensure getsupportactionbar()
not return null before use it. in case layout missing 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 set toolbar if view has been found. seems view has not been found.
after if access actionbar has not been set , therefore null.
also in them have <item name="windowactionbar">false</item>
disables default actionbar.
you can following:
- set
windowactionbar
true in theme have default actionbar , wont need toolbar. - if want use toolbar on non default location have ensure
findviewbyid(r.id.toolbar)
returns toolbar (check layouts there toolbar id) - or not use getsupportactionbar without testing null.
Comments
Post a Comment