android - imageview in actionbar with navigationdrawer -
i'm using navigation drawer , define theme change actionbar color. want add image menu (bell) actionbar receive notification. problem application crush log point @ imageview imageview bell= (imageview)badgelayout.findviewbyid(r.id.bell);
this log:
04-29 16:38:14.835 10481-10481/com.project e/androidruntime﹕ fatal exception: main java.lang.nullpointerexception @ com.nearby.project.princ.main.oncreateoptionsmenu(main.java:520) @ android.app.activity.oncreatepanelmenu(activity.java:2553) @ android.support.v4.app.fragmentactivity.oncreatepanelmenu(fragmentactivity.java:275) @ android.support.v7.app.actionbaractivity.superoncreatepanelmenu(actionbaractivity.java:276) @ android.support.v7.app.actionbaractivitydelegate$1.oncreatepanelmenu(actionbaractivitydelegate.java:79) @ android.support.v7.app.actionbaractivitydelegatebase.preparepanel(actionbaractivitydelegatebase.java:979) @ android.support.v7.app.actionbaractivitydelegatebase.doinvalidatepanelmenu(actionbaractivitydelegatebase.java:1182) @ android.support.v7.app.actionbaractivitydelegatebase.access$100(actionbaractivitydelegatebase.java:79) @ android.support.v7.app.actionbaractivitydelegatebase$1.run(actionbaractivitydelegatebase.java:118) @ android.os.handler.handlecallback(handler.java:615) @ android.os.handler.dispatchmessage(handler.java:92) @ android.os.looper.loop(looper.java:137) @ android.app.activitythread.main(activitythread.java:4921) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:511) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1038) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:805) @ dalvik.system.nativestart.main(native method)
this theme:
<?xml version="1.0" encoding="utf-8"?> <resources> <!-- theme applied application or activity --> <style name="customactionbartheme" parent="@style/theme.appcompat.light.darkactionbar"> <item name="android:actionbarstyle">@style/myactionbar</item> <!-- support library compatibility --> <item name="actionbarstyle">@style/myactionbar</item> </style> <!-- actionbar styles --> <style name="myactionbar" parent="@style/widget.appcompat.light.actionbar.solid.inverse"> <item name="android:background">@color/blue_pressed_want</item> <item name="android:windownotitle">false</item> <item name="android:windowactionbar">true</item> <item name="windowactionbar">true</item> <!-- support library compatibility --> <item name="background">@color/blue_pressed_want</item> </style>
this main activity (oncreateoptionmenu method):
@override public boolean oncreateoptionsmenu(menu menu) { menuinflater inflater = getmenuinflater();//getsupportmenuinflater(); getmenuinflater().inflate(r.menu.activity_main, menu); relativelayout badgelayout = (relativelayout) menu.finditem(r.id.menu_notif).getactionview(); imageview bell= (imageview)badgelayout.findviewbyid(r.id.bell); //here error // bell.setimageresource(r.drawable.notification); if (sharedpreferences.contains(keynotifcount)) { countnotif = sharedpreferences.getstring(keynotifcount, ""); mnotifcount= integer.parseint(countnotif); } if(mnotifcount!=0) { textview notifcount = (textview) badgelayout.findviewbyid(r.id.actionbar_notifcation_textview); notifcount.setvisibility(view.visible); notifcount.settext(string.valueof(mnotifcount)); etat=true; } if(mnotifcount==0) { textview notifcount = (textview) badgelayout.findviewbyid(r.id.actionbar_notifcation_textviewoff); etat=false; } toast.maketext(this, "notifcount" + mnotifcount, toast.length_short).show(); bell.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { //startactivity(new intent(main.this, management.class)); displayview(4); etat=false; mnotifcount=0; countnotif= string.valueof(mnotifcount); sharedpreferences.editor editor = sharedpreferences.edit(); editor.putstring(keynotifcount, countnotif); editor.commit(); } }); // return true; return super.oncreateoptionsmenu(menu); }
here menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:background="@color/blue_pressed_want" xmlns:tools="http://schemas.android.com/tools" tools:context="com.nearby.pfe.mainactivity"> <item android:id="@+id/menu_notif" android:icon="@drawable/notification" android:actionlayout="@layout/view_feed_update_count" android:showasaction="always" android:title="@string/notif"/>
here layout view_feed_update_count:
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="48dp" android:layout_height="fill_parent" android:layout_gravity="right"> <!-- menu item image --> <imageview android:layout_width="36dp" android:layout_height="fill_parent" android:clickable="true" android:layout_alignparentright="true" android:id="@+id/bell" android:src="@drawable/notification" /> <!-- badge count --> <textview android:id="@+id/actionbar_notifcation_textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/counter_bg" android:layout_alignparentright="true" android:visibility="invisible" android:paddingright="13px" android:paddingleft="13px" android:textcolor="#ffffff" /> <textview android:id="@+id/actionbar_notifcation_textviewoff" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentright="true" android:padding="13px" android:layout_alignleft="@+id/actionbar_notifcation_textview" android:layout_alignstart="@+id/actionbar_notifcation_textview" />
you haven't defined actionview attribute in items relativelayout.
Comments
Post a Comment