質問

I'm using ActionBarSherlock library in my app. I followed this Tutorial but it is not working, app crashes as soon as it starts due to OnCreateOptionsMenu(). I can get what is the error. Please help

03-11 14:29:16.037: W/MenuInflater(10097): Cannot instantiate class: com.actionbarsherlock.widget.SearchView
03-11 14:29:16.037: W/MenuInflater(10097): java.lang.reflect.InvocationTargetException
03-11 14:29:16.037: W/MenuInflater(10097):  at java.lang.reflect.Constructor.constructNative(Native Method)
03-11 14:29:16.037: W/MenuInflater(10097):  at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
03-11 14:29:16.037: W/MenuInflater(10097):  at com.actionbarsherlock.view.MenuInflater$MenuState.newInstance(MenuInflater.java:488)
03-11 14:29:16.037: W/MenuInflater(10097):  at com.actionbarsherlock.view.MenuInflater$MenuState.setItem(MenuInflater.java:447)
03-11 14:29:16.037: W/MenuInflater(10097):  at com.actionbarsherlock.view.MenuInflater$MenuState.addItem(MenuInflater.java:468)
03-11 14:29:16.037: W/MenuInflater(10097):  at com.actionbarsherlock.view.MenuInflater.parseMenu(MenuInflater.java:190)
03-11 14:29:16.037: W/MenuInflater(10097):  at com.actionbarsherlock.view.MenuInflater.inflate(MenuInflater.java:112)
03-11 14:29:16.037: W/MenuInflater(10097):  at com.earth.map.location.MainActivity.onCreateOptionsMenu(MainActivity.java:71)
03-11 14:29:16.037: W/MenuInflater(10097):  at android.support.v4.app.Watson.onCreatePanelMenu(Watson.java:44)
03-11 14:29:16.037: W/MenuInflater(10097):  at com.actionbarsherlock.ActionBarSherlock.callbackCreateOptionsMenu(ActionBarSherlock.java:560)
03-11 14:29:16.037: W/MenuInflater(10097):  at com.actionbarsherlock.internal.ActionBarSherlockNative.dispatchCreateOptionsMenu(ActionBarSherlockNative.java:64)
03-11 14:29:16.037: W/MenuInflater(10097):  at com.actionbarsherlock.app.SherlockFragmentActivity.onCreatePanelMenu(SherlockFragmentActivity.java:164)
03-11 14:29:16.037: W/MenuInflater(10097):  at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:397)
03-11 14:29:16.037: W/MenuInflater(10097):  at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:759)
03-11 14:29:16.037: W/MenuInflater(10097):  at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:2992)
03-11 14:29:16.037: W/MenuInflater(10097):  at android.os.Handler.handleCallback(Handler.java:615)
03-11 14:29:16.037: W/MenuInflater(10097):  at android.os.Handler.dispatchMessage(Handler.java:92)
03-11 14:29:16.037: W/MenuInflater(10097):  at android.os.Looper.loop(Looper.java:137)
03-11 14:29:16.037: W/MenuInflater(10097):  at android.app.ActivityThread.main(ActivityThread.java:5059)
03-11 14:29:16.037: W/MenuInflater(10097):  at java.lang.reflect.Method.invokeNative(Native Method)
03-11 14:29:16.037: W/MenuInflater(10097):  at java.lang.reflect.Method.invoke(Method.java:511)
03-11 14:29:16.037: W/MenuInflater(10097):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
03-11 14:29:16.037: W/MenuInflater(10097):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
03-11 14:29:16.037: W/MenuInflater(10097):  at dalvik.system.NativeStart.main(Native Method)
03-11 14:29:16.037: W/MenuInflater(10097): Caused by: android.view.InflateException: Binary XML file line #29: Error inflating class <unknown>
03-11 14:29:16.037: W/MenuInflater(10097):  at android.view.LayoutInflater.createView(LayoutInflater.java:613)
03-11 14:29:16.037: W/MenuInflater(10097):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
03-11 14:29:16.037: W/MenuInflater(10097):  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
03-11 14:29:16.037: W/MenuInflater(10097):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
03-11 14:29:16.037: W/MenuInflater(10097):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
03-11 14:29:16.037: W/MenuInflater(10097):  at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
03-11 14:29:16.037: W/MenuInflater(10097):  at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
03-11 14:29:16.037: W/MenuInflater(10097):  at com.actionbarsherlock.widget.SearchView.<init>(SearchView.java:264)
03-11 14:29:16.037: W/MenuInflater(10097):  at com.actionbarsherlock.widget.SearchView.<init>(SearchView.java:252)
03-11 14:29:16.037: W/MenuInflater(10097):  ... 24 more
03-11 14:29:16.037: W/MenuInflater(10097): Caused by: java.lang.reflect.InvocationTargetException
03-11 14:29:16.037: W/MenuInflater(10097):  at java.lang.reflect.Constructor.constructNative(Native Method)
03-11 14:29:16.037: W/MenuInflater(10097):  at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
03-11 14:29:16.037: W/MenuInflater(10097):  at android.view.LayoutInflater.createView(LayoutInflater.java:587)
03-11 14:29:16.037: W/MenuInflater(10097):  ... 32 more
03-11 14:29:16.037: W/MenuInflater(10097): Caused by: android.content.res.Resources$NotFoundException: Resource is not a ColorStateList (color or path): TypedValue{t=0x2/d=0x7f010030 a=-1}
03-11 14:29:16.037: W/MenuInflater(10097):  at android.content.res.Resources.loadColorStateList(Resources.java:2053)
03-11 14:29:16.037: W/MenuInflater(10097):  at android.content.res.TypedArray.getColorStateList(TypedArray.java:342)
03-11 14:29:16.037: W/MenuInflater(10097):  at android.widget.TextView.<init>(TextView.java:803)
03-11 14:29:16.037: W/MenuInflater(10097):  at android.widget.TextView.<init>(TextView.java:477)
03-11 14:29:16.037: W/MenuInflater(10097):  ... 35 more
03-11 14:29:16.045: D/AndroidRuntime(10097): Shutting down VM
03-11 14:29:16.045: W/dalvikvm(10097): threadid=1: thread exiting with uncaught exception (group=0x418ab2a0)
03-11 14:29:16.045: E/AndroidRuntime(10097): FATAL EXCEPTION: main
03-11 14:29:16.045: E/AndroidRuntime(10097): java.lang.NullPointerException
03-11 14:29:16.045: E/AndroidRuntime(10097):    at com.earth.map.location.MainActivity.onCreateOptionsMenu(MainActivity.java:76)
03-11 14:29:16.045: E/AndroidRuntime(10097):    at android.support.v4.app.Watson.onCreatePanelMenu(Watson.java:44)
03-11 14:29:16.045: E/AndroidRuntime(10097):    at com.actionbarsherlock.ActionBarSherlock.callbackCreateOptionsMenu(ActionBarSherlock.java:560)
03-11 14:29:16.045: E/AndroidRuntime(10097):    at com.actionbarsherlock.internal.ActionBarSherlockNative.dispatchCreateOptionsMenu(ActionBarSherlockNative.java:64)
03-11 14:29:16.045: E/AndroidRuntime(10097):    at com.actionbarsherlock.app.SherlockFragmentActivity.onCreatePanelMenu(SherlockFragmentActivity.java:164)
03-11 14:29:16.045: E/AndroidRuntime(10097):    at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:397)
03-11 14:29:16.045: E/AndroidRuntime(10097):    at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:759)
03-11 14:29:16.045: E/AndroidRuntime(10097):    at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:2992)
03-11 14:29:16.045: E/AndroidRuntime(10097):    at android.os.Handler.handleCallback(Handler.java:615)
03-11 14:29:16.045: E/AndroidRuntime(10097):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-11 14:29:16.045: E/AndroidRuntime(10097):    at android.os.Looper.loop(Looper.java:137)
03-11 14:29:16.045: E/AndroidRuntime(10097):    at android.app.ActivityThread.main(ActivityThread.java:5059)
03-11 14:29:16.045: E/AndroidRuntime(10097):    at java.lang.reflect.Method.invokeNative(Native Method)
03-11 14:29:16.045: E/AndroidRuntime(10097):    at java.lang.reflect.Method.invoke(Method.java:511)
03-11 14:29:16.045: E/AndroidRuntime(10097):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
03-11 14:29:16.045: E/AndroidRuntime(10097):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
03-11 14:29:16.045: E/AndroidRuntime(10097):    at dalvik.system.NativeStart.main(Native Method)

MainActivity.java

public class MainActivity extends SherlockFragmentActivity implements LoaderCallbacks<Cursor> {
....
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    MenuInflater inflater = getSupportMenuInflater();
    inflater.inflate(R.menu.main, menu);
    // Get the SearchView and set the searchable configuration
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.action_search)
            .getActionView();
    searchView.setSearchableInfo(searchManager
            .getSearchableInfo(getComponentName()));
    return true;
}
役に立ちましたか?

解決

One thing which I noticed while ago when using ActionBarSherlock is that to use it correctly and without any errors, you should use one of the themes from this library as your parent in all folders (values, values-v11, values-v14). So I guess in your values-v14/styles or values-v14/themes, depends on where is your main custom theme which you are using for your application, instead of using Theme.Holo as your parent, change it to use one of the themes from ActionBarSherlock :

  • Theme.Sherlock
  • Theme.Sherlock.Light
  • Theme.Sherlock.Light.DarkActionBar
  • Theme.Sherlock.NoActionBar
  • Theme.Sherlock.Light.NoActionBar

That should fix your problem.

他のヒント

Why your are using action bar sherlock? You can directly use actiobar activity.

http://developer.android.com/reference/android/support/v7/app/ActionBarActivity.html

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top