Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 不能';无法从colors.xml文件设置颜色_Android_Xml - Fatal编程技术网

Android 不能';无法从colors.xml文件设置颜色

Android 不能';无法从colors.xml文件设置颜色,android,xml,Android,Xml,为了将所有颜色保存在一个地方,我将所有颜色复制到colors.xml文件中 <resources> <color name="footerColor">#FF607675</color> <color name="activityBackground">#FFB7D4E1</color> <color name="listView1">#FFB7D3E1</color> <

为了将所有颜色保存在一个地方,我将所有颜色复制到
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>
但是如果我硬编码颜色,那么它就工作了

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;
    }
这是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)

然后就没有
NullPointerException
错误。

颜色。subTitle
不指向任何地方,也不存在

  • setTextColor()
    需要
    Color
  • 使用
    Context.getResources()
    res/
    文件夹访问资源
  • 因此,请使用
    Context.getResources().getColor(R.color.subTitle)

    在您的代码中:

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

    关于:更新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) };
    }
    
    公共类SpecialAdapter扩展了SimpleAdapter{
    语境;
    公共SpecialAdapter(上下文,
    ArrayList获取数据,int资源,
    字符串[]从,整数[]到){
    super(上下文、获取数据、资源、发件人、收件人);
    this.context=上下文;
    }
    私有int[]颜色={
    context.getResources().getColor(R.color.subTitle),
    context.getResources().getColor(R.color.title)};
    }
    
    无法按您的方式初始化数组。初始化int[]颜色时,上下文为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];
        }
    
    公共类SpecialAdapter扩展了SimpleAdapter{
    语境;
    公共SpecialAdapter(上下文,
    ArrayList获取数据,int资源,
    字符串[]从,整数[]到){
    super(上下文、获取数据、资源、发件人、收件人);
    this.context=上下文;
    colors[0]=context.getResources().getColor(R.color.subTitle);
    colors[1]=context.getResources().getColor(R.color.title);
    }
    私有int[]颜色=新int[2];
    }
    
    这个怎么样>
    com.news.testapp.R.color
    ?它存在但也不工作。另外,您的代码不起作用,因为java文件可能不是我的主类。
    无法从类型上下文中静态引用非静态方法getResources()。看看我的编辑,我包括了你需要使用的代码
    Context.getResource()
    仅供参考…我尝试了您的解决方案,但没有上下文,所以在搜索stackoverflow后,我学会了如何使用构造函数创建上下文,所以我做到了。但现在我得到了nullpointerexception错误。请查看更新的问题?我已经添加了logcat输出和整个代码,请看一看谢谢,但是这一行
    public int[]colors=new int[2]
    在构造函数之后,因此它将如何在构造函数之前执行?我建议你买一本关于Java基础知识、变量初始化和REST的书,我正在从这本书中学到先生,你能告诉我这种现象叫什么吗?所以我可以研究它?
    private int[]colors
    是一个私有变量<代码>[]
    表示它是数组。给你一些链接
    private int[] colors = {
                context.getResources().getColor(R.color.subTitle),
                context.getResources().getColor(R.color.title) };
    
    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) };
    }
    
    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];
        }