带有选项菜单的sherlock操作栏中的空指针异常

时间:2013-04-17 06:24:51

标签: actionbarsherlock searchview

我想要sherlock操作栏和默认的sherlock搜索小部件。搜索图标显示在sherlock操作栏上,但是点击它强行关闭。我的Logcat如下所示

04-17 11:44:48.877:E / AndroidRuntime(530):致命异常:主要 04-17 11:44:48.877:E / AndroidRuntime(530):java.lang.NullPointerException 04-17 11:44:48.877:E / AndroidRuntime(530):at com.android.internal.view.menu.MenuItemImpl.toString(MenuItemImpl.java:490) 04-17 11:44:48.877:E / AndroidRuntime(530):at java.lang.StringBuilder.append(StringBuilder.java:202) 04-17 11:44:48.877:E / AndroidRuntime(530):at com.actionbarsherlock.app.SherlockFragmentActivity.onMenuItemSelected(SherlockFragmentActivity.java:201) 04-17 11:44:48.877:E / AndroidRuntime(530):at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:950) 04-17 11:44:48.877:E / AndroidRuntime(530):at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735) 04-17 11:44:48.877:E / AndroidRuntime(530):at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149) 04-17 11:44:48.877:E / AndroidRuntime(530):at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874) 04-17 11:44:48.877:E / AndroidRuntime(530):at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:490) 04-17 11:44:48.877:E / AndroidRuntime(530):at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:108) 04-17 11:44:48.877:E / AndroidRuntime(530):在android.view.View.performClick(View.java:3511) 04-17 11:44:48.877:E / AndroidRuntime(530):在android.view.View $ PerformClick.run(View.java:14105) 04-17 11:44:48.877:E / AndroidRuntime(530):在android.os.Handler.handleCallback(Handler.java:605) 04-17 11:44:48.877:E / AndroidRuntime(530):在android.os.Handler.dispatchMessage(Handler.java:92) 04-17 11:44:48.877:E / AndroidRuntime(530):在android.os.Looper.loop(Looper.java:137) 04-17 11:44:48.877:E / AndroidRuntime(530):在android.app.ActivityThread.main(ActivityThread.java:4424) 04-17 11:44:48.877:E / AndroidRuntime(530):at java.lang.reflect.Method.invokeNative(Native Method) 04-17 11:44:48.877:E / AndroidRuntime(530):at java.lang.reflect.Method.invoke(Method.java:511) 04-17 11:44:48.877:E / AndroidRuntime(530):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784) 04-17 11:44:48.877:E / AndroidRuntime(530):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 04-17 11:44:48.877:E / AndroidRuntime(530):at dalvik.system.NativeStart.main(Native Method)

menu.xml

  <?xml version="1.0" encoding="utf-8"?>
  <menu xmlns:android="http://schemas.android.com/apk/res/android" >

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

sherlock活动中的代码

  @Override
public boolean onCreateOptionsMenu(Menu menu) 
{
    MenuInflater inflater = getSupportMenuInflater();
            inflater.inflate(R.menu.tvguide_menu, menu);
     }
  public boolean onOptionsItemSelected(MenuItem item) 
{
      Log.e("", "itemselected" +item.getItemId());
       switch(item.getItemId()) 
        {
            case R.id.menu_search:
                Toast.makeText(TVBuddyMainActivity.this,"search selected", Toast.LENGTH_LONG).show();
                Intent intent = new Intent(TVBuddyMainActivity.this,CustomSearch.class);
                startActivity(intent);
                break;
        }
     return true;
}

请帮我这个logcat ....

1 个答案:

答案 0 :(得分:0)

这是ActionBarSherlock版本4.3.0中的一个错误,我用这种方式修复了它:

--- 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;