AppCompat v21中的SearchView问题

时间:2014-10-25 16:15:29

标签: android android-appcompat

我正在使用AppCompat v21和工具栏,并在Android Studio 0.8.14中进行开发

如果我通过USB将我的(4.4)手机插入PC并运行app,则searchview可以正常工作。我调出片段并按下搜索图标,它允许我输入文字。

然而,如果我在gradle中组装发布并在我的设备上安装应用程序,当我按下搜索图标时没有任何反应。不启动searchview。这是我的代码。

我的IconsFrag.java

@Override
public void onPrepareOptionsMenu (Menu menu) {
    Log.i(FRAG_TAG, "onPrepareOptionsMenu");
    MenuInflater inflater = getActivity().getMenuInflater();
    inflater.inflate(R.menu.icon_menu, menu);
    MenuItem searchItem = menu.findItem(R.id.search);

    SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);

    SearchView searchView = null;
    if (searchItem != null) {
        searchView = (SearchView) searchItem.getActionView();
    }
    if (searchView != null) {
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
    }

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    Log.i(FRAG_TAG, "onOptionsItemSelected");
    return super.onOptionsItemSelected(item);
}

我的模块build.gradle

repositories {
    mavenLocal()
    mavenCentral()
    dependencies {
        apply plugin: 'maven'
        compile 'com.github.gabrielemariotti.cards:library:1.9.1'
        compile 'com.nhaarman.listviewanimations:library:2.6.0'
        compile 'com.github.gabrielemariotti.changeloglib:library:1.5.2'
        compile 'com.shamanland:fab:0.0.5'
        compile 'com.nineoldandroids:library:2.4.0'
        compile 'com.google.android.apps.muzei:muzei-api:+'
        compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.3'
        compile 'com.loopj.android:android-async-http:1.4.6'
        compile 'com.github.chrisbanes.photoview:library:1.2.3'
        compile 'com.readystatesoftware.systembartint:systembartint:1.0.3'
        compile 'com.daimajia.easing:library:1.0.0@aar'
        compile 'com.daimajia.androidanimations:library:1.1.2@aar'
    }
    maven { url 'http://download.crashlytics.com/maven' }
    flatDir{
        dirs 'libs'
    }
}

Properties props = new Properties()
props.load(new FileInputStream(file(project.property("test.properties"))))

android {
    compileSdkVersion 21
    buildToolsVersion "21.0.2"

    defaultConfig {
        applicationId "source.test.com"
        minSdkVersion 15
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }

    signingConfigs {
        release {
            storeFile file(props['keystore'])
            storePassword props['keystore.password']
            keyAlias props['keyAlias']
            keyPassword props['keyPassword']
        }
    }

    buildTypes {
        release {
            debuggable false
            zipAlign true
            runProguard true
            proguardFile getDefaultProguardFile('proguard-android-optimize.txt')
            proguardFile file('proguard.txt')
            signingConfig signingConfigs.release
        }
    }
    lintOptions {
        checkReleaseBuilds true
    }

}


dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    compile 'com.android.support:appcompat-v7:21.0.0'
    compile 'com.android.support:palette-v7:21.0.0'
    compile 'com.android.support:cardview-v7:21.0.0'
    compile 'com.android.support:recyclerview-v7:21.0.0'
    compile 'com.crashlytics.android:crashlytics:1.+'
    compile 'uk.me.lewisdeane.ldialogs:materialdialog@aar'
}

这是我的菜单

<?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/search"
        android:title="@string/search"
        android:icon="@drawable/app_ic_menu_search"
        app:showAsAction="always"
        app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>

1 个答案:

答案 0 :(得分:1)

可能由proguard重命名SearchView类。尝试将其添加到您的proguard.txt文件

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