Resources$NotFoundException in Graphical Layout ADT preview (but app actually Works)

前端 未结 2 551
醉酒成梦
醉酒成梦 2020-12-17 01:02

My problem is that loading an array of strings defined in XML works in the app but will result in an error in the ADT Graphical Layout preview.

Now I can\'t see any

相关标签:
2条回答
  • 2020-12-17 01:32

    Your code is alright but unfortunately there are still some bugs in ADT plugin and there is one of them. Layout Editor has troubles with rendering custom views. I had the same issue and the only workout I have found is checking View.isInEditMode and initializing int array in some other way but not from resources. So your code will look like this:

    int score_vals[];
    if (isInEditMode()) {
        score_vals = { 10, 20, 50 };
    } else {
        score_vals = getResources().getIntArray(R.array.score_vals);
    }
    

    And by the way don't create or load any resources in your onDraw methods. I suppose getResources().getIntArray uses some sort of caching but anyway your perfomance may suffer.

    0 讨论(0)
  • 2020-12-17 01:37

    I found a kind of a workaround whereby you have to hijack android's own attributes to get access to resources in the designer.

    The following should provide the idea, but you would have to find a native android property of type int[]

    This custom view XML should render in the graphical layout preview while using resources

    <!-- Could override individual attributes here too rather than using a style -->
    <com.github.espiandev.showcaseview.ShowcaseView
         style="@style/ShowcaseView"/>
    

    styles.xml - Style specifying some of the resources to use

    <style name="ShowcaseView" parent="match_fill">
        <!--# Cling drawable -->
        <item name="android:src">@drawable/cling</item>
        <!--# Title #-->
        <item name="android:contentDescription">@string/showcase_title</item>
        <!--# Description #-->
        <item name="android:description">@string/showcase_description</item>
        <!--# Button Text #-->
        <item name="android:text">@string/ok</item>
        <item name="sv_titleTextColor">#33B5E5</item>
        <item name="sv_detailTextColor">#FFFFFF</item>
        <item name="sv_backgroundColor">#3333B5E5</item>
        <item name="sv_buttonBackgroundColor">#3333B5E5</item>
        <item name="sv_buttonForegroundColor">#33B5E5</item>
    </style>
    

    attrs.xml - Custom attribute definition compatible with design-time preview

    <!-- The android attrs assume the corresponding android format / data type --> 
    <declare-styleable name="ShowcaseView">
        <!--# Cling drawable -->
        <attr name="android:src"/>
        <!--# Title #-->
        <attr name="android:contentDescription"/>
        <!--# Description #-->
        <attr name="android:description"/>
        <!--# Button Text #-->
        <attr name="android:text"/>
        <attr name="sv_backgroundColor" format="color|reference" />
        <attr name="sv_detailTextColor" format="color|reference" />
        <attr name="sv_titleTextColor" format="color|reference" />
        <attr name="sv_buttonBackgroundColor" format="color|reference" />
        <attr name="sv_buttonForegroundColor" format="color|reference" />
    </declare-styleable>
    

    ShowcaseView.java - Using the custom attributes in the custom view

    public ShowcaseView(Context context) {
        this(context, null, R.styleable.CustomTheme_showcaseViewStyle);
    }
    
    public ShowcaseView(Context context, AttributeSet attrs) {
        this(context, attrs, R.styleable.CustomTheme_showcaseViewStyle);
    }
    
    public ShowcaseView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // Get the attributes for the ShowcaseView
        final TypedArray styled = context.getTheme().obtainStyledAttributes(attrs, R.styleable.ShowcaseView, 0, 0);
        showcase = styled.getDrawable(R.styleable.ShowcaseView_android_src);
        titleText = styled.getString(R.styleable.ShowcaseView_android_contentDescription);
        subText = styled.getString(R.styleable.ShowcaseView_android_description);
        buttonText = styled.getString(R.styleable.ShowcaseView_android_text);
        backColor = styled.getInt(R.styleable.ShowcaseView_sv_backgroundColor, Color.argb(128, 80, 80, 80));
        detailTextColor = styled.getColor(R.styleable.ShowcaseView_sv_detailTextColor, Color.WHITE);
        titleTextColor = styled.getColor(R.styleable.ShowcaseView_sv_titleTextColor, Color.parseColor("#49C0EC"));
        styled.recycle();
        // Now make use of the fields / do further initialization ..
    }
    
    0 讨论(0)
提交回复
热议问题