0

For whatever reason, my ActionBar crashes when I click on one of its buttons. Here's the code for the optionsSelectMenu:

public boolean onOptionsItemSelected(MenuItem item)
{   
    switch (item.getItemId())
    {
    case R.id.refresh_button_actionbar:
        if (this.frag instanceof DashboardFragment)
            dashboardFrag.refresh();
        break;
    default: 
        return false;
    }
    return true;
}

And here is the logcat:

07-30 19:17:52.981: E/AndroidRuntime(1358): FATAL EXCEPTION: main
07-30 19:17:52.981: E/AndroidRuntime(1358): java.lang.NullPointerException
07-30 19:17:52.981: E/AndroidRuntime(1358):     at com.android.internal.view.menu.MenuItemImpl.toString(MenuItemImpl.java:496)
07-30 19:17:52.981: E/AndroidRuntime(1358):     at java.lang.StringBuilder.append(StringBuilder.java:202)
07-30 19:17:52.981: E/AndroidRuntime(1358):     at com.actionbarsherlock.app.SherlockFragmentActivity.onMenuItemSelected(SherlockFragmentActivity.java:201)
07-30 19:17:52.981: E/AndroidRuntime(1358):     at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:980)
07-30 19:17:52.981: E/AndroidRuntime(1358):     at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
07-30 19:17:52.981: E/AndroidRuntime(1358):     at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
07-30 19:17:52.981: E/AndroidRuntime(1358):     at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
07-30 19:17:52.981: E/AndroidRuntime(1358):     at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:547)
07-30 19:17:52.981: E/AndroidRuntime(1358):     at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:115)
07-30 19:17:52.981: E/AndroidRuntime(1358):     at android.view.View.performClick(View.java:4204)
07-30 19:17:52.981: E/AndroidRuntime(1358):     at android.view.View$PerformClick.run(View.java:17355)
07-30 19:17:52.981: E/AndroidRuntime(1358):     at android.os.Handler.handleCallback(Handler.java:725)
07-30 19:17:52.981: E/AndroidRuntime(1358):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-30 19:17:52.981: E/AndroidRuntime(1358):     at android.os.Looper.loop(Looper.java:137)
07-30 19:17:52.981: E/AndroidRuntime(1358):     at android.app.ActivityThread.main(ActivityThread.java:5041)
07-30 19:17:52.981: E/AndroidRuntime(1358):     at java.lang.reflect.Method.invokeNative(Native Method)
07-30 19:17:52.981: E/AndroidRuntime(1358):     at java.lang.reflect.Method.invoke(Method.java:511)
07-30 19:17:52.981: E/AndroidRuntime(1358):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-30 19:17:52.981: E/AndroidRuntime(1358):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-30 19:17:52.981: E/AndroidRuntime(1358):     at dalvik.system.NativeStart.main(Native Method)

Any ideas of what I'm doing wrong? I'm thinking it has to do with the fragment comparison. Also, to note, when I just set the code to "return true" (as in it will do nothing but return true), it will re-start the activity. Is this intended behavior? If so, how can I get around it?

Here are my imports:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import com.actifio.android.R;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.ActionBar.TabListener;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
Aneesh Ashutosh
  • 762
  • 7
  • 18

1 Answers1

1

You are using Android's Menu and MenuInflater, but should be using the classes that come with ActionBarSherlock. Check if this is your import for them:

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;

Check the answer given in this thread for a detailed explanation.Your problem should be solved. Next time make sure if this type of question is asked already.

EDIT:

I came up with a tweak(not very clean but works) to solve your problem. Hope you have the actionbarsharlock project in your workspace. Go to the package com.actionbarsharlock.app then locate the file SharlockFragmentActivity.java,open it up and go to line 201 (CMD/CTRL+L then enter the line number) now comment out the following lines:

try
        {
            if (BuildConfig.DEBUG) Log.d(TAG, "[onMenuItemSelected] featureId: " + featureId + ", item: " + item);
        }catch(Exception e)
        {
            Log.d(TAG,"Exception");
        }

Your problem should be fixed.

Community
  • 1
  • 1
Munim
  • 2,626
  • 1
  • 19
  • 28
  • The thing is, the imports I'm using are from Action Bar Sherlock. They match those exactly. – Aneesh Ashutosh Jul 30 '13 at 20:46
  • I don't get this then. Take a look at the log cat output.`java.lang.NullPointerException 07-30 19:17:52.981: E/AndroidRuntime(1358): at com.android.internal.view.menu.MenuItemImpl.toString(MenuItemImpl.java:496)` If you have used sharlock imports then the error should be from `com.actionbarsharlock.internal.view.menu.MenuItemImpl` class. Please recheck your code. I am afraid you have imported both but using the android one. – Munim Jul 31 '13 at 20:43
  • Added my imports to the main thread. Those are the only ones I'm using :/ Not using any of the default Android ones. I'm instantiating the Action Bar with getSupportActionBar(), and the inflater with getSupportMenuInflater(). – Aneesh Ashutosh Aug 01 '13 at 12:36