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

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

我使用以下指南:

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

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

下面是一些图片:

My _ Awesome _ toolbar.xml 布局预览中的错误 error in my_awesome_toolbar.xml

Activity _ my.xml 布局预览中的错误 error in activity_my.xml

文件的 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:

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

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);
}
}

样式(value/styles.xml)的 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>

目前我使用的是 Android Studio Beta 0.8.9

134489 次浏览

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/

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.

For Android Studio (or 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.

how to invalidate cache & restart

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

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.

my fix

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

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

to this:

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

and that solved the problem.

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

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

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

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

  1. Clean project
  2. Rebuild project
  3. Sync Gradle

This did the trick for me.

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.

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

import com.melnykov.fab.FloatingActionButton;

instead of

import android.support.design.widget.FloatingActionButton;

in your MainActivity.java

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:

layout options

  1. Clean project
  2. Rebuild project
  3. Sync Gradle

it work for me

I use android studio 2.3.3: - Open styles.xml - Android studio will show a notification on the top right with two options: "open editor" and "Hide notification". - Click "open editor" - Under theme parent dropdown, click show all themes - Select any theme starting with AppCompat... (I used AppComat.DayNight)

NB: If your title bar disappears, you need to extend AppCompatActivity instead of Activity.

All the best!

I did what @ang_lee said and also i added this line to the app theme style :

<item name="windowActionBar">false</item>

i am using version 26.0.1 :

com.android.support:design:26.0.1
com.android.support:appcompat-v7:26.0.1

building tools:

buildToolsVersion "26.0.1"

none of above worked for me , i updated the appCompat - v7 version in my app gradle file from 23 to 25.3.1. helped to make it work for me

I had the same problem for one of the activities in my app , one of the causes of this problem is that Theme in the theme editor might be different than the theme defined in the 'styles.xml'.change the Theme in the theme editor to your 'Apptheme' or your custom defined theme(if you have defined). Doing this fixed my issue.

My Problem has also been solved by changing in styles.xml

<!-- Base application theme. -->
<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">

Find styles.xml in app/res/values folder.

Parent attribute of the style could be missing "Base". It should start as

<style name="AppTheme" parent="Base.Theme.AppCompat...