Android SearchView不起作用

时间:2014-09-04 08:33:11

标签: android android-support-library searchview

我尝试更新所有库,但我仍然遇到错误。我可以在模拟器上运行应用程序,但是当我导出APK并在真正的Android设备上运行时,该应用程序无法正常工作。

MinSdkVersion为8,targetSdkVersion为19。 请帮忙!!!

menu.xml文件

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
        xmlns:example="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/action_showTC"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="Terms and Conditions"/>

    <item android:id="@+id/action_search"
          android:title="Search"
          android:orderInCategory="100"
          example:showAsAction="ifRoom|collapseActionView"
          example:actionViewClass="android.support.v7.widget.SearchView"/>

</menu>

SearchView

@Override
public boolean onOptionsItemSelected(MenuItem item){
    if(item.getItemId() == R.id.action_showTC){
        startActivity(new Intent(MainActivity.this, T_C.class));
        return true;
    }else{
        return super.onOptionsItemSelected(item);
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SupportMenuItem searchMenuItem = ((SupportMenuItem) menu.findItem(R.id.action_search));
    SearchView searchView = (SearchView) searchMenuItem.getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

    return super.onCreateOptionsMenu(menu);
}

清单

    <activity android:name="com.agehk.queue.SearchResultsActivity"
        android:parentActivityName="com.agehk.queue.MainActivity" >
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>

        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />
    </activity>

searchable.xml

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:hint="Search Queue"
    android:label="@string/app_name" />

错误

09-04 15:57:13.105: W/SupportMenuInflater(17184): Cannot instantiate class: android.support.v7.widget.SearchView
09-04 15:57:13.105: W/SupportMenuInflater(17184): java.lang.NoSuchMethodException: <init> [class android.content.Context]
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at java.lang.Class.getConstructorOrMethod(Class.java:423)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at java.lang.Class.getConstructor(Class.java:397)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at android.support.v7.internal.view.f.a(Unknown Source)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at android.support.v7.internal.view.f.a(Unknown Source)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at android.support.v7.internal.view.f.b(Unknown Source)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at android.support.v7.internal.view.d.a(Unknown Source)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at android.support.v7.internal.view.d.inflate(Unknown Source)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at com.agehk.queue.MainActivity.onCreateOptionsMenu(Unknown Source)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at android.app.Activity.onCreatePanelMenu(Activity.java:2577)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at android.support.v4.app.j.onCreatePanelMenu(Unknown Source)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at android.support.v7.a.g.a(Unknown Source)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at android.support.v7.a.n.a(Unknown Source)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at android.support.v7.a.g.onCreatePanelMenu(Unknown Source)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at android.support.v7.a.o.onCreatePanelMenu(Unknown Source)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:414)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:777)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:199)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:835)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at android.view.Choreographer.doCallbacks(Choreographer.java:626)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at android.view.Choreographer.doFrame(Choreographer.java:589)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:821)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at android.os.Handler.handleCallback(Handler.java:730)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at android.os.Handler.dispatchMessage(Handler.java:92)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at android.os.Looper.loop(Looper.java:150)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at android.app.ActivityThread.main(ActivityThread.java:5390)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at java.lang.reflect.Method.invokeNative(Native Method)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at java.lang.reflect.Method.invoke(Method.java:525)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-04 15:57:13.105: W/SupportMenuInflater(17184):   at dalvik.system.NativeStart.main(Native Method)
09-04 15:57:13.105: D/AndroidRuntime(17184): Shutting down VM
09-04 15:57:13.105: W/dalvikvm(17184): threadid=1: thread exiting with uncaught exception (group=0x42f05500)
09-04 15:57:13.105: E/AndroidRuntime(17184): FATAL EXCEPTION: main
09-04 15:57:13.105: E/AndroidRuntime(17184): java.lang.NullPointerException
09-04 15:57:13.105: E/AndroidRuntime(17184):    at com.agehk.queue.MainActivity.onCreateOptionsMenu(Unknown Source)
09-04 15:57:13.105: E/AndroidRuntime(17184):    at android.app.Activity.onCreatePanelMenu(Activity.java:2577)
09-04 15:57:13.105: E/AndroidRuntime(17184):    at android.support.v4.app.j.onCreatePanelMenu(Unknown Source)
09-04 15:57:13.105: E/AndroidRuntime(17184):    at android.support.v7.a.g.a(Unknown Source)
09-04 15:57:13.105: E/AndroidRuntime(17184):    at android.support.v7.a.n.a(Unknown Source)
09-04 15:57:13.105: E/AndroidRuntime(17184):    at android.support.v7.a.g.onCreatePanelMenu(Unknown Source)
09-04 15:57:13.105: E/AndroidRuntime(17184):    at android.support.v7.a.o.onCreatePanelMenu(Unknown Source)
09-04 15:57:13.105: E/AndroidRuntime(17184):    at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:414)
09-04 15:57:13.105: E/AndroidRuntime(17184):    at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:777)
09-04 15:57:13.105: E/AndroidRuntime(17184):    at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:199)
09-04 15:57:13.105: E/AndroidRuntime(17184):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:835)
09-04 15:57:13.105: E/AndroidRuntime(17184):    at android.view.Choreographer.doCallbacks(Choreographer.java:626)
09-04 15:57:13.105: E/AndroidRuntime(17184):    at android.view.Choreographer.doFrame(Choreographer.java:589)
09-04 15:57:13.105: E/AndroidRuntime(17184):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:821)
09-04 15:57:13.105: E/AndroidRuntime(17184):    at android.os.Handler.handleCallback(Handler.java:730)
09-04 15:57:13.105: E/AndroidRuntime(17184):    at android.os.Handler.dispatchMessage(Handler.java:92)
09-04 15:57:13.105: E/AndroidRuntime(17184):    at android.os.Looper.loop(Looper.java:150)
09-04 15:57:13.105: E/AndroidRuntime(17184):    at android.app.ActivityThread.main(ActivityThread.java:5390)
09-04 15:57:13.105: E/AndroidRuntime(17184):    at java.lang.reflect.Method.invokeNative(Native Method)
09-04 15:57:13.105: E/AndroidRuntime(17184):    at java.lang.reflect.Method.invoke(Method.java:525)
09-04 15:57:13.105: E/AndroidRuntime(17184):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-04 15:57:13.105: E/AndroidRuntime(17184):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-04 15:57:13.105: E/AndroidRuntime(17184):    at dalvik.system.NativeStart.main(Native Method)
09-04 15:57:13.115: D/BatteryStatsImpl(445): noteStopWakeLocked com.google.android.gms isFromForeground = false
09-04 15:57:13.115: W/ActivityManager(445):   Force finishing activity com.agehk.queue/.MainActivity

8 个答案:

答案 0 :(得分:37)

将以下代码添加到proguard-rules.pro

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

答案 1 :(得分:15)

您是否在构建中启用了Proguard?如果是这样,您可能希望确保appcompat库位于Proguard排除列表中(在proguard.cfg中)。蛮力方法是将所有支持库类保留在:

   -keep class android.support.v4.app.** { *; }
   -keep interface android.support.v4.app.** { *; }
   -keep class android.support.v7.app.** { *; }
   -keep interface android.support.v7.app.** { *; }

在我的情况下,我有一个扩展支持库的SearchView的类,所以我将它添加到我的proguard.cfg中:

-keep public class * extends android.support.v7.widget.SearchView {
   public <init>(android.content.Context);
   public <init>(android.content.Context, android.util.AttributeSet);
}

特别提到构造函数以避免错误:

java.lang.NoSuchMethodException: <init> [class android.content.Context]

答案 2 :(得分:10)

经过长时间的研究,这个问题的简单解决方案即添加

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

在AndroidStudio的app / proguard / android.proguard文件中。

干杯!

答案 3 :(得分:5)

我(也)得到了

Attempt to invoke virtual method 'void android.support.v7.widget.SearchView.setOnQueryTextListener(android.support.v7.widget.ao)' on a null object reference

启动发布版本时(启用了proguard / minify)。

将其添加到proguard规则中修复它:

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

这将保留SearchView小部件,但仍然允许proguard丢弃您未使用的任何其他支持库类,因此您可以使发布版本保持良好和整洁。

答案 4 :(得分:5)

proguard-rules.pro

所需的最低要求
-keep class android.support.v7.widget.SearchView {
   public <init>(android.content.Context);
   public <init>(android.content.Context, android.util.AttributeSet);
}

答案 5 :(得分:1)

我必须添加它,现在它可以顺利运行:

-keep class android.support.v7.widget.SearchView {
   public <init>(android.content.Context);
   public <init>(android.content.Context, android.util.AttributeSet);
}
-keep interface android.support.v7.widget.SearchView {
   public <init>(android.content.Context);
   public <init>(android.content.Context, android.util.AttributeSet);
}

答案 6 :(得分:0)

对于AndroidX searchview proguard,您必须使用:

-keep class androidx.appcompat.widget.SearchView { *; }

答案 7 :(得分:-1)

对于11之前的API,您应该在兼容模式下初始化操作栏项目:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        MenuItem searchMenuItem = menu.findItem(R.id.action_search);
        SearchView searchView = (SearchView)MenuItemCompat.getActionView(searchMenuItem);
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        return true;
    }

<强>更新:
所以,我尝试在API 8模拟器和4.4 KitKat上重现它。不幸的是,我的2.3.8设备前一段时间被砖了,因此无法检查低API的硬件。我可以建议你:
1.检查SearchView是否从android.support.v7.widget.SearchView;导入 2.检查菜单资源是否正确:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto" <----------- init compatible namespace
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity" >

    <item
        android:id="@+id/action_showTC"
        android:orderInCategory="100"
        android:title="@string/terms_and_conditions"
        app:showAsAction="never"/>  <----------- use compatible namespace

    <item android:id="@+id/action_search"
          android:title="@string/search"
          android:orderInCategory="100"  <---------- use different value
          app:showAsAction="ifRoom|collapseActionView"
          app:actionViewClass="android.support.v7.widget.SearchView"/>

</menu>

3。如果没有基础处理(活动类没有子类),则从onCreateOptionsMenu返回true

UPDATE2: GOT IT!您可能从Activity课程延伸。应该是ActioBarActivity

public class MainActivity extends ActionBarActivity {
...