Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android SDK 22 - SearchView rendering problems

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 error

<?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>
like image 638
Davide Avatar asked Mar 12 '15 09:03

Davide


3 Answers

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

enter image description here

like image 154
N J Avatar answered Nov 12 '22 04:11

N J


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" />
like image 43
Dieglock Avatar answered Nov 12 '22 04:11

Dieglock


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?

like image 1
mach Avatar answered Nov 12 '22 05:11

mach