0

I have changed some code from ActionBar to a Support.v7.ActionBar - after the normal complications of getting compilation to work I am getting an error in the following code, which previously was working :

   @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        inflater.inflate(R.menu.main_activity_actions, menu);
        RelativeLayout badgeLayout = (RelativeLayout) menu.findItem(R.id.badge).getActionView();

        /**** App crashes here *****/
        TextView tv_mode = (TextView) badgeLayout.findViewById(R.id.actionbar_notifcation_textview_mode);


        tv_mode.setText(modeOutput);
        TextView tv_type = (TextView) badgeLayout.findViewById(R.id.actionbar_notifcation_textview_type);

        tv_type.setText(typeOutput);
        return super.onCreateOptionsMenu(menu);
    }

BadgeLayout is just a Relative layout for the Drawer I am using

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="500dp"
     android:layout_height="fill_parent"
     android:layout_gravity="right"
     android:paddingLeft="16dp"
     android:paddingRight="16dp"
     android:paddingTop="16dp" >

<!-- Menu Item Image -->

<!-- Badge Count -->    
<TextView
    android:id="@+id/actionbar_notifcation_textview_mode"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:textSize="16sp"
    android:background="#F60"
    android:paddingRight="16dp"
    android:paddingLeft="16dp"
    android:layout_marginRight="15sp"
   />
<TextView
    android:id="@+id/actionbar_notifcation_textview_type"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@+id/actionbar_notifcation_textview_mode"
    android:textSize="16sp"
    android:background="#09F"
    android:paddingRight="16dp"
    android:paddingLeft="16dp"
    android:layout_marginRight="15sp"
   />
</RelativeLayout>

The highlighted line in the onCreateOptionsMenu is 345

01-15 21:36:33.889: E/AndroidRuntime(5265): java.lang.NullPointerException
01-15 21:36:33.889: E/AndroidRuntime(5265):     at com.MainActivity.onCreateOptionsMenu(MainActivity.java:345)
01-15 21:36:33.889: E/AndroidRuntime(5265):     at android.app.Activity.onCreatePanelMenu(Activity.java:2504)
01-15 21:36:33.889: E/AndroidRuntime(5265):     at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:224)
01-15 21:36:33.889: E/AndroidRuntime(5265):     at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:232)
01-15 21:36:33.889: E/AndroidRuntime(5265):     at android.support.v7.app.ActionBarActivityDelegateICS.onCreatePanelMenu(ActionBarActivityDelegateICS.java:147)
01-15 21:36:33.889: E/AndroidRuntime(5265):     at android.support.v7.app.ActionBarActivity.onCreatePanelMenu(ActionBarActivity.java:199)
01-15 21:36:33.889: E/AndroidRuntime(5265):     at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.onCreatePanelMenu(ActionBarActivityDelegateICS.java:285)
01-15 21:36:33.889: E/AndroidRuntime(5265):     at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:413)
01-15 21:36:33.889: E/AndroidRuntime(5265):     at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:775)
01-15 21:36:33.889: E/AndroidRuntime(5265):     at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:198)
01-15 21:36:33.889: E/AndroidRuntime(5265):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
01-15 21:36:33.889: E/AndroidRuntime(5265):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
01-15 21:36:33.889: E/AndroidRuntime(5265):     at android.view.Choreographer.doFrame(Choreographer.java:531)
01-15 21:36:33.889: E/AndroidRuntime(5265):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
01-15 21:36:33.889: E/AndroidRuntime(5265):     at android.os.Handler.handleCallback(Handler.java:730)
01-15 21:36:33.889: E/AndroidRuntime(5265):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-15 21:36:33.889: E/AndroidRuntime(5265):     at android.os.Looper.loop(Looper.java:137)
01-15 21:36:33.889: E/AndroidRuntime(5265):     at android.app.ActivityThread.main(ActivityThread.java:5103)
01-15 21:36:33.889: E/AndroidRuntime(5265):     at java.lang.reflect.Method.invokeNative(Native Method)
01-15 21:36:33.889: E/AndroidRuntime(5265):     at java.lang.reflect.Method.invoke(Method.java:525)
01-15 21:36:33.889: E/AndroidRuntime(5265):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-15 21:36:33.889: E/AndroidRuntime(5265):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-15 21:36:33.889: E/AndroidRuntime(5265):     at dalvik.system.NativeStart.main(Native Method)

I can`t understand what it is about my Drawer handling and the addition of the Support.v7.actionbar could be causing this error ... any ideas ?

I tried putting a try catch around the offending line and it seems as if any reference to the drawer TextView findViewById(R.id.actionbar_notifcation_textview_type); generates this null pointer exception

I added the support.v7 as a library to my existing project , wonder if this is related ?

UPDATE:

I removed all references to the support.v7 ActionBar and changed back to the normal ActionBar and it all works fine. So changing causes this weird issue ....

avrono
  • 1,648
  • 3
  • 19
  • 40

1 Answers1

0

Have you checked if badgeLayout is null? There is a possiblity of getActionView() returning null.

have a look at MenuItemCompat.getActionView always returns null

Community
  • 1
  • 1
Androidme
  • 3,145
  • 3
  • 24
  • 27