Android Studio - 预览中缺少CustomView布局

时间:2014-02-24 11:08:28

标签: android android-studio custom-view

我是Android开发的初学者,面临着下面的问题,并没有得到任何解决方案:

在Android Studio 0.4.5中,我在项目中添加了一个CustomView。在编译项目并通过USB在我的设备上进行测试之后 - 一切似乎都很完美。除Android Studio外。

当我打开放置了我的CustomView的fragment.xml时,我在工作室的预览窗口中收到一条错误消息,我真的不知道发生了什么。我能够解决“问题”,但没有错(从我的角度来看)。

希望任何人都能理解我的问题。我敢肯定,只需对代码进行一些小改动就可以修复它; - )

这是我的XML(CustomView)

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:background="@color/background_default_card"
    android:padding="@dimen/spacing_default_card"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView_label"
        android:layout_weight="1"
        android:text="@string/label_wealthStatus"
        style="@style/text_default_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/textView_value"
        android:layout_weight="1"
        android:text="@string/dummy_value"
        style="@style/text_default_title"
        android:gravity="end"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

这是我的fragment.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="@drawable/shape_default_background"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.zjuhasz.PocketBudget.MainOverviewActivity$PlaceholderFragment">

    <ScrollView
        android:layout_below="@+id/customView_wealthStatus"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:id="@+id/linearLayout_root"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <include
                layout="@layout/layout_default_card"
                android:layout_marginTop="12dp"
                android:layout_margin="6dp"
                android:layout_width="match_parent"
                android:layout_height="50dp"/>

            <include
                layout="@layout/layout_default_card"
                android:layout_width="match_parent"
                android:layout_margin="6dp"
                android:layout_height="200dp"/>

            <include
                layout="@layout/layout_default_card"
                android:layout_width="match_parent"
                android:layout_margin="6dp"
                android:layout_height="400dp"/>



            </LinearLayout>


        </ScrollView>

<!-- here is my custom view snippet -->
    <com.zjuhasz.PocketBudget.CustomViews.WealthStatusView
        android:id="@+id/customView_wealthStatus"
        android:layout_width="match_parent"
        android:layout_height="100dp"/>

    <TextView
        android:layout_below="@+id/customView_wealthStatus"
        android:background="@drawable/shape_default_shadow_topdown"
        android:layout_width="match_parent"
        android:layout_height="6dp" />


</RelativeLayout>

我的CustomView代码

public class WealthStatusView extends LinearLayout {

private Context _context;
private LinearLayout _rootView;
private LayoutInflater _inflater;

private TextView _textView_Label;
private TextView _textView_Value;

public WealthStatusView(Context context) {
    super(context);
    init(context);
    createLayout();
}

public WealthStatusView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context);
    createLayout();
}

public WealthStatusView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init(context);
    createLayout();
}

private void init(Context context) {
    _context = context;
}

private void createLayout() {

    _inflater = (LayoutInflater) _context.getSystemService(_context.getApplicationContext().LAYOUT_INFLATER_SERVICE );

    if(_inflater != null) {
        _rootView = (LinearLayout)_inflater.inflate(com.zjuhasz.PocketBudget.R.layout.customview_wealth_status, this, true); // here is some error, I don't understand
        //_rootView = (LinearLayout)_inflater.inflate(R.layout.customview_wealth_status, null);

            _textView_Label = (TextView)_rootView.findViewById(com.zjuhasz.PocketBudget.R.id.textView_label);
            _textView_Value = (TextView)_rootView.findViewById(com.zjuhasz.PocketBudget.R.id.textView_value);


        set_fontFace();
    }
}

private void set_fontFace() {

//      Helper.set_fontFace(_textView_Label);
//      Helper.set_fontFace(_textView_Value);

}
}

Android Studio预览窗口中的错误消息

android.content.res.Resources$NotFoundException: Could not resolve resource value: 0x7F03001C.
    at android.content.res.BridgeResources.throwException(BridgeResources.java:693)
    at android.content.res.BridgeResources.getLayout(BridgeResources.java:271)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:395)
    at com.zjuhasz.PocketBudget.CustomViews.WealthStatusView.createLayout(WealthStatusView.java:51)
    at com.zjuhasz.PocketBudget.CustomViews.WealthStatusView.<init>(WealthStatusView.java:33)
    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:375)
    at org.jetbrains.android.uipreview.ViewLoader.loadView(ViewLoader.java:100)
    at com.android.tools.idea.rendering.ProjectCallback.loadView(ProjectCallback.java:165)
    at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:207)
    at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:135)
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:755)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:727)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:373)
    at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:399)
    at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:336)
    at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:332)
    at com.android.tools.idea.rendering.RenderService$3.compute(RenderService.java:548)
    at com.android.tools.idea.rendering.RenderService$3.compute(RenderService.java:537)
    at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:929)
    at com.android.tools.idea.rendering.RenderService.createRenderSession(RenderService.java:537)
    at com.android.tools.idea.rendering.RenderService.render(RenderService.java:609)
    at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager.doRender(AndroidLayoutPreviewToolWindowManager.java:575)
    at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager.access$1700(AndroidLayoutPreviewToolWindowManager.java:81)
    at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager$6$1.run(AndroidLayoutPreviewToolWindowManager.java:521)
    at com.intellij.openapi.progress.impl.ProgressManagerImpl$2.run(ProgressManagerImpl.java:178)
    at com.intellij.openapi.progress.ProgressManager.executeProcessUnderProgress(ProgressManager.java:209)
    at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:212)
    at com.intellij.openapi.progress.impl.ProgressManagerImpl.runProcess(ProgressManagerImpl.java:171)
    at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager$6.run(AndroidLayoutPreviewToolWindowManager.java:516)
    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:297)
    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:724)

0 个答案:

没有答案