4

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)
4

3 回答 3

8

问题是您的Menu项目没有指定标题,并且在行中:

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

item将采用以下toString()方法:

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

会抛出一个NullPointerException. 在修复之前,一个简单的解决方法是使用:

android:title=""

为您的MenuItem.

此外,这不会在您的应用程序发布时发生。

于 2013-05-14T11:43:25.403 回答
6

这是ActionBarSherlock 4.3.0版本的一个bug,我是这样修复的(需要注释actionbarsherlock/src/com/actionbarsherlock/app/SherlockFragmentActivity.java的第201行:

--- 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;
于 2013-04-22T16:58:09.663 回答
0

如果有人遇到此问题并想创建没有标题的菜单项,则可以通过简单地修改 actionbarsherlock 源代码中的类中的toString()方法,com/android/internal/view/menu/MenuItemImpl在返回前进行​​空字符串检查来解决此 nullpointerexception:

@Override
public String toString() {
    return (mTitle != null)? mTitle.toString(): "";
}
于 2013-05-26T09:45:15.790 回答