Android Studio UI预览渲染问题

时间:2015-11-30 23:04:40

标签: android android-layout

我对android编程很新。我正在使用android studio 1.4,有一次我注意到UI预览不再适用于特定项目。它告诉我,我有渲染问题。我试着清理和重建,但它没有用。在其他项目或新项目中,我创建预览工作正常。任何想法如何解决这个问题。

我得到的堆栈跟踪是:

    android.content.res.Resources$NotFoundException
at com.android.layoutlib.bridge.android.BridgeContext.obtainStyledAttributes(BridgeContext.java:620)
at android.content.res.Resources_Theme_Delegate.obtainStyledAttributes(Resources_Theme_Delegate.java:71)
at android.content.res.Resources$Theme.obtainStyledAttributes(Resources.java:1512)
at android.widget.TextView.<init>(TextView.java:728)
at android.widget.Button.<init>(Button.java:109)
at android.widget.Button.<init>(Button.java:105)
at android.widget.Button.<init>(Button.java:101)
at sun.reflect.GeneratedConstructorAccessor111.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at android.view.LayoutInflater.createView(LayoutInflater.java:619)
at android.view.BridgeInflater.onCreateView(BridgeInflater.java:102)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:694)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:762)
at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:141)
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_Delegate.parseInclude(LayoutInflater_Delegate.java:197)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:879)
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:831)
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:366)
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:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

并且xml文件如下:

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

<TextView
    android:text="@string/select_technical_area"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_marginTop="10dp"
    android:id="@+id/technical_area"
    android:textSize="16dp" />
<Spinner
    android:id="@+id/spinner1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/technical_area"
    android:layout_alignParentStart="true"
    android:layout_alignEnd="@+id/telecommunication_center"></Spinner>

<TextView
    android:text="@string/select_telecommunication_center"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/telecommunication_center"
    android:layout_below="@+id/spinner1"
    android:layout_alignParentStart="true"
    android:layout_marginTop="20dp"
    android:textSize="16dp" />
<Spinner
    android:id="@+id/spinner2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/telecommunication_center"
    android:layout_alignParentStart="true"
    android:layout_alignEnd="@+id/telecommunication_center"></Spinner>>

<TextView
    android:text="Select Facility"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/KV"
    android:layout_below="@+id/spinner2"
    android:layout_alignParentStart="true"
    android:layout_marginTop="20dp"
    android:textSize="16dp" />
<Spinner
    android:id="@+id/spinner3"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/KV"
    android:layout_alignParentStart="true"
    android:layout_alignEnd="@+id/spinner2"></Spinner>

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Show Facility"
    android:id="@+id/button1"
    android:layout_below="@+id/spinner3"
    android:layout_alignParentStart="true"
    android:layout_marginTop="10dp"
    android:onClick="showClick"/>


<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Disarm Facility"
    android:id="@+id/button3"
    android:layout_below="@+id/button1"
    android:layout_alignParentStart="true"
    android:layout_marginTop="10dp"/>

<TextView
    android:text="@string/seyk"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/seyk"
    android:layout_below="@+id/button3"
    android:layout_alignParentStart="true"
    android:layout_marginTop="10dp"
    android:textSize="20dp"
    android:textColor="#000000"
    />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="SOS"
    android:id="@+id/button4"
    android:textSize="56dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentEnd="true"
    android:elegantTextHeight="true"
    android:textAlignment="center"
    android:longClickable="true"
    android:textAppearance="@android:drawable/alert_dark_frame"
    android:textStyle="bold"
    android:textColor="#FF0000" />

提前谢谢!!!

2 个答案:

答案 0 :(得分:0)

您必须将所有元素放在父RelativeLayout或更好(我认为)LinearLayout之类的父布局上。 因此,您的布局文件必须如下所示:

<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">


    <TextView
        android:text="@string/select_technical_area"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="10dp"
        android:id="@+id/technical_area"
        android:textSize="16dp" />
    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/technical_area"
        android:layout_alignParentStart="true"
        android:layout_alignEnd="@+id/telecommunication_center"></Spinner>

    <TextView
        android:text="@string/select_telecommunication_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/telecommunication_center"
        android:layout_below="@+id/spinner1"
        android:layout_alignParentStart="true"
        android:layout_marginTop="20dp"
        android:textSize="16dp" />
    <Spinner
        android:id="@+id/spinner2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/telecommunication_center"
        android:layout_alignParentStart="true"
        android:layout_alignEnd="@+id/telecommunication_center"></Spinner>>

    <TextView
        android:text="Select Facility"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/KV"
        android:layout_below="@+id/spinner2"
        android:layout_alignParentStart="true"
        android:layout_marginTop="20dp"
        android:textSize="16dp" />
    <Spinner
        android:id="@+id/spinner3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/KV"
        android:layout_alignParentStart="true"
        android:layout_alignEnd="@+id/spinner2"></Spinner>

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Facility"
        android:id="@+id/button1"
        android:layout_below="@+id/spinner3"
        android:layout_alignParentStart="true"
        android:layout_marginTop="10dp"
        android:onClick="showClick"/>


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Disarm Facility"
        android:id="@+id/button3"
        android:layout_below="@+id/button1"
        android:layout_alignParentStart="true"
        android:layout_marginTop="10dp"/>

    <TextView
        android:text="@string/seyk"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/seyk"
        android:layout_below="@+id/button3"
        android:layout_alignParentStart="true"
        android:layout_marginTop="10dp"
        android:textSize="20dp"
        android:textColor="#000000"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="SOS"
        android:id="@+id/button4"
        android:textSize="56dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"
        android:elegantTextHeight="true"
        android:textAlignment="center"
        android:longClickable="true"
        android:textAppearance="@android:drawable/alert_dark_frame"
        android:textStyle="bold"
        android:textColor="#FF0000" />

</LinearLayout>

如果您的屏幕需要滚动,那么您必须在ScrollView中添加所有这些内容。

答案 1 :(得分:0)

我遇到了同样的问题,我猜你有一个ImageBackground或者除了颜色作为背景之外的其他任何东西,你必须在你的xml文件中添加android android:popupBackground="#c5212121"到你的微调器,在这种情况下我把透明的黑色作为背景颜色,但你可以改变它,当然,你还必须将android:background="00ffffff"添加到你的微调器,它创建微调器的背景而不是下拉菜单,我已经设置了透明