The following classes could not be instantiated: -

2019-01-02 18:07发布

I am creating an app with the new Material Design used in 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"

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

Here there are some images:

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

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

The xml code of the files:

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

At the moment I use Android Studio Beta 0.8.9

标签: java android xml
16条回答
呛了眼睛熬了心
2楼-- · 2019-01-02 18:14

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

查看更多
像晚风撩人
3楼-- · 2019-01-02 18:14

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.

查看更多
伤终究还是伤i
4楼-- · 2019-01-02 18:15

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

查看更多
临风纵饮
5楼-- · 2019-01-02 18:16

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

查看更多
梦寄多情
6楼-- · 2019-01-02 18:16

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"
查看更多
宁负流年不负卿
7楼-- · 2019-01-02 18:16

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

查看更多
登录 后发表回答