Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Null pointer exception showing in action bar sherlock library

I want to implement the search view widget of Sherlock action bar. My problem is OnOptionItemSelected of search view item. I am getting a null pointer exception in library activity.

The menu xml:

<item android:id="@+id/menu_search"
      android:actionViewClass="com.actionbarsherlock.widget.SearchView"
      android:icon="@android:drawable/ic_menu_search"
/>

Code snippet for option menu:

public boolean onCreateOptionsMenu(Menu menu) 
{
    MenuInflater inflater = getSupportMenuInflater();
    inflater.inflate(R.menu.tvguide_menu, menu);
    MenuItem mSearch = menu.findItem(R.id.menu_search);
    searchView = new SearchView(getSupportActionBar().getThemedContext());
    searchView.setQueryHint("Search for countries…");
    searchView.setOnQueryTextListener(this);
    mSearch.setActionView(searchView);
    mSearch.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM |
            MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
}

I am getting a forced close on clicking over search android. I have shared my log below

04-17 15:10:08.508: E/AndroidRuntime(23283): FATAL EXCEPTION: main
04-17 15:10:08.508: E/AndroidRuntime(23283): java.lang.NullPointerException
04-17 15:10:08.508: E/AndroidRuntime(23283):    at com.android.internal.view.menu.MenuItemImpl.toString(MenuItemImpl.java:490)
04-17 15:10:08.508: E/AndroidRuntime(23283):    at java.lang.StringBuilder.append(StringBuilder.java:202)
04-17 15:10:08.508: E/AndroidRuntime(23283):    at com.actionbarsherlock.app.SherlockFragmentActivity.onMenuItemSelected(SherlockFragmentActivity.java:201)
04-17 15:10:08.508: E/AndroidRuntime(23283):    at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:953)
04-17 15:10:08.508: E/AndroidRuntime(23283):    at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
04-17 15:10:08.508: E/AndroidRuntime(23283):    at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
04-17 15:10:08.508: E/AndroidRuntime(23283):    at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
04-17 15:10:08.508: E/AndroidRuntime(23283):    at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:491)
04-17 15:10:08.508: E/AndroidRuntime(23283):    at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:110)
04-17 15:10:08.508: E/AndroidRuntime(23283):    at android.view.View.performClick(View.java:3511)
04-17 15:10:08.508: E/AndroidRuntime(23283):    at android.view.View$PerformClick.run(View.java:14105)
04-17 15:10:08.508: E/AndroidRuntime(23283):    at android.os.Handler.handleCallback(Handler.java:605)
04-17 15:10:08.508: E/AndroidRuntime(23283):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-17 15:10:08.508: E/AndroidRuntime(23283):    at android.os.Looper.loop(Looper.java:137)
04-17 15:10:08.508: E/AndroidRuntime(23283):    at android.app.ActivityThread.main(ActivityThread.java:4438)
04-17 15:10:08.508: E/AndroidRuntime(23283):    at java.lang.reflect.Method.invokeNative(Native Method)
04-17 15:10:08.508: E/AndroidRuntime(23283):    at java.lang.reflect.Method.invoke(Method.java:511)
04-17 15:10:08.508: E/AndroidRuntime(23283):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-17 15:10:08.508: E/AndroidRuntime(23283):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-17 15:10:08.508: E/AndroidRuntime(23283):    at dalvik.system.NativeStart.main(Native Method)
like image 320
user1526671 Avatar asked Dec 09 '22 16:12

user1526671


2 Answers

The problem is that your Menu item does not specify an title, and in the line :

@Override
public final boolean onMenuItemSelected(int featureId, android.view.MenuItem item) {
    if (BuildConfig.DEBUG) Log.d(TAG, "[onMenuItemSelected] featureId: " 
           + featureId + ", item: " + item);
    ...
}

item will inkove the toString() method which :

@Override
public String toString() {
    return mTitle.toString();
}

will throw a NullPointerException. A easy workaround for this, until it will be fixed is to use :

android:title=""

for your MenuItem.

Also, this will not happen on a release of your application.

like image 69
Ovidiu Latcu Avatar answered Dec 11 '22 05:12

Ovidiu Latcu


It is a bug in version 4.3.0 of ActionBarSherlock, I fixed it in this way (you need to comment the line 201 of actionbarsherlock/src/com/actionbarsherlock/app/SherlockFragmentActivity.java :

--- a/actionbarsherlock/src/com/actionbarsherlock/app/SherlockFragmentActivity.java
+++ b/actionbarsherlock/src/com/actionbarsherlock/app/SherlockFragmentActivity.java
@@ -198,7 +198,7 @@ public class SherlockFragmentActivity extends Watson implements OnActionModeStar

     @Override
     public final boolean onMenuItemSelected(int featureId, android.view.MenuItem item) {
-        if (BuildConfig.DEBUG) Log.d(TAG, "[onMenuItemSelected] featureId: " + featureId + ", item: " + item);
+       // if (BuildConfig.DEBUG) Log.d(TAG, "[onMenuItemSelected] featureId: " + featureId + ", item: " + item);

         if (featureId == Window.FEATURE_OPTIONS_PANEL && !mIgnoreNativeSelected) {
             mIgnoreNativeSelected = true;
like image 30
Giuseppe Avatar answered Dec 11 '22 05:12

Giuseppe