Android SDK 22 - SearchView rendering problems

北战南征 提交于 2019-11-26 04:54:25

问题


We\'ve just updated local Android SDK to new version 22, and we are trying to update also our apps, but it seems to be a problem with SearchView rendering in layout Design view of Android Studio: everytime designer try to render a SearchView element, it throws an exception:

java.lang.NullPointerException   at android.content.res.TypedArray.hasValueOrEmpty(TypedArray.java:845)   at android.widget.SearchView.(SearchView.java:295)   at android.widget.SearchView.(SearchView.java:258)   at android.widget.SearchView.(SearchView.java:254)   at java.lang.reflect.Constructor.newInstance(Constructor.java:422)   at android.view.LayoutInflater.createView(LayoutInflater.java:607)   at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)   at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:806)   at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)   at android.view.LayoutInflater.rInflate(LayoutInflater.java:782)   at android.view.LayoutInflater.inflate(LayoutInflater.java:504)   at android.view.LayoutInflater.inflate(LayoutInflater.java:385)

This occours even if we create a new element from left toolbar in a new layout file. Any idea?

\"Rendering

<?xml version=\"1.0\" encoding=\"utf-8\"?>
<LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"
    android:orientation=\"vertical\"
    android:layout_width=\"match_parent\"
    android:layout_height=\"match_parent\">
    <SearchView
        android:layout_width=\"wrap_content\"
        android:layout_height=\"wrap_content\"
        android:id=\"@+id/searchView\" />
</LinearLayout>

回答1:


Edit

Change your android version on your designer preview into your current version depend on your Manifest. rendering problem caused your designer preview used higher API level than your current android API level.


It should work

Just change the api 22 to 21 in android xml layout




回答2:


I voted up Davide comments. Error means searchIcon can not be null (why?).

This works to compile at Studio with api 22, but forces you to select an icon. The example is one of 2 defaults at android (none as cool as searchview default), you can always have your custom one...

Here the snippet.

        <SearchView
        android:id="@+id/searchBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:searchIcon="@android:drawable/ic_search_category_default"
        tools:ignore="UnusedAttribute" />



回答3:


Found this in the source. It's the dereference of a that fails in the evaluation.

    final TypedArray a = context.obtainStyledAttributes(
            attrs, R.styleable.SearchView, defStyleAttr, defStyleRes);

    [...]

    // Prior to L MR1, the search hint icon defaulted to searchIcon. If the
    // style does not have an explicit value set, fall back to that.
    if (a.hasValueOrEmpty(R.styleable.SearchView_searchHintIcon)) {
        mSearchHintIcon = a.getDrawable(R.styleable.SearchView_searchHintIcon);
    } else {
        mSearchHintIcon = a.getDrawable(R.styleable.SearchView_searchIcon);
    }

Try setting the styled attributes?




回答4:


Try changing API level to higher Rendering error will not be there.

It will resolve rendering issues



来源:https://stackoverflow.com/questions/29005958/android-sdk-22-searchview-rendering-problems

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!