Android Studio(1.5.1)渲染选择空白活动布局时出现问题

时间:2016-02-26 19:52:54

标签: java android xml rendering

感谢任何帮助。

当我在Android Studio(1.5.1)中创建一个新项目时,我不断遇到渲染问题。这是错误。

我还应该注意,我已经尝试过更改Android预览版本和Theams并且每次选择仍然会出错

修改

我想我刚刚在.java中解决了这个问题。删除setContentView(R.layout.activity_main)下的所有内容;并且错误消失了,现在......

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}
Rendering Problem
The following classes could not be instantiated:

android.support.desitn.widget.FloatingActionButton (open class, show exception, clear cache)


android.content.res.Resources$NotFoundException: Unable to find resource ID #0x1080029
    at android.content.res.Resources.getResourceName(Resources.java:2235)
    at android.content.res.Resources.loadDrawableForCookie(Resources.java:2602)
    at android.content.res.Resources.loadDrawable(Resources.java:2540)
    at android.content.res.Resources.getDrawable(Resources.java:806)
    at android.content.Context.getDrawable(Context.java:458)
    at android.support.v4.content.ContextCompatApi21.getDrawable(ContextCompatApi21.java:26)
    at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:321)
    at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:180)
    at android.support.v7.widget.TintTypedArray.getDrawableIfKnown(TintTypedArray.java:70)
    at android.support.v7.widget.AppCompatImageHelper.loadFromAttributes(AppCompatImageHelper.java:39)
    at android.support.v7.widget.AppCompatImageButton.<init>(AppCompatImageButton.java:65)
    at android.support.design.widget.VisibilityAwareImageButton.<init>(VisibilityAwareImageButton.java:37)
    at android.support.design.widget.FloatingActionButton.<init>(FloatingActionButton.java:109)
    at android.support.design.widget.FloatingActionButton.<init>(FloatingActionButton.java:105)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at org.jetbrains.android.uipreview.ViewLoader.createNewInstance(ViewLoader.java:458)
    at org.jetbrains.android.uipreview.ViewLoader.loadClass(ViewLoader.java:170)
    at org.jetbrains.android.uipreview.ViewLoader.loadView(ViewLoader.java:103)
    at com.android.tools.idea.rendering.LayoutlibCallbackImpl.loadView(LayoutlibCallbackImpl.java:168)
    at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:247)
    at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:171)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:835)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:70)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:811)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
    at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:223)
    at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:426)
    at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:350)
    at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:510)
    at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:498)
    at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:888)
    at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:498)
    at com.android.tools.idea.rendering.RenderTask.access$600(RenderTask.java:72)
    at com.android.tools.idea.rendering.RenderTask$3.call(RenderTask.java:610)
    at com.android.tools.idea.rendering.RenderTask$3.call(RenderTask.java:607)
    at com.android.tools.idea.rendering.RenderService.runRenderAction(RenderService.java:362)
    at com.android.tools.idea.rendering.RenderTask.render(RenderTask.java:607)
    at com.android.tools.idea.rendering.RenderTask.render(RenderTask.java:629)
    at com.intellij.android.designer.designSurface.AndroidDesignerEditorPanel$6.run(AndroidDesignerEditorPanel.java:480)
    at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:320)
    at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:310)
    at com.intellij.util.ui.update.MergingUpdateQueue$2.run(MergingUpdateQueue.java:254)
    at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:269)
    at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:227)
    at com.intellij.util.ui.update.MergingUpdateQueue.run(MergingUpdateQueue.java:217)
    at com.intellij.util.concurrency.QueueProcessor.runSafely(QueueProcessor.java:238)
    at com.intellij.util.Alarm$Request$1.run(Alarm.java:351)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)

这是我的XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.matt.myapplication.MainActivity"
    tools:showIn="@layout/activity_main">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
</RelativeLayout>

如果你需要它,我的.java

package com.example.matt.myapplication;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, 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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

如果您需要有关此问题的更多信息,请与我们联系。这是Android Studio的新安装

4 个答案:

答案 0 :(得分:8)

我在新安装的Android Studio时遇到了同样的问题。对build.gradle(Module:app)文件的最后几行的以下更改似乎已更正它。 (此更改基于查看最近未显示问题的示例项目):

原件:

compile 'com.android.support:appcompat-v7:23.2.0'
compile 'com.android.support:design:23.2.0'

更改为:

compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'

答案 1 :(得分:2)

首先,您需要在build.gradle中更改此行

compile 'com.android.support:design:23.2.0'

compile 'com.android.support:design:23.1.1'

然后转到:

Build -> Clean Project
Build -> Rebuild Project

以上步骤解决了我的问题......祝你好运。

答案 2 :(得分:0)

Rendering Problem
The following classes could not be instantiated:

android.support.desitn.widget.FloatingActionButton (open class, show exception, clear cache)

某处你有一个拼写错误,写了“desitn”而不是“design”(android.support。 desitn .widget.FloatingActionButton)。

答案 3 :(得分:0)

我猜你刚忘了发布了activity_main.xml文件,请确保该文件看起来像那样(由向导创建)

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

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email" />