繁体   English   中英

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

[英]The following classes could not be instantiated: - android.support.v7.widget.Toolbar

我正在使用Android Lollipop(5.0)中使用的新Material Design创建一个应用程序。

我使用以下指南:

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

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

这里有一些图片:

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

activity_my.xml布局预览出错 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);
    }
}

样式的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>

目前我使用Android Studio Beta 0.8.9

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

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

对此:

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

这解决了这个问题。

对于Android Studio(或IntelliJ IDEA),

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

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

如何使缓存和重启无效

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

我正在使用向后兼容性来构建Android版本22,但是目标17(idk如果正确地说,我仍然试图将这个应用程序的东西拿出来),这样就触发了向后兼容性,这就是android.support.v7.* afaik android.support.v7.*是。 这可能是他们的渲染代码的一个错误。 不确定是否需要按照上面的建议清除缓存,因为渲染在使缓存无效后不起作用,它在我将版本更改为渲染后开始工作。 如果我改回版本22,渲染会中断,如果我切换回17,它会再次起作用。

我的修复

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

  1. 清洁项目
  2. 重建项目
  3. 同步Gradle

这对我有用。

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

布局选项

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

更新后,问题消失了:

更新后,问题就消失了

我把这个问题留给那些将来遇到这个问题的人。

  1. 清洁项目
  2. 重建项目
  3. 同步Gradle

它对我有用

我做了@ang_lee所说的,并且我将这一行添加到app主题样式:

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

我使用的是版本26.0.1:

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

建筑工具:

buildToolsVersion "26.0.1"

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

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

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

以上都没有为我工作,我更新了我的应用程序gradle文件中的appCompat - v7版本从23到25.3.1。 帮助我的工作

我有同样的错误。 最后我收到通知说插件已经过时了:

错误对话框

我更新后,问题就消失了。

上面的解决方案对我没有帮助。 我从这个链接尝试了2个第一步。 为我工作得很好。 但别忘了

import com.melnykov.fab.FloatingActionButton; 

代替

import android.support.design.widget.FloatingActionButton;

在您的MainActivity.java中

我使用android studio 2.3.3: - 打开styles.xml - Android工作室将在右上角显示一个通知,有两个选项:“打开编辑器”和“隐藏通知”。 - 单击“打开编辑器” - 在主题父下拉列表下,单击显示所有主题 - 选择以AppCompat开头的任何主题...(我使用了AppComat.DayNight)

注意:如果标题栏消失,则需要扩展AppCompatActivity而不是Activity。

祝一切顺利!

我的应用程序中的一个活动遇到了同样的问题,导致此问题的原因之一是主题编辑器中的主题可能与'styles.xml'中定义的主题不同。在主题编辑器中更改主题到你的'Apptheme'或你定制的主题(如果你已定义)。 这样做解决了我的问题。

我的问题也通过在styles.xml中更改来解决

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

在app / res / values文件夹中找到styles.xml。

样式的父属性可能缺少“基础”。 它应该从头开始

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM