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

时间:2015-11-28 23:45:34

标签: android android-studio 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$1.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

这是我的AndroidManifest

<?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>

主要活动

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'
}

我不知道此时该做什么。我看过所有其他人都有同样问题的帖子,但我认为其中任何一个都不适用于我。

1 个答案:

答案 0 :(得分:0)

你的menu.xml中有android.support。 w 7.widget.SearchView。它必须是

android.support.v7.widget.SearchView

&#34; W&#34;看起来很像&#34; v&#34;