无法实例化以下类:- android.support.v7.widget.Toolbar [英] The following classes could not be instantiated: - android.support.v7.widget.Toolbar

查看:27
本文介绍了无法实例化以下类:- android.support.v7.widget.Toolbar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Android Lollipop (5.0) 中使用的新材料设计创建应用.

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

我正在使用以下指南:

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

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

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

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

这里有一些图片:

my_awesome_toolbar.xml 布局预览中的错误

Error in my_awesome_toolbar.xml layout preview

activity_my.xml 布局预览中的错误

Error in activity_my.xml layout preview

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

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:

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

xml code of the styles (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

At the moment I use Android Studio Beta 0.8.9

推荐答案

对于 Android Studio(或 IntelliJ IDEA),

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

等到 Android Studio 完成创建所有缓存 &索引.

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

这篇关于无法实例化以下类:- android.support.v7.widget.Toolbar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆