2014-03-07 104 views
18

我正在嘗試爲我的應用程序使用SearchView。首先,我加入appcompat到我的項目:無法實例化類:android.support.v7.widget.SearchView

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

謝勝利,我這個代碼添加到我的菜單:

<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> 

但是當我開始activity我得到這個:

03-07 17:25:39.110 16861-16861/de.treevo.app W/MenuInflater:不能 實例化類:android.support.v7.widget.SearchView java.lang.reflect.InvocationTargetExcepti (MenuInfo) at android.view.MenuInflater $ MenuState.newInstance(MenuInflater.java:471) at android.view.MenuInflater $ MenuState.setItem(MenuInflater.java: 430) 在android.view.MenuInflater $ MenuState.addItem(MenuInflater.java:451) 在android.view.MenuInflater.parseMenu(MenuInflater.java:188) 在android.view.MenuInflater.inflate(MenuInflater.java: 110) 在de.treevo.app.search.onCreateOptionsMenu(search.java:260) 在android.app.Activity.onCreatePanelMenu(Activity.java:2538) 在com.android.internal.policy.impl.PhoneWindow。 preparePanel(PhoneWindow.java:489) at com.android.internal.policy.impl.PhoneWind ow.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 ) 在android.view.Choreographer.doCallbacks(Choreographer.java:574) 在android.view.Choreographer.doFrame(Choreographer.java:543) 在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。調用(Native Method) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) :android.view.InflateException:二進制XML文件行#41:錯誤在android.view.LayoutInflater.createView(LayoutInflater.java:620) 在類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.j ava: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) 在java.lang.reflect.Constructor.newInstance(本機方法) 在android.view.MenuInflater $ MenuState.newInstance(MenuInflater.java:471) 在android.view.MenuInflater $ MenuState .setItem(MenuInflater.java:430) at android.view.MenuInflater $ MenuState.addItem(MenuInflater。Java的:451) 在android.view.MenuInflater.parseMenu(MenuInflater.java:188) 在android.view.MenuInflater.inflate(MenuInflater.java:110) 在de.treevo.app.search.onCreateOptionsMenu(搜索。的java:260) 在android.app.Activity.onCreatePanelMenu(Activity.java:2538) 在com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:489) 在com.android.internal。在android.view.Choreographer $ CallbackRecord.r中的policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:853) un(Choreographer.java:761) at android.view.Choreographer.doCallbacks(Choreographer.java:574) at android.view.Choreographer.doFrame(Choreographer.java:543) at android.view.Choreographer $ FrameDisplayEventReceiver。運行(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) 在android.app.ActivityThread.main(ActivityThread.java:5017) 在java.lang.reflect.Method.invoke(本機方法) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 引起:java.lang.reflect .InvocationTargetException 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(在android.view.LayoutInflater.inflate(LayoutInflater.java:492) 處的android.view.LayoutInflater.inflate(LayoutInflater.java:397) 處於android.support.v7.widget.SearchView處的LayoutInflater.java:755) 。 (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) 在android.view.MenuInflater $ MenuState.setItem(MenuInflater.java:430) 在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) 在android.app.Activity.onCreatePanelMenu(Activity.java:2538) 在com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:489) 在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(Choreogr在android.view.Choreographer.doFrame(Choreographer.java:543) at android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:574) (apher.java:761) Choreographer.java:747) 在android.os.Handler.handleCallback(處理程序。的java:733) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:136) 在android.app.ActivityThread.main(ActivityThread.java: 5017) 在java.lang.reflect.Method.invoke(本機方法) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779) 在com.android.internal.os.ZygoteInit .main(ZygoteInit.java:595) 引起:android.content.res.Resources $ NotFoundException:資源不是Drawable(顏色或路徑): TypedValue {t = 0x2/d = 0x7f010030 a = -1} 在android.content.res.Resources.loadDrawable(Resources.ja (ImageView.java:129) at android.widget.ImageView。(ImageView.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 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.L ayoutInflater.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(M enuInflater.java:451) 在android.view.MenuInflater.parseMenu(MenuInflater.java:188) 在android.view.MenuInflater.inflate(MenuInflater.java:110) 在de.treevo.app.search.onCreateOptionsMenu( search.java:260) 在android.app.Activity.onCreatePanelMenu(Activity.java:2538) 在com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:489) 在com.android。在android.view.Choreographer $ Call中調用internal.Policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:853) com.android.internal.policy.impl.PhoneWindow $ 1.run(PhoneWindow.java:273) backRecord.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) 在android.os.Handler.handleCallback(Handler.java:733) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper。循環(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779) 在com.android.internal.os.ZygoteInit.main(ZygoteInit。Java的:595)

,當我推的搜索圖標沒有發生:(

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; 
} 

回答

13

嘗試添加自定義prefixe你的佈局,如:

<?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> 

請參閱the documentation,在該示例中,您必須添加自定義前綴(如上面的代碼中的「app」)。

代替

而且,:

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

試試這個:

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

讓我知道,如果這有助於。

+0

沒有任何反應。它與「app」的所有內容一樣沒有效果 – ternes3

+0

這不是「app」的所有內容,只是xmlns(* res-auto *),showAsAction和actionViewClass屬性。你可以用'onCreateOptionsMenu'中的代碼編輯你的問題嗎? – Fllo

+0

當然,請參閱編輯。 – ternes3

32

在我的情況下,它適用於開發模式,但在發佈模式下崩潰(導出已簽名的apk)。爲了解決這個問題,請將此行添加到proguard-rules.txt

-keep class android.support.v7.widget.SearchView { *; } 
相關問題