无法实例化 class: android.support.w7.widget.SearchView

Cannot instantiate class: android.support.w7.widget.SearchView

我要疯了。我一直在努力弄清楚如何实现 SearchView,但无论我尝试多少种不同的解决方案,我都会 运行 陷入同一个问题。我以为只是我的项目有问题,所以我创建了一个新项目,我唯一尝试实现的是 SearchView,但得到了同样的错误。我正在使用 Android Studio 1.5 但 运行 在 1.3 和 1.4 上遇到了同样的问题。我下载了 API 21,22,23 以及所有额外的工具,包括支持库。

这是错误日志:

11-28 15:35:54.061 18015-18015/? W/SupportMenuInflater: Cannot instantiate class: android.support.w7.widget.SearchView
                                                    java.lang.ClassNotFoundException: Didn't find class "android.support.w7.widget.SearchView" on path: DexPathList[[zip file "/data/app/com.gevdev.tesssst-2/base.apk"],nativeLibraryDirectories=[/vendor/lib64, /system/lib64]]
                                                        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
                                                        at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
                                                        at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
                                                        at android.support.v7.view.SupportMenuInflater$MenuState.newInstance(SupportMenuInflater.java:497)
                                                        at android.support.v7.view.SupportMenuInflater$MenuState.setItem(SupportMenuInflater.java:458)
                                                        at android.support.v7.view.SupportMenuInflater$MenuState.addItem(SupportMenuInflater.java:479)
                                                        at android.support.v7.view.SupportMenuInflater.parseMenu(SupportMenuInflater.java:196)
                                                        at android.support.v7.view.SupportMenuInflater.inflate(SupportMenuInflater.java:118)
                                                        at com.gevdev.tesssst.MainActivity.onCreateOptionsMenu(MainActivity.java:36)
                                                        at android.app.Activity.onCreatePanelMenu(Activity.java:3025)
                                                        at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:298)
                                                        at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85)
                                                        at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:241)
                                                        at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85)
                                                        at android.support.v7.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:443)
                                                        at android.support.v7.app.ToolbarActionBar.run(ToolbarActionBar.java:60)
                                                        at android.os.Handler.handleCallback(Handler.java:739)
                                                        at android.os.Handler.dispatchMessage(Handler.java:95)
                                                        at android.os.Looper.loop(Looper.java:145)
                                                        at android.app.ActivityThread.main(ActivityThread.java:6837)
                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                        at java.lang.reflect.Method.invoke(Method.java:372)
                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
                                                        Suppressed: java.lang.ClassNotFoundException: android.support.w7.widget.SearchView
                                                        at java.lang.Class.classForName(Native Method)
                                                        at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
                                                        at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
                                                        at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
                                                                ... 22 more
                                                     Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

这是我的Android清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gevdev.tesssst">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

主要Activity

package com.gevdev.tesssst;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.SearchView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    MenuItem searchItem = menu.findItem(R.id.action_settings);
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

我的菜单xml文件

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.gevdev.tesssst.MainActivity.MainActivity">
<item
    android:id="@+id/action_settings"
    android:title="Search"
    android:icon="@mipmap/ic_launcher"
    app:showAsAction="ifRoom|collapseActionView"
    app:actionViewClass="android.support.w7.widget.SearchView" >
</item>

应用 build.gradle

apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"

defaultConfig {
    applicationId "com.gevdev.tesssst"
    minSdkVersion 21
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'),                      'proguard-rules.pro'
    }
}
} 

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
}

我只是不确定此时该做什么。我已经查看了所有其他人遇到同样问题的帖子,但我认为他们中的任何一个都不适用于我。

您的 menu.xml 中有 android.support.w7.widget.SearchView。必须

android.support.v7.widget.SearchView

"w" 看起来很像 "v"