Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActionbarSherlock SearchView in menu.xml gives Resources$NotFoundException

When I try to add an ActionbarSherlock SearchView to my ActionBar the application crashes as soon as the activity should be shown. As reason for this, I found the following in LogCat:

10-22 21:22:51.070: W/MenuInflater(21873): Cannot instantiate class: com.actionbarsherlock.widget.SearchView
...
10-22 21:22:51.070: W/MenuInflater(21873): Caused by: java.lang.reflect.InvocationTargetException
10-22 21:22:51.070: W/MenuInflater(21873):  at java.lang.reflect.Constructor.constructNative(Native Method)
10-22 21:22:51.070: W/MenuInflater(21873):  at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
10-22 21:22:51.070: W/MenuInflater(21873):  at android.view.LayoutInflater.createView(LayoutInflater.java:587)
10-22 21:22:51.070: W/MenuInflater(21873):  ... 32 more
10-22 21:22:51.070: W/MenuInflater(21873): Caused by: android.content.res.Resources$NotFoundException: Resource is not a ColorStateList (color or path): TypedValue{t=0x2/d=0x7f01001f a=-1}
10-22 21:22:51.070: W/MenuInflater(21873):  at android.content.res.Resources.loadColorStateList(Resources.java:2035)
10-22 21:22:51.070: W/MenuInflater(21873):  at android.content.res.TypedArray.getColorStateList(TypedArray.java:342)
10-22 21:22:51.070: W/MenuInflater(21873):  at android.widget.TextView.<init>(TextView.java:768)
10-22 21:22:51.070: W/MenuInflater(21873):  at android.widget.TextView.<init>(TextView.java:442)

When I use the regular android.widget.SearchView for the android:actionViewClass everything works fine. I followed the tutorial from the Android website.

This is my menu xml

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

    <item
        android:id="@+id/menu_search"
        android:actionViewClass="com.actionbarsherlock.widget.SearchView"
        android:icon="@drawable/ic_action_search"
        android:showAsAction="ifRoom|collapseActionView"
        android:title="@string/menu_search"/>

    <item android:id="@+id/menu_add_page"
        android:title="@string/menu_add_page"
        android:icon="@drawable/ic_action_add"
        android:showAsAction="always" />

    <item android:id="@+id/menu_settings"
        android:title="@string/menu_settings"
        android:showAsAction="never" />
</menu>

And this is the relevant code:

public boolean onCreateOptionsMenu(Menu menu) {
    getSupportMenuInflater().inflate(R.menu.activity_overview, menu);

    // Get the SearchView and set the searchable configuration
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

    return true;
}

Is this a (known) bug, or am I missing something else here?

like image 764
Ridcully Avatar asked Oct 22 '12 19:10

Ridcully


1 Answers

This issue is caused by the lack of certain attributes in v-14 version of Sherlock Theme.

I fixed it by adding

<item name="textColorPrimary">@color/abs__primary_text_holo_light</item>
<item name="textColorPrimaryInverse">@color/abs__primary_text_holo_dark</item>

to the Sherlock theme and it runs beautifully again. Not sure if ideal solution but for now it works :)

like image 74
RobGThai Avatar answered Nov 11 '22 09:11

RobGThai