Java 无法实例化以下类:-?android.support.v7.widget.Toolbar

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26575815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 02:50:54  来源:igfitidea点击:

The following classes could not be instantiated: -?android.support.v7.widget.Toolbar

javaandroidxml

提问by FirestormXYZ

I am creating an app with the new Material Design used in Android Lollipop (5.0).

我正在使用 Android Lollipop (5.0) 中使用的新材料设计创建一个应用程序。

I am using the following guides:

我正在使用以下指南:

After I create my toolbar, I receive this error: "The following classes could not be instantiated: -?android.support.v7.widget.Toolbar"

创建工具栏后,我收到此错误:“无法实例化以下类:-?android.support.v7.widget.Toolbar”

The app works well in the phone or emulator, but the layout designer of Android Studio doesn't show the layout correctly.

该应用程序在手机或模拟器中运行良好,但 Android Studio 的布局设计器无法正确显示布局。

Here there are some images:

这里有一些图片:

Error in my_awesome_toolbar.xml layout preview error in my_awesome_toolbar.xml

my_awesome_toolbar.xml 布局预览中的错误 my_awesome_toolbar.xml 中的错误

Error in activity_my.xml layout preview error in activity_my.xml

activity_my.xml 布局预览中的错误 activity_my.xml 中的错误

The xml code of the files:

文件的xml代码:

my_awesome_toolbar.xml:

my_awesome_toolbar.xml:

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_awesome_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary" />

activity_my.xml:

活动_我的.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/root"
            tools:context="com.myapp.test.MyActivity"
            android:orientation="horizontal">

    <include
        android:id="@+id/toolbar"
        layout="@layout/my_awesome_toolbar"
        />
    <!-- Rest of layout -->
</RelativeLayout>

MyActivity.java:

我的Activity.java:

package com.myapp.test;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;


public class MyActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.my, menu);
        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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

xml code of the styles (values/styles.xml):

样式的 xml 代码 (values/styles.xml):

<resources>

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/blue</item>
        <item name="colorPrimaryDark">@color/dark_blue</item>
        <item name="actionOverflowButtonStyle">@style/AppTheme.OverflowButtonStyle</item>
    </style>

    <!-- Style Title -->
    <style name="Title">
        <item name="android:textColor">@color/white</item>
        <item name="android:textStyle">bold</item>
        <item name="android:textSize">16sp</item>
    </style>

    <style name="AppTheme.OverflowButtonStyle" parent="Widget.AppCompat.ActionButton.Overflow">
        <item name="android:src">@drawable/overflow_icon</item>
    </style>
</resources>

At the moment I use Android Studio Beta 0.8.9

目前我使用 Android Studio Beta 0.8.9

采纳答案by gbero

For Android Studio (or IntelliJ IDEA),

对于 Android Studio(或 IntelliJ IDEA),

If everything looks OK in your project and that you're still receiving the error in all your layouts, try to 'Invalidate caches & restart'.

如果您的项目中一切正常,并且您仍然在所有布局中收到错误,请尝试“使缓存无效并重新启动”。

Wait until Android Studio has finished to create all the caches & indexes.

等到 Android Studio 完成创建所有缓存和索引。

how to invalidate cache & restart

如何使缓存无效并重新启动

回答by FirestormXYZ

Sorry if I answer myself, but, at the finally, the solution of my problem was update Android Studio to the new version 0.8.14 by Canary Channel: http://tools.android.com/recent/

对不起,如果我回答自己,但最后,我的问题的解决方案是通过 Canary Channel 将 Android Studio 更新到新版本 0.8.14:http://tools.android.com/recent/

After the update, the problem is gone:

更新后,问题消失了:

After the update, the problem is gone

更新后问题就解决了

I leave this question here for those who have this problem in the future.

我把这个问题留在这里给将来遇到这个问题的人。

回答by Overloaded_Operator

I did what @gbero said, and I changed the Android version number that Studio uses from 22 to 17 and it works.

我按照@gbero 说的做了,我将 Studio 使用的 Android 版本号从 22 更改为 17,并且可以正常工作。

I am using the backwards compatibility to build for Android ver 22 but to target 17 (idk if that's correctly said, I am still trying to figure this app stuff out) so that triggered the backwards compatibility, which afaik is what the android.support.v7.*is. This is probably a bug with their rendering code. Not sure if clearing the cache as suggested above was needed as rendering didn't work just after invalidating the cache, it started working after I changed the version to render. If I change back to version 22, the rendering breaks, if I switch back to 17, it works again.

我正在使用向后兼容性来构建 Android ver 22,但目标是 17(如果说得对,idk,我仍在尝试弄清楚这个应用程序的内容),以便触发向后兼容性,这就是事实android.support.v7.*。这可能是他们渲染代码的错误。不确定是否需要按照上面的建议清除缓存,因为在使缓存无效后渲染不起作用,它在我将版本更改为渲染后开始工作。如果我改回 22 版,渲染会中断,如果我改回 17 版,它会再次工作。

my fix

我的修复

回答by Ang Lee

I changed the res/values/styles.xml file from this:

我从这里更改了 res/values/styles.xml 文件:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

to this:

对此:

<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">

and that solved the problem.

这解决了问题。

回答by kilian agboton

I had the same error on my Android Studio screen when i wanted to prevew my project. I fix the problem by this ways:

当我想预览我的项目时,我的 Android Studio 屏幕上出现了同样的错误。我通过以下方式解决问题:

1- I chang the version from 22 to 21. But if I change back to version 22, the rendering breaks, if I switch back to 21, it works again. Thank you @Overloaded_Operator

1- 我将版本从 22 更改为 21。但是如果我更改回版本 22,渲染会中断,如果我切换回 21,它会再次工作。谢谢@Overloaded_Operator

I updated my Android Studio but not working. Thank you @Salvuccio96

我更新了我的 Android Studio 但没有工作。谢谢@Salvuccio96

回答by Eric B.

From: https://stackoverflow.com/a/29989542/4123403

来自:https: //stackoverflow.com/a/29989542/4123403

  1. Clean project
  2. Rebuild project
  3. Sync Gradle
  1. 清洁工程
  2. 重建项目
  3. 同步摇篮

This did the trick for me.

这对我有用。

回答by James Moore

I had the same error. Eventually I got this notice that a plugin was out of date:

我有同样的错误。最终我收到了一个插件过期的通知:

error dialog

错误对话框

After I updated, the problem went away.

我更新后,问题就解决了。

回答by WishMaster

The solutions above didn't help me. I've tried 2 first steps from this link. Worked fine for me. But don't forget to

上面的解决方案对我没有帮助。我已经从这个链接尝试了 2 个第一步 。对我来说效果很好。但不要忘记

import com.melnykov.fab.FloatingActionButton; 

instead of

代替

import android.support.design.widget.FloatingActionButton;

in your MainActivity.java

在你的 MainActivity.java

回答by Bj?rn Kechel

Another mistake that can have the same effect can be the wrong theme in the preview. For some reason I had selected some other theme here. After choosing my AppTheme it worked fine again:

另一个可以产生相同效果的错误可能是预览中的错误主题。出于某种原因,我在这里选择了一些其他主题。选择我的 AppTheme 后,它再次正常工作:

layout options

布局选项

回答by Taioli Francesco

  1. Clean project
  2. Rebuild project
  3. Sync Gradle
  1. 清洁工程
  2. 重建项目
  3. 同步摇篮

it work for me

它对我有用