android - setSupportActionBar() throws Nullpointer exception -
i new android , following following tutorial material design toolbar :
http://www.android4devs.com/2014/12/how-to-make-material-design-app.html
but after implementation of code. following error shown in logcat :
04-01 19:16:10.214 2246-2253/com.example.bhaskar.ddit_results e/art﹕ failed sending reply debugger: broken pipe 04-01 19:16:11.985 2246-2246/com.example.bhaskar.ddit_results e/androidruntime﹕ fatal exception: main process: com.example.bhaskar.ddit_results, pid: 2246 java.lang.runtimeexception: unable start activity componentinfo{com.example.bhaskar.ddit_results/com.example.bhaskar.ddit_results.mainactivity}: java.lang.nullpointerexception: attempt invoke virtual method 'java.lang.charsequence android.support.v7.widget.toolbar.gettitle()' on null object reference @ android.app.activitythread.performlaunchactivity(activitythread.java:2298) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2360) @ android.app.activitythread.access$800(activitythread.java:144) @ android.app.activitythread$h.handlemessage(activitythread.java:1278) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5221) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:899) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:694) caused by: java.lang.nullpointerexception: attempt invoke virtual method 'java.lang.charsequence android.support.v7.widget.toolbar.gettitle()' on null object reference @ android.support.v7.internal.widget.toolbarwidgetwrapper.<init>(toolbarwidgetwrapper.java:95) @ android.support.v7.internal.widget.toolbarwidgetwrapper.<init>(toolbarwidgetwrapper.java:88) @ android.support.v7.internal.app.toolbaractionbar.<init>(toolbaractionbar.java:84) @ android.support.v7.app.actionbaractivitydelegatebase.setsupportactionbar(actionbaractivitydelegatebase.java:175) @ android.support.v7.app.actionbaractivity.setsupportactionbar(actionbaractivity.java:92) @ com.example.bhaskar.ddit_results.mainactivity.oncreate(mainactivity.java:41) @ android.app.activity.performcreate(activity.java:5933) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1105) @ android.app.activitythread.performlaunchactivity(activitythread.java:2251) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2360) @ android.app.activitythread.access$800(activitythread.java:144) @ android.app.activitythread$h.handlemessage(activitythread.java:1278) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5221) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:899) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:694)
which caused nullpointerexception.i using android studio 1.0.1.
styles.xml :
<resources> <!-- base application theme. --> <style name="mytheme" parent="mytheme.base"> </style> <style name="mytheme.base" parent="theme.appcompat.light.noactionbar"> <item name="colorprimary">#ff32cd32</item> <item name="colorprimarydark">#ff04cd2c</item> <item name="coloraccent">#ff4081</item> </style>
v21/styles.xml:
<resources> <style name="mytheme" parent="mytheme.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>
in toolbar.xml i've :
<android.support.v7.widget.toolbar xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorprimary" app:theme="@style/themeoverlay.appcompat.dark.actionbar" app:popuptheme="@style/themeoverlay.appcompat.light" android:elevation="4dp"/>
also i've included toolbar layout in activity_main.xml file. in mainactivity.java :
public class mainactivity extends actionbaractivity { .... private toolbar toolbar; public static final int record_entry_request_code=3; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // set our view "main" layout resource toolbar = (toolbar)findviewbyid(r.id.toolbar); //toolbar take on default actionbar characteristics setcontentview(r.layout.activity_main); setsupportactionbar(toolbar); settitle("ddit_results"); .... }
i searched problem , didn't come solutions.hopefully, recognizes error , has quick answer - otherwise can provide more information if requested.any appriciated.
you need call setcontentview
before adding view
s such toolbar
.
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); // <-- call first toolbar = (toolbar)findviewbyid(r.id.toolbar); // <-- setup views after setsupportactionbar(toolbar); settitle("ddit_results"); }
please see official android developers blog here: http://android-developers.blogspot.com/2014/10/appcompat-v21-material-design-for-pre.html
action bar
to use toolbar action bar, first disable decor-provided action bar. easiest way have theme extend theme.appcompat.noactionbar (or light variant).
second, create toolbar instance, via layout xml:
<android.support.v7.widget.toolbar android:id=”@+id/my_awesome_toolbar” android:layout_height=”wrap_content” android:layout_width=”match_parent” android:minheight=”?attr/actionbarsize” android:background=”?attr/colorprimary” />
the height, width, background, , on totally you; these examples. toolbar viewgroup, can style , position want.
then in activity or fragment, set toolbar act action bar:
@override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.blah); toolbar toolbar = (toolbar) findviewbyid(r.id.my_awesome_toolbar); setsupportactionbar(toolbar); }
from point on, menu items displayed in toolbar, populated via standard options menu callbacks.
Comments
Post a Comment