Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I´m trying to use a SearchView for my Application. First i added appcompat to my project:

 compile "com.android.support:appcompat-v7:19.0.1+"

secound i add this code to my menu:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/action_search"
    android:title="@string/search"
    android:icon="@drawable/ic_action_search"
    android:showAsAction="ifRoom|collapseActionView"
    android:actionViewClass="android.support.v7.widget.SearchView" />
</menu>

but when i start the activity i get this:

03-07 17:25:39.110 16861-16861/de.treevo.app W/MenuInflater﹕ Cannot instantiate class: android.support.v7.widget.SearchView java.lang.reflect.InvocationTargetException at java.lang.reflect.Constructor.newInstance(Native Method) at android.view.MenuInflater$MenuState.newInstance(MenuInflater.java:471) at android.view.MenuInflater$MenuState.setItem(MenuInflater.java:430) at android.view.MenuInflater$MenuState.addItem(MenuInflater.java:451) at android.view.MenuInflater.parseMenu(MenuInflater.java:188) at android.view.MenuInflater.inflate(MenuInflater.java:110) at de.treevo.app.search.onCreateOptionsMenu(search.java:260) at android.app.Activity.onCreatePanelMenu(Activity.java:2538) at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:489) at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:853) at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:273) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761) at android.view.Choreographer.doCallbacks(Choreographer.java:574) at android.view.Choreographer.doFrame(Choreographer.java:543) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) Caused by: android.view.InflateException: Binary XML file line #41: Error inflating class at android.view.LayoutInflater.createView(LayoutInflater.java:620) at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694) at android.view.LayoutInflater.rInflate(LayoutInflater.java:755) at android.view.LayoutInflater.inflate(LayoutInflater.java:492) at android.view.LayoutInflater.inflate(LayoutInflater.java:397) at android.support.v7.widget.SearchView.(SearchView.java:262) at android.support.v7.widget.SearchView.(SearchView.java:254)             at java.lang.reflect.Constructor.newInstance(Native Method)             at android.view.MenuInflater$MenuState.newInstance(MenuInflater.java:471)             at android.view.MenuInflater$MenuState.setItem(MenuInflater.java:430)             at android.view.MenuInflater$MenuState.addItem(MenuInflater.java:451)             at android.view.MenuInflater.parseMenu(MenuInflater.java:188)             at android.view.MenuInflater.inflate(MenuInflater.java:110)             at de.treevo.app.search.onCreateOptionsMenu(search.java:260)             at android.app.Activity.onCreatePanelMenu(Activity.java:2538)             at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:489)             at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:853)             at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:273)             at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)             at android.view.Choreographer.doCallbacks(Choreographer.java:574)             at android.view.Choreographer.doFrame(Choreographer.java:543)             at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)             at android.os.Handler.handleCallback(Handler.java:733)             at android.os.Handler.dispatchMessage(Handler.java:95)             at android.os.Looper.loop(Looper.java:136)             at android.app.ActivityThread.main(ActivityThread.java:5017)             at java.lang.reflect.Method.invoke(Native Method)             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Constructor.newInstance(Native Method) at android.view.LayoutInflater.createView(LayoutInflater.java:594)             at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)             at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)             at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)             at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)             at android.view.LayoutInflater.inflate(LayoutInflater.java:492)             at android.view.LayoutInflater.inflate(LayoutInflater.java:397)             at android.support.v7.widget.SearchView.(SearchView.java:262)             at android.support.v7.widget.SearchView.(SearchView.java:254)             at java.lang.reflect.Constructor.newInstance(Native Method)             at android.view.MenuInflater$MenuState.newInstance(MenuInflater.java:471)             at android.view.MenuInflater$MenuState.setItem(MenuInflater.java:430)             at android.view.MenuInflater$MenuState.addItem(MenuInflater.java:451)             at android.view.MenuInflater.parseMenu(MenuInflater.java:188)             at android.view.MenuInflater.inflate(MenuInflater.java:110)             at de.treevo.app.search.onCreateOptionsMenu(search.java:260)             at android.app.Activity.onCreatePanelMenu(Activity.java:2538)             at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:489)             at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:853)             at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:273)             at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)             at android.view.Choreographer.doCallbacks(Choreographer.java:574)             at android.view.Choreographer.doFrame(Choreographer.java:543)             at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)             at android.os.Handler.handleCallback(Handler.java:733)             at android.os.Handler.dispatchMessage(Handler.java:95)             at android.os.Looper.loop(Looper.java:136)             at android.app.ActivityThread.main(ActivityThread.java:5017)             at java.lang.reflect.Method.invoke(Native Method)             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x2/d=0x7f010030 a=-1} at android.content.res.Resources.loadDrawable(Resources.java:2068) at android.content.res.TypedArray.getDrawable(TypedArray.java:602) at android.widget.ImageView.(ImageView.java:129) at android.widget.ImageView.(ImageView.java:119)             at java.lang.reflect.Constructor.newInstance(Native Method)             at android.view.LayoutInflater.createView(LayoutInflater.java:594)             at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)             at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)             at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)             at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)             at android.view.LayoutInflater.inflate(LayoutInflater.java:492)             at android.view.LayoutInflater.inflate(LayoutInflater.java:397)             at android.support.v7.widget.SearchView.(SearchView.java:262)             at android.support.v7.widget.SearchView.(SearchView.java:254)             at java.lang.reflect.Constructor.newInstance(Native Method)             at android.view.MenuInflater$MenuState.newInstance(MenuInflater.java:471)             at android.view.MenuInflater$MenuState.setItem(MenuInflater.java:430)             at android.view.MenuInflater$MenuState.addItem(MenuInflater.java:451)             at android.view.MenuInflater.parseMenu(MenuInflater.java:188)             at android.view.MenuInflater.inflate(MenuInflater.java:110)             at de.treevo.app.search.onCreateOptionsMenu(search.java:260)             at android.app.Activity.onCreatePanelMenu(Activity.java:2538)             at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:489)             at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:853)             at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:273)             at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)             at android.view.Choreographer.doCallbacks(Choreographer.java:574)             at android.view.Choreographer.doFrame(Choreographer.java:543)             at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)             at android.os.Handler.handleCallback(Handler.java:733)             at android.os.Handler.dispatchMessage(Handler.java:95)             at android.os.Looper.loop(Looper.java:136)             at android.app.ActivityThread.main(ActivityThread.java:5017)             at java.lang.reflect.Method.invoke(Native Method)             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)

and when i push the search icon nothing happen :(

Edit1:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.offline, menu);
    MenuItem searchItem = menu.findItem(R.id.action_search);
    mSearchView = (SearchView) searchItem.getActionView();
    //setupSearchView(searchItem);

   // menu.findItem(R.id.action_search).getActionView();

    return true;
}
like image 520
ternes3 Avatar asked Mar 07 '14 16:03

ternes3


2 Answers

In my case it works on development mode however crashes on release mode (export signed apk). To fix that please adding this line to proguard-rules.txt

-keep class android.support.v7.widget.SearchView { *; }
like image 183
Quan Nguyen Avatar answered Oct 14 '22 11:10

Quan Nguyen


Try to add a custom prefixe on your layout like:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto" >

      <item android:id="@+id/action_search"
          android:title="@string/search"
          android:icon="@drawable/ic_action_search"
          app:showAsAction="ifRoom|collapseActionView"
          app:actionViewClass="android.support.v7.widget.SearchView" />

</menu>  

See the documentation, in the example, you must to add a custom prefixe (as "app" in the code above).


Also, instead of:

MenuItem searchItem = menu.findItem(R.id.action_search);
mSearchView = (SearchView) searchItem.getActionView();  

Try this:

MenuItem searchItem = menu.findItem(R.id.action_search);
// with MenuItemCompat instead of your MenuItem
mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);  

Let me know if this helps.

like image 24
Blo Avatar answered Oct 14 '22 11:10

Blo