android .widget.SearchView.setOnQueryTextListener (android .widget.SearchView $ OnQueryTextListener) 'для пустой ссылки на объект - PullRequest
0 голосов
/ 18 апреля 2020

Я пытаюсь добавить функцию поиска в свой проект. Когда я запускаю код, у меня возникает эта проблема. Я видел решения с файлом proguard, добавив эту строку (первую), но она все еще не работает. В других вопросах они допустили ошибку, поставив android: showAsAction вместо app: showAsAction. В моем случае это нормально, но Android Studio не распознает пакет v7, и линия отображается красным.

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

меню. xml

<?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="Search"
        android:icon="@drawable/ic_action_search_"
        app:showAsAction="always|collapseActionView"
        app:actionViewClass="android.support.v7.widget.SearchView"
        />


</menu>

proguard-rules.pro

# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
#   http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
#   public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
-keep class android.support.v7.widget.SearchView { *; }

build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId "com.example.janazahapp"
        minSdkVersion 16
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'com.android.support:recyclerview-v7:29.0.3'
    implementation 'com.android.support:cardview-v7:29.0.3'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'com.google.android.gms:play-services-maps:17.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    //noinspection GradleCompatible
    implementation 'com.android.support:design:28.0.0'
    implementation 'com.google.firebase:firebase-analytics:17.2.2'
    implementation 'com.google.firebase:firebase-core:11.8.0'
    implementation 'com.google.firebase:firebase-auth:19.3.0'
    implementation 'com.google.android.gms:play-services-auth:17.0.0'
    implementation 'com.google.android.material:material:1.0.0'
}
apply plugin: 'com.google.gms.google-services'

MainActivity . java (фрагмент кода, в котором я получаю сообщение об ошибке в методе searchView.setOnQueryTextListener)

 @Override
    public boolean  onCreateOptionsMenu(Menu menu){

            MenuInflater menuInflater = getMenuInflater();
            menuInflater.inflate(R.menu.menu,menu);
            MenuItem item = menu.findItem(R.id.search);
            SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
            searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener(){

                @Override
                public boolean onQueryTextSubmit(String query) {

                    myAdapter.getFilter().filter(query);
                    return false;
                }

                @Override
                public boolean onQueryTextChange(String newText) {

                    myAdapter.getFilter().filter(newText);
                    return false;
                }
        });
        return  true;
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...