我正在嘗試爲我的應用程序使用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;
}
沒有任何反應。它與「app」的所有內容一樣沒有效果 – ternes3
這不是「app」的所有內容,只是xmlns(* res-auto *),showAsAction和actionViewClass屬性。你可以用'onCreateOptionsMenu'中的代碼編輯你的問題嗎? – Fllo
當然,請參閱編輯。 – ternes3