无法从colors.xml文件中设置颜色

时间:2015-01-20 14:44:27

标签: android xml

为了将所有颜色保存在一个地方我将所有颜色复制到colors.xml文件中,

<resources>

    <color name="footerColor">#FF607675</color>
    <color name="activityBackground">#FFB7D4E1</color>
    <color name="listView1">#FFB7D3E1</color>
    <color name="listView2">#FFC5DCE7</color>
    <color name="title">#AA4C4E44</color>
    <color name="subTitle">#FF013C3B</color>

</resources>

但是当我在.java文件中设置此颜色时,它们无法正常工作,

像这样,

date.setTextColor(color.subTitle);

但是如果我对颜色进行硬编码那么它就可以了,

date.setTextColor(0xFF013C3B);

我也试过像这样提供我的资源文件的完整路径,但这不起作用

date.setTextColor(com.news.testapp.R.color.subTitle)

它没有显示错误或任何其他内容,但它也无法正常工作。 我做错了什么?

更新

我尝试了答案解决方案并做到了,

context.getResources().getColor(R.color.subTitle);

其中context是构造函数中指定的类的context

Context context;
    public myAdapter(Context context, ArrayList<HashMap<String, Object>> fetchedData, int resource, String[] from, int[] to) {
        super(context, fetchedData, resource, from, to);
        this.context = context;
    }

但现在我收到java.lang.NullPointerException错误。

更新2:

这是完整的代码,

public class SpecialAdapter extends SimpleAdapter {
    Context context;

    public SpecialAdapter(Context context,
            ArrayList<HashMap<String, Object>> fetchedData, int resource,
            String[] from, int[] to) {

        super(context, fetchedData, resource, from, to);
        this.context = context;
    }

    private int[] colors = {
            context.getResources().getColor(R.color.subTitle),
            context.getResources().getColor(R.color.title) };


}

这是logcat的一些输出,

01-20 20:54:40.164: E/AndroidRuntime(3269): FATAL EXCEPTION: main
01-20 20:54:40.164: E/AndroidRuntime(3269): java.lang.NullPointerException
01-20 20:54:40.164: E/AndroidRuntime(3269):     at com.news.myApp.SpecialAdapter.<init>(SpecialAdapter.java:25)
01-20 20:54:40.164: E/AndroidRuntime(3269):     at com.news.myApp.myApp$GetNews.addUpdateData(myApp.java:358)
01-20 20:54:40.164: E/AndroidRuntime(3269):     at com.news.myApp.myApp$GetNews.onPostExecute(myApp.java:331)
01-20 20:54:40.164: E/AndroidRuntime(3269):     at com.news.myApp.myApp$GetNews.onPostExecute(myApp.java:1)

当我删除此行时,

private int[] colors = {
            context.getResources().getColor(R.color.subTitle),
            context.getResources().getColor(R.color.title) };

然后没有NullPointerException错误。

2 个答案:

答案 0 :(得分:1)

color.subTitle指向无处,不存在。

  1. setTextColor()需要Color
  2. 使用Context.getResources()访问res/文件夹
  3. 中的资源

    所以请使用Context.getResources().getColor(R.color.subTitle)

    在您的代码中:

    date.setTextColor(getResources().getColor(R.color.subTitle));

答案 1 :(得分:1)

回复:更新2:

public class SpecialAdapter extends SimpleAdapter {
    Context context;

    public SpecialAdapter(Context context,
            ArrayList<HashMap<String, Object>> fetchedData, int resource,
            String[] from, int[] to) {

        super(context, fetchedData, resource, from, to);
        this.context = context;
    }

    private int[] colors = {
            context.getResources().getColor(R.color.subTitle),
            context.getResources().getColor(R.color.title) };
}

无法按照您的方式初始化数组。当int [] colors初始化时,context为null。您可以在运行时将上下文传递给构造函数。

将上述代码改为:

public class SpecialAdapter extends SimpleAdapter {
        Context context;

        public SpecialAdapter(Context context,
                ArrayList<HashMap<String, Object>> fetchedData, int resource,
                String[] from, int[] to) {

            super(context, fetchedData, resource, from, to);
            this.context = context;
            colors[0] = context.getResources().getColor(R.color.subTitle);
            colors[1] = context.getResources().getColor(R.color.title);
        }

        private int[] colors = new int[2];
    }