2013-10-22 54 views
1

這是我menu.xml文件無法實例類:com.actionbarsherlock.widget.SearchView

<menu xmlns:android="http://schemas.android.com/apk/res/android" > 

    <item 
     android:id="@+id/menu_search" 
     android:actionViewClass="com.actionbarsherlock.widget.SearchView" 
     android:showAsAction="always|withText" 
     android:title="Search"/> 

</menu> 

我試圖誇大這一menu.xml

MainActivity.java

@Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     super.onCreateOptionsMenu(menu); 
     MenuInflater inflater=new MenuInflater(getBaseContext()); 
     inflater.inflate(R.menu.menu, menu); 

     return true; 
    } 

但logcat拋出錯誤

10-22 20:03:24.170: W/ResourceType(25679): No package identifier when getting value for resource number 0x00000000 
10-22 20:03:24.170: W/MenuInflater(25679): Cannot instantiate class: com.actionbarsherlock.widget.SearchView 
10-22 20:03:24.170: W/MenuInflater(25679): java.lang.reflect.InvocationTargetException 
10-22 20:03:24.170: W/MenuInflater(25679): at java.lang.reflect.Constructor.constructNative(Native Method) 
10-22 20:03:24.170: W/MenuInflater(25679): at java.lang.reflect.Constructor.newInstance(Constructor.java:417) 
10-22 20:03:24.170: W/MenuInflater(25679): at com.actionbarsherlock.view.MenuInflater$MenuState.newInstance(MenuInflater.java:488) 
10-22 20:03:24.170: W/MenuInflater(25679): at com.actionbarsherlock.view.MenuInflater$MenuState.setItem(MenuInflater.java:447) 
10-22 20:03:24.170: W/MenuInflater(25679): at com.actionbarsherlock.view.MenuInflater$MenuState.addItem(MenuInflater.java:468) 
10-22 20:03:24.170: W/MenuInflater(25679): at com.actionbarsherlock.view.MenuInflater.parseMenu(MenuInflater.java:190) 
10-22 20:03:24.170: W/MenuInflater(25679): at com.actionbarsherlock.view.MenuInflater.inflate(MenuInflater.java:112) 
10-22 20:03:24.170: W/MenuInflater(25679): at com.example.sk.MainActivity.onCreateOptionsMenu(MainActivity.java:211) 
10-22 20:03:24.170: W/MenuInflater(25679): at android.support.v4.app.Watson.onCreatePanelMenu(Watson.java:45) 
10-22 20:03:24.170: W/MenuInflater(25679): at com.actionbarsherlock.ActionBarSherlock.callbackCreateOptionsMenu(ActionBarSherlock.java:559) 
10-22 20:03:24.170: W/MenuInflater(25679): at com.actionbarsherlock.internal.ActionBarSherlockNative.dispatchCreateOptionsMenu(ActionBarSherlockNative.java:60) 
10-22 20:03:24.170: W/MenuInflater(25679): at com.actionbarsherlock.app.SherlockFragmentActivity.onCreatePanelMenu(SherlockFragmentActivity.java:165) 
10-22 20:03:24.170: W/MenuInflater(25679): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:453) 
10-22 20:03:24.170: W/MenuInflater(25679): at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:833) 
10-22 20:03:24.170: W/MenuInflater(25679): at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:3290) 
10-22 20:03:24.170: W/MenuInflater(25679): at android.os.Handler.handleCallback(Handler.java:615) 
10-22 20:03:24.170: W/MenuInflater(25679): at android.os.Handler.dispatchMessage(Handler.java:92) 
10-22 20:03:24.170: W/MenuInflater(25679): at android.os.Looper.loop(Looper.java:137) 
10-22 20:03:24.170: W/MenuInflater(25679): at android.app.ActivityThread.main(ActivityThread.java:4921) 
10-22 20:03:24.170: W/MenuInflater(25679): at java.lang.reflect.Method.invokeNative(Native Method) 
10-22 20:03:24.170: W/MenuInflater(25679): at java.lang.reflect.Method.invoke(Method.java:511) 
10-22 20:03:24.170: W/MenuInflater(25679): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038) 
10-22 20:03:24.170: W/MenuInflater(25679): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805) 
10-22 20:03:24.170: W/MenuInflater(25679): at dalvik.system.NativeStart.main(Native Method) 
10-22 20:03:24.170: W/MenuInflater(25679): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0 
10-22 20:03:24.170: W/MenuInflater(25679): at android.content.res.Resources.getValue(Resources.java:1339) 
10-22 20:03:24.170: W/MenuInflater(25679): at android.content.res.Resources.getDrawable(Resources.java:976) 
10-22 20:03:24.170: W/MenuInflater(25679): at com.actionbarsherlock.widget.SearchView.getDecoratedHint(SearchView.java:1076) 
10-22 20:03:24.170: W/MenuInflater(25679): at com.actionbarsherlock.widget.SearchView.updateQueryHint(SearchView.java:1096) 
10-22 20:03:24.170: W/MenuInflater(25679): at com.actionbarsherlock.widget.SearchView.setIconifiedByDefault(SearchView.java:595) 
10-22 20:03:24.170: W/MenuInflater(25679): at com.actionbarsherlock.widget.SearchView.<init>(SearchView.java:300) 
10-22 20:03:24.170: W/MenuInflater(25679): at com.actionbarsherlock.widget.SearchView.<init>(SearchView.java:252) 
10-22 20:03:24.170: W/MenuInflater(25679): ... 24 more 

我也保留主題爲android:theme="@style/Theme.Sherlock.Light"

我該如何解決這個錯誤?

回答

0

添加代碼的評論部分解決了問題不完全,但它很好。

@Override 
     public boolean onCreateOptionsMenu(Menu menu) { 
      super.onCreateOptionsMenu(menu); 
      MenuInflater inflater=new MenuInflater(getBaseContext()); 
      inflater.inflate(R.menu.menu, menu); 
      // adding this section of code resolved the problem 
      SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); 
      com.actionbarsherlock.widget.SearchView searchView = new com.actionbarsherlock.widget.SearchView(getSupportActionBar().getThemedContext()); 
      searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); 
      menu.findItem(R.id.menu_search).setActionView(searchView); 
      return true; 
     } 

但它顯示在logcat中相同的警告,因爲我張貼在的問題,我發現這是/是衆所周知的問題ActionBarSherlock更多細節click here