Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting null in MenuInflater Android using Latest SupportVersion

I am trying to create a badge like view in the Action bar. (Using the latest support library).

I am trying the following way. But its saying null pointer exception.

    public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.activity_dashboard_menu, menu);

    MenuItem textBadge = menu.findItem(R.id.badge);
    RelativeLayout badgeLayout = (RelativeLayout) MenuItemCompat.getActionView(textBadge);
    TextView tv = (TextView) badgeLayout.findViewById(R.id.actionbar_notifcation_textview);
    tv.setText("12");

    return super.onCreateOptionsMenu(menu);

}

Menu.xml is

    <item
    android:id="@+id/badge"
    android:actionLayout="@layout/actionbar_badge_layout"
    android:icon="@drawable/btn_star_off_disabled_holo_light"
    android:showAsAction="always|withText">
</item> 

and the Layout.xml is

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="48dp"
android:layout_height="fill_parent"
android:layout_gravity="right" >

<!-- Menu Item Image -->
<ImageView
    android:layout_width="48dp"
    android:layout_height="fill_parent"
    android:clickable="true"
    android:src="@drawable/btn_star_off_disabled_holo_light" />

<!-- Badge Count -->    
<TextView
    android:id="@+id/actionbar_notifcation_textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:padding="5dp"
    android:text="99"
    android:textColor="#ffffff" />

</RelativeLayout>

And here is my Log:

01-22 09:12:45.439: E/AndroidRuntime(4819): FATAL EXCEPTION: main
01-22 09:12:45.439: E/AndroidRuntime(4819): java.lang.NullPointerException
01-22 09:12:45.439: E/AndroidRuntime(4819):     at    com.activities.DashboardActivity.onCreateOptionsMenu(DashboardActivity.java:617)
01-22 09:12:45.439: E/AndroidRuntime(4819):     at    android.app.Activity.onCreatePanelMenu(Activity.java:2158)
01-22 09:12:45.439: E/AndroidRuntime(4819):     at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:224)
01-22 09:12:45.439: E/AndroidRuntime(4819):     at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:232)
01-22 09:12:45.439: E/AndroidRuntime(4819):     at android.support.v7.app.ActionBarActivityDelegateBase$1.run(ActionBarActivityDelegateBase.java:70)
01-22 09:12:45.439: E/AndroidRuntime(4819):     at android.os.Handler.handleCallback(Handler.java:587)
01-22 09:12:45.439: E/AndroidRuntime(4819):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-22 09:12:45.439: E/AndroidRuntime(4819):     at android.os.Looper.loop(Looper.java:130)
01-22 09:12:45.439: E/AndroidRuntime(4819):     at android.app.ActivityThread.main(ActivityThread.java:3687)
01-22 09:12:45.439: E/AndroidRuntime(4819):     at java.lang.reflect.Method.invokeNative(Native Method)
01-22 09:12:45.439: E/AndroidRuntime(4819):     at java.lang.reflect.Method.invoke(Method.java:507)
01-22 09:12:45.439: E/AndroidRuntime(4819):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
01-22 09:12:45.439: E/AndroidRuntime(4819):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
01-22 09:12:45.439: E/AndroidRuntime(4819):     at dalvik.system.NativeStart.main(Native Method)
like image 494
dhiku Avatar asked Dec 08 '22 10:12

dhiku


1 Answers

Thank you beworker for your help.

For all those who are looking for answer.

Its in the Menu XML thats causing the Null Pointer Exception. Changed android:actionLayout to app:actionLayout.

Earlier:

 <item
android:id="@+id/badge"
android:actionLayout="@layout/actionbar_badge_layout"
android:icon="@drawable/btn_star_off_disabled_holo_light"
android:showAsAction="always|withText">

Changed to:

 <item
android:id="@+id/badge"
yourapp:actionLayout="@layout/actionbar_badge_layout"
android:icon="@drawable/btn_star_off_disabled_holo_light"
yourapp:showAsAction="always|withText">

like image 99
dhiku Avatar answered May 02 '23 14:05

dhiku