Android Studio库模块无法生成R.java

时间:2015-11-07 10:07:14

标签: android

我编写了一个其他模块可以依赖的库模块。 我创建了一个库模块并在其中编写了一些代码。 这是一个自定义视图。我像这样创建attr.xml。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="top_icon" format="reference" />
    <attr name="bottom_icon" format="reference" />

    <declare-styleable name="GradientIconView">
        <attr name="top_icon" />
        <attr name="bottom_icon" />
    </declare-styleable>

    <attr name="text" format="string" />
    <attr name="text_size" format="dimension" />
    <attr name="top_text_color" format="color" />
    <attr name="bottom_text_color" format="color" />

    <declare-styleable name="GradientTextView">
        <attr name="text" />
        <attr name="text_size" />
        <attr name="top_text_color" />
        <attr name="bottom_text_color" />
    </declare-styleable>
</resources>

我写了这样的代码。

TypedArray a = context.obtainStyledAttributes(attrs,
            R.styleable.GradientIconView);

    BitmapDrawable drawable;

    int n = a.getIndexCount();
    for (int i = 0; i < n; i++) {

        int attr = a.getIndex(i);
        switch (attr) {
            case R.styleable.GradientIconView_top_icon:
                drawable = (BitmapDrawable) a.getDrawable(attr);
                setTopIconView(drawable);
                break;
            case R.styleable.GradientIconView_bottom_icon:
                drawable = (BitmapDrawable) a.getDrawable(attr);
                setBottomIconView(drawable);
                break;

        }
    }

它说无法解析符号&#39; R&#39;,我重建项目并制作库模块。它不起作用。 这是项目结构。 Structure about project

2 个答案:

答案 0 :(得分:1)

我发现问题所在。这不是R.java没有生成的。它只是R.styleable。***不能在Android库模块的switch语句中使用。

从ADT 14开始,图书馆项目中的资源常量不再是最终的。 所以我改变了我的代码。它运作良好。

TypedArray a = context.obtainStyledAttributes(attrs,
            R.styleable.GradientIconView);

    BitmapDrawable drawable;

    int n = a.getIndexCount();
    for (int i = 0; i < n; i++) {

        int attr = a.getIndex(i);
        if (attr == R.styleable.GradientIconView_top_icon) {
            drawable = (BitmapDrawable) a.getDrawable(attr);
            setTopIconView(drawable);
            break;
        }
        else if(attr == R.styleable.GradientIconView_bottom_icon) {
                drawable = (BitmapDrawable) a.getDrawable(attr);
                setBottomIconView(drawable);
                break;
        }
    }

    a.recycle();

最后,我自己解决了这个问题:)

答案 1 :(得分:0)

试试这个: -Delete文件夹YourApp / build,YourApp / app / build和所有文件.iml -Restart android studio和clean-rebuild 希望它能帮到你

相关问题