gpt4 book ai didi

java - 无法实例化类 : android. support.v7.widget.SearchView

转载 作者:行者123 更新时间:2023-12-03 07:36:07 27 4
gpt4 key购买 nike

我正在尝试使用 SearchView对于我的应用程序。首先我添加了appcompat到我的项目:

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



当我按下搜索图标时,什么也没有发生:(

编辑1:
@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;
}

最佳答案

就我而言,它适用于开发模式,但在 Release模式下崩溃(导出签名的 apk)。要解决此问题,请将此行添加到 proguard-rules.txt

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

关于java - 无法实例化类 : android. support.v7.widget.SearchView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22255513/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com