menubarSherlock menu.xml中的SearchView给出了Resources $ NotFoundException

时间:2012-10-22 19:54:15

标签: android actionbarsherlock

当我尝试将ActionbarSherlock SearchView添加到ActionBar时,应用程序会在应显示活动时立即崩溃。作为理由,我在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)

当我对android.widget.SearchView使用常规android:actionViewClass时,一切正常。 我关注了Android网站上的tutorial

这是我的菜单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>

这是相关的代码:

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

这是一个(已知的)错误,还是我在这里遗漏了其他东西?

5 个答案:

答案 0 :(得分:3)

此问题是由于v-14版本的Sherlock Theme缺少某些属性引起的。

我通过添加

修复了它
<item name="textColorPrimary">@color/abs__primary_text_holo_light</item>
<item name="textColorPrimaryInverse">@color/abs__primary_text_holo_dark</item>
对于Sherlock主题而言,它再次运行得很漂亮。不确定是否理想的解决方案,但现在它的工作原理:)

答案 1 :(得分:2)

SearchView中的

ActionbarSherlock是一个已知问题。 dev分支包含支持,但它不完整,似乎仍然存在建议问题。

请参阅github herehere以供参考。

以下是指向ActionBarSherlock&gt;的链接SearchView Source

答案 2 :(得分:1)

正如 hahnjo 提到here

  

如果您使用主题Theme.Sherlock.Light.DarkActionBar并使用new SearchView(this)创建视图,则无效...   可行的组合:   Theme.Sherlock.Light.DarkActionBar new SearchView(getSupportActionBar().getThemedContext()) Theme.Sherlock.Light   Theme.Sherlock.Lightnew SearchView(this) {{1}}

答案 3 :(得分:0)

我也收到了此错误,但这是因为我没有在android:theme的{​​{1}}节点上设置<application>

AndroidManifest.xml节点上设置android:theme="@style/Theme.Sherlock"后,问题就解决了。

答案 4 :(得分:0)

我发现我必须执行以下所有操作才能使其正常工作:

确保在AndroidManifest.xml中设置了应用程序主题:

<application android:name="com.xxx.MyApplication" android:theme="@style/AppTheme">

我在基本样式中添加了“缺失”设置:

<style>
    <item name="textColorPrimary">@color/abs__primary_text_holo_light</item>
    <item name="textColorPrimaryInverse">@color/abs__primary_text_holo_dark</item>
    <item name="searchDropdownBackground">@drawable/abs__search_dropdown_dark</item>
    <item name="searchViewTextField">@drawable/abs__textfield_searchview_holo_dark</item>
    <item name="searchViewTextFieldRight">@drawable/abs__textfield_searchview_right_holo_dark</item>
    <item name="searchViewCloseIcon">@drawable/abs__ic_clear</item>
    <item name="searchViewSearchIcon">@drawable/abs__ic_search</item>
    <item name="searchViewGoIcon">@drawable/abs__ic_go</item>
    <item name="searchViewVoiceIcon">@drawable/abs__ic_voice_search</item>
    <item name="searchAutoCompleteTextView">@style/Widget.Sherlock.SearchAutoCompleteTextView</item>
 </style>

然后更新我的Activity.onCreateOptionsMenu()以将“主题上下文”传递给菜单inflater,如:

@Override
public boolean onCreateOptionsMenu(final Menu menu)
{
    final MenuInflater inflater = new MenuInflater(this.getSupportActionBar().getThemedContext());
    inflater.inflate(R.menu.main_menu, menu);
    this.initSearchView(menu);
    return true;
}

然后将相同的主题上下文传递给SearchView创建/初始化:

private void initSearchView(final Menu menu)
{
    final SearchManager searchManager = (SearchManager) this.getSystemService(Context.SEARCH_SERVICE);
    final MenuItem searchViewMenuItem = menu.findItem(R.id.menu_search);

    final SearchView searchView = new SearchView(this.getSupportActionBar().getThemedContext());
    searchView.setSearchableInfo(searchManager.getSearchableInfo(this.getComponentName()));
    searchView.setIconifiedByDefault(false);
    searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener()
    {
        @Override
        public void onFocusChange(final View v, final boolean hasFocus)
        {
            if (!hasFocus)
            {
                // Collapse the searchbox on ActionBar
                searchViewMenuItem.collapseActionView();
            }
        }
    });
}

我希望这可以帮助某人或它得到“修复”,这样才有效。