如何使用view.isInEditMode()在Android Studio的编辑器中查看带有自定义View的布局XML?

时间:2016-03-25 00:33:20

标签: java android android-layout

我正在尝试编辑应用。当我编辑布局XML时,IDE(Android Studio)说:

  

无法实例化以下类:      - com.integreight.onesheeld.utils.customviews.OneSheeldTextView(Open Class,Show Exception,Clear Cache)提示:使用View.isInEditMode()   在您的自定义视图中跳过

但我不知道该怎么做。

这是XML文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/superDarkGray"
android:clickable="true">

    <ImageView
        android:id="@+id/speaker_fragment_background_imageview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:layout_marginBottom="20dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="20dp"
        android:background="@drawable/curved_back_layer"
        android:contentDescription="@string/app_name"
        android:scaleType="fitXY" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="20dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="20dp">

        <com.integreight.onesheeld.utils.customviews.OneSheeldTextView
            android:id="@+id/shieldName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginLeft="50dp"
            android:layout_marginRight="50dp"
            android:padding="5dp"
            android:singleLine="true"
            android:textColor="@color/offWhite"
            android:textSize="27sp" />

        <FrameLayout
            android:id="@+id/shieldsContainerFrame"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/shieldName"></FrameLayout>
    </RelativeLayout>

    <CheckBox
        android:id="@+id/isMenuOpening"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="25dp"
        android:layout_marginLeft="15dp"
        android:background="@drawable/shield_docking_checkbox_selelctor"
        android:button="@null" />

    <ToggleButton
        android:id="@+id/shieldStatus"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="15dp"
        android:layout_marginTop="25dp"
        android:background="@drawable/shield_status_toggle_selelctor"
        android:checked="true"
        android:textOff=""
        android:textOn="" />

    <com.integreight.onesheeld.utils.customviews.MultiDirectionSlidingDrawer xmlns:my="http://schemas.android.com/apk/res/com.integreight.onesheeld"
        android:id="@+id/pinsViewSlidingView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        my:content="@id/pinsViewContainer"
        my:direction="rightToLeft"
        my:handle="@id/handler">

        <Button
            android:id="@+id/handler"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:visibility="invisible" />

        <FrameLayout
            android:id="@+id/pinsViewContainer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="end"
            android:background="#D9000000"></FrameLayout>
    </com.integreight.onesheeld.utils.customviews.MultiDirectionSlidingDrawer>

    <com.integreight.onesheeld.utils.customviews.MultiDirectionSlidingDrawer xmlns:my="http://schemas.android.com/apk/res/com.integreight.onesheeld"
        android:id="@+id/settingsSlidingView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        my:content="@id/settingsViewContainer"
        my:direction="rightToLeft"
        my:handle="@id/settingsHandler">

        <Button
            android:id="@+id/settingsHandler"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:visibility="invisible" />

        <FrameLayout
            android:id="@+id/settingsViewContainer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="end"
            android:background="#D9000000"></FrameLayout>
    </com.integreight.onesheeld.utils.customviews.MultiDirectionSlidingDrawer>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/pinsFixedHandler"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/connect_button"
            android:contentDescription="@string/app_name" />

        <ImageView
            android:id="@+id/settingsFixedHandler"
            android:layout_width="match_parent"
            android:layout_height="110dp"
            android:layout_marginTop="15dp"
            android:background="#D9000000"
            android:contentDescription="@string/app_name"
            android:src="@drawable/settings"
            android:visibility="gone" />
    </LinearLayout>

</RelativeLayout>

,Java代码是:

package com.integreight.onesheeld.utils.customviews;

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

import com.integreight.onesheeld.OneSheeldApplication;

public class OneSheeldTextView extends TextView {

    public OneSheeldTextView(Context context, AttributeSet attrs) {

        super(context, attrs);
        if(!isInEditMode())
        setTypeface(
                ((OneSheeldApplication) context.getApplicationContext()).appFont,
                getTypeface() == null ? Typeface.NORMAL : getTypeface()
                        .getStyle() == Typeface.BOLD ? Typeface.BOLD
                        : Typeface.NORMAL);
        setPadding(getPaddingLeft(),
                getPaddingTop()
                        - ((int) (1.5 * context.getResources()
                        .getDisplayMetrics().density + .5f)),
                getPaddingRight(), getBottom());
    }

    public OneSheeldTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setTypeface(
                ((OneSheeldApplication) context.getApplicationContext()).appFont,
                getTypeface() == null ? Typeface.NORMAL : getTypeface()
                        .getStyle() == Typeface.BOLD ? Typeface.BOLD
                        : Typeface.NORMAL);
        setPadding(getPaddingLeft(),
                getPaddingTop()
                        - ((int) (1.5 * context.getResources()
                        .getDisplayMetrics().density + .5f)),
                getPaddingRight(), getBottom());
    }

    public OneSheeldTextView(Context context) {
        super(context);
        if(!isInEditMode())

        setTypeface(
                ((OneSheeldApplication) context.getApplicationContext()).appFont,
                getTypeface() == null ? Typeface.NORMAL : getTypeface()
                        .getStyle() == Typeface.BOLD ? Typeface.BOLD
                        : Typeface.NORMAL);
        setPadding(getPaddingLeft(),
                getPaddingTop()
                        - ((int) (1.5 * context.getResources()
                        .getDisplayMetrics().density + .5f)),
                getPaddingRight(), getBottom());
    }
}

和例外细节:

java.lang.ClassCastException: com.android.layoutlib.bridge.android.BridgeContext cannot be cast to com.integreight.onesheeld.OneSheeldApplication
at com.integreight.onesheeld.utils.customviews.OneSheeldTextView.<init>(OneSheeldTextView.java:15)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
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.rInflate_Original(LayoutInflater.java:838)
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 org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager.doRender(AndroidLayoutPreviewToolWindowManager.java:652)
at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager.access$1700(AndroidLayoutPreviewToolWindowManager.java:80)
at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager$7$1.run(AndroidLayoutPreviewToolWindowManager.java:594)
at com.intellij.openapi.progress.impl.CoreProgressManager$2.run(CoreProgressManager.java:152)
at com.intellij.openapi.progress.impl.CoreProgressManager.registerIndicatorAndRun(CoreProgressManager.java:452)
at com.intellij.openapi.progress.impl.CoreProgressManager.executeProcessUnderProgress(CoreProgressManager.java:402)
at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:54)
at com.intellij.openapi.progress.impl.CoreProgressManager.runProcess(CoreProgressManager.java:137)
at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager$7.run(AndroidLayoutPreviewToolWindowManager.java:589)
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)

0 个答案:

没有答案