API 15:不支持此功能,请使用MenuItemCompat.setOnActionExpandListener()

时间:2018-12-17 15:46:56

标签: android-studio-3.0

Android Studio 3.2.1

app / build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    defaultConfig {
        applicationId "com.myproject"
        minSdkVersion 15
        targetSdkVersion 21
        versionCode 421
        versionName "2.1.421"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

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

    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
    }
}

def AAVersion = '4.5.2'

dependencies {
    annotationProcessor "org.androidannotations:androidannotations:$AAVersion"
    annotationProcessor "org.androidannotations:ormlite:$AAVersion"

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

    implementation 'com.android.support:appcompat-v7:21.0.0'

    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'org.apache.commons:commons-lang3:3.8.1'
    implementation 'org.apache.httpcomponents:httpclient:4.5.6'
    implementation 'com.google.android.gms:play-services-gcm:16.0.0'
    implementation 'com.google.code.gson:gson:2.8.2'
    implementation 'com.j256.ormlite:ormlite-android:5.1'
    implementation 'commons-io:commons-io:2.6'
    implementation "org.androidannotations:androidannotations-api:$AAVersion"
    implementation "org.androidannotations:ormlite-api:$AAVersion"   

}

这是我的课:

import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SearchView;
public class SearchViewProviderWidget {

MenuItemCompat.setOnActionExpandListener(searchMenuItem, new MenuItemCompat.OnActionExpandListener() {
            @Override
            public boolean onMenuItemActionExpand(MenuItem menuItem) {
                isSearchWidgetExpanded = true;
                mToolbar.setBackgroundColor(activity.getResources().getColor(R.color.search_view_background));
                searchView.setFocusable(true);
                searchView.setIconified(false);
                searchView.requestFocusFromTouch();
                searchView.setIconifiedByDefault(false);

                menu.setGroupVisible(R.id.main_menu_group, false);
                return true;
            }

            @Override
            public boolean onMenuItemActionCollapse(MenuItem menuItem) {                
                isSearchWidgetExpanded = false;
                mToolbar.setBackgroundColor(activity.getResources().getColor(R.color.factura_background_color));
                searchView.setQuery("", false);
                menu.setGroupVisible(R.id.main_menu_group, true);
                AndroidUtil.hideKeyboard((Activity) activity);
                return true;
            }
        });
}

但是在这一行:

MenuItemCompat.setOnActionExpandListener(searchMenuItem, new MenuItemCompat.OnActionExpandListener() {

我收到错误消息:

D/AndroidRuntime(11203): Shutting down VM                                                
E/AndroidRuntime(11203): FATAL EXCEPTION: main                                               
E/AndroidRuntime(11203): Process: com.myproject.app, PID: 11203                                      
E/AndroidRuntime(11203): java.lang.UnsupportedOperationException: This is not supported, use MenuItemCompat.setOnActionExpandListener()  
E/AndroidRuntime(11203):    at android.support.v7.internal.view.menu.MenuItemImpl.setOnActionExpandListener(MenuItemImpl.java:729)      
E/AndroidRuntime(11203):    at android.support.v4.view.MenuItemCompat.setOnActionExpandListener(MenuItemCompat.java:464)            
E/AndroidRuntime(11203):    at com.myproject.app.widget.SearchViewProviderWidget.<init>(SearchViewProviderWidget.java:146)          
E/AndroidRuntime(11203):    at com.myproject.app.activity.SubscribesFragmentActivity.onCreateOptionsMenu(SubscribesFragmentActivity.java:85
E/AndroidRuntime(11203):    at android.app.Activity.onCreatePanelMenu(Activity.java:2852)                           
E/AndroidRuntime(11203):    at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:328)             
E/AndroidRuntime(11203):    at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:276)          
E/AndroidRuntime(11203):    at android.support.v7.app.ActionBarActivityDelegate$1.onCreatePanelMenu(ActionBarActivityDelegate.java:79)  
E/AndroidRuntime(11203):    at android.support.v7.widget.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:49)     
E/AndroidRuntime(11203):    at android.support.v7.internal.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:459)      
E/AndroidRuntime(11203):    at android.support.v7.internal.app.ToolbarActionBar$1.run(ToolbarActionBar.java:69)             
E/AndroidRuntime(11203):    at android.os.Handler.handleCallback(Handler.java:739)                              
E/AndroidRuntime(11203):    at android.os.Handler.dispatchMessage(Handler.java:95)                              
E/AndroidRuntime(11203):    at android.os.Looper.loop(Looper.java:148)                                  
E/AndroidRuntime(11203):    at android.app.ActivityThread.main(ActivityThread.java:5417)                            
E/AndroidRuntime(11203):    at java.lang.reflect.Method.invoke(Native Method)                               
E/AndroidRuntime(11203):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)              
E/AndroidRuntime(11203):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)                         
W/ActivityManager(  776):   Force finishing activity com.myproject.app/.activity.SubscribesFragmentActivity              

我必须使用API​​ 15

0 个答案:

没有答案