studio - searchable android
No se puede crear una instancia de la clase android.support.v7.widget.SearchView (3)
Por favor que alguien me ayude! Intento agregar la vista de búsqueda de appcompat a mi proyecto. Sin éxito Cuando cambio android.support.v7.widget.SearchView a SearchView (API> 10) todo está bien.
Este es mi menú 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" />
Así es como trato de usar 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);
}
Este es mi archivo manifiesto:
<?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>
Y esto lo que veo en los 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
El problema estaba en la mala biblioteca de soporte v7. Después de actualizar todo bien.
Probablemente tuve una versión similar que apareció en mis compilaciones de lanzamiento, pero no mis compilaciones de depuración cuando cambié a la biblioteca de soporte v21. Resultó ser un problema de ofuscación, y agregar esta línea a mi archivo proguard-rules.txt
solucionó:
-keep class android.support.v7.widget.SearchView { *; }
Tuve el mismo problema cuando estaba usando Android Studio y Gradle para compilar mi proyecto. Me gustaría añadir un SearchView de compat V7.
La solución es cambiar la versión del complemento gradle de 0.9.1 u otros a 0.9.0. Es una forma temporal que encontré. Es tal vez un error del plugin gradle. Espero que ayude.
dependencies {
classpath "com.android.tools.build:gradle:0.9.0"
}