I want to change the menu item icon dynamically when I receive a notification from the server. However, I get a NullPointerException when the codes for changing the menu item icon are executed.
The codes that I used to change the menu item icon are defined in the onCreatOptionsMenu method as follows:
@Override public boolean onCreateOptionsMenu(Menu menu) { // getMenuInflater().inflate(R.menu.main, menu); this.menu = menu; if (mDrawerLayout != null && isDrawerOpen()) showGlobalContextActionBar(); MenuInflater menuInflater = this.getMenuInflater(); menuInflater.inflate(R.menu.notification, menu); return super.onCreateOptionsMenu(menu); } }
and in the updateCount method updateCount I change the icon as follows:
public void updateCount(int count) { hot_count = count; System.out.println("Value of count: " + count); runOnUiThread(new Runnable() { @Override public void run() {
Here is my notification file for my menu.
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" > <item android:id="@+id/menu_hotlist" android:actionLayout="@layout/action_bar_notification_icon" android:showAsAction="always" android:icon="@drawable/ic_notification" android:title="Notification" /> </menu>
Here is my logcat:
01-20 15:03:29.811: E/AndroidRuntime(10318): java.lang.NullPointerException 01-20 15:03:29.811: E/AndroidRuntime(10318): at com.xsinfosol.helpdesk_customer.TAB_Activity$3.run(TAB_Activity.java:294) 01-20 15:03:29.811: E/AndroidRuntime(10318): at android.os.Handler.handleCallback(Handler.java:730) 01-20 15:03:29.811: E/AndroidRuntime(10318): at android.os.Handler.dispatchMessage(Handler.java:92) 01-20 15:03:29.811: E/AndroidRuntime(10318): at android.os.Looper.loop(Looper.java:137) 01-20 15:03:29.811: E/AndroidRuntime(10318): at android.os.HandlerThread.run(HandlerThread.java:61) 01-20 15:04:04.881: I/System.out(11629)
Please, help.
android android-actionbar menuitem
ankur arora
source share