Cannot instantiate class android.support.v7.widget.SearchView

jgnt32 picture jgnt32 · Mar 22, 2014 · Viewed 8.8k times · Source

Please somebody help me! I trying add appcompat search view to my project. Unsuccessfully. When I change android.support.v7.widget.SearchView to SearchView (API > 10) everything's fine.

This my menu res:

<item android:id="@+id/action_search"
    android:title="@string/search_view_title"
    android:icon="@drawable/action_search"
    sabd:showAsAction="always|collapseActionView"
    sabd:actionViewClass="android.support.v7.widget.SearchView" />

This how I try to use SeachView:

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.teach, menu);
        mSearchItem = menu.findItem(R.id.action_search);
        mSearchItem.setVisible(true);
        mSearchView = (SearchView) MenuItemCompat.getActionView(mSearchItem);
        mSearchView.setQueryHint(getString(R.string.search_view_hint));
        mSearchView.setOnQueryTextListener(this);
    }

This my manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="ru.rgups.time"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.Base.AppCompat.Light"
        android:name="RTApplication">
        <activity
            android:name="ru.rgups.time.MainActivity"
            android:configChanges="orientation|keyboardHidden"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".fragments.WelcomeActivity"></activity>
        <service android:name=".spice.SampleSpiceService"></service>
        <activity android:name=".activities.AuthActivity"></activity>
        <activity 
            android:name=".activities.HomeWorkActivity"
            android:uiOptions="splitActionBarWhenNarrow">

        </activity>

        <receiver android:name="ru.rgups.time.receiver.HomeWorkNotificationReceiver">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>
        </receiver>

    </application>

</manifest>

And this what I see in logs:

   03-22 21:46:58.583: W/SupportMenuInflater(28798): Cannot instantiate class: android.support.v7.widget.SearchView
    03-22 21:46:58.583: W/SupportMenuInflater(28798): java.lang.reflect.InvocationTargetException
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at java.lang.reflect.Constructor.constructNative(Native Method)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.support.v7.internal.view.SupportMenuInflater$MenuState.newInstance(SupportMenuInflater.java:482)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.support.v7.internal.view.SupportMenuInflater$MenuState.setItem(SupportMenuInflater.java:441)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.support.v7.internal.view.SupportMenuInflater$MenuState.addItem(SupportMenuInflater.java:462)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.support.v7.internal.view.SupportMenuInflater.parseMenu(SupportMenuInflater.java:196)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.support.v7.internal.view.SupportMenuInflater.inflate(SupportMenuInflater.java:118)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at ru.rgups.time.fragments.TeachersListFragment.onCreateOptionsMenu(TeachersListFragment.java:109)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.support.v4.app.Fragment.performCreateOptionsMenu(Fragment.java:1582)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.support.v4.app.FragmentManagerImpl.dispatchCreateOptionsMenu(FragmentManager.java:1956)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:225)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:232)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.support.v7.app.ActionBarActivityDelegateICS.onCreatePanelMenu(ActionBarActivityDelegateICS.java:147)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.support.v7.app.ActionBarActivity.onCreatePanelMenu(ActionBarActivity.java:199)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.onCreatePanelMenu(ActionBarActivityDelegateICS.java:285)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:436)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:800)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:221)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.view.Choreographer.doCallbacks(Choreographer.java:574)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.view.Choreographer.doFrame(Choreographer.java:543)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.os.Handler.handleCallback(Handler.java:733)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.os.Handler.dispatchMessage(Handler.java:95)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.os.Looper.loop(Looper.java:136)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.app.ActivityThread.main(ActivityThread.java:5017)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at java.lang.reflect.Method.invokeNative(Native Method)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at java.lang.reflect.Method.invoke(Method.java:515)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at dalvik.system.NativeStart.main(Native Method)
    03-22 21:46:58.583: W/SupportMenuInflater(28798): Caused by: android.view.InflateException: Binary XML file line #41: Error inflating class <unknown>
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.view.LayoutInflater.createView(LayoutInflater.java:620)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.support.v7.widget.SearchView.<init>(SearchView.java:262)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.support.v7.widget.SearchView.<init>(SearchView.java:254)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  ... 31 more
    03-22 21:46:58.583: W/SupportMenuInflater(28798): Caused by: java.lang.reflect.InvocationTargetException
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at java.lang.reflect.Constructor.constructNative(Native Method)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.view.LayoutInflater.createView(LayoutInflater.java:594)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  ... 39 more
    03-22 21:46:58.583: W/SupportMenuInflater(28798): Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x2/d=0x7f010069 a=-1}
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.content.res.Resources.loadDrawable(Resources.java:2068)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.widget.ImageView.<init>(ImageView.java:129)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.widget.ImageView.<init>(ImageView.java:119)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  ... 42 more

Answer

waynesford picture waynesford · Nov 12, 2014

I had a similar probably that appeared on my release builds but not my debug builds when switching over to the v21 support library. Turned out to be an obfuscation problem, and adding this line to my proguard-rules.txt file fixed it:

-keep class android.support.v7.widget.SearchView { *; }