AndroidPlot:通过在上下文和colors.xml资源ID时LineAndPointFormatter导致崩溃 [英] AndroidPlot: LineAndPointFormatter causing crash when passing in Context and colors.xml resource ID

查看:455
本文介绍了AndroidPlot:通过在上下文和colors.xml资源ID时LineAndPointFormatter导致崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图避免使用RGB值,所以这个方法:

 公共LineAndPointFormatter(整数lineColor,整型顶点颜色,整数填充颜色){
     这个(lineColor,顶点颜色,填充颜色,FillDirection.BOTTOM);
}

在这里我应该传递:

  LineAndPointFormatter LPF =新LineAndPointFormatter(Color.rgb(0,0,200),空,Color.rgb(0,0,80));

时,工作得很好,但我想用这个方法:

 公共LineAndPointFormatter(上下文CTX,诠释xmlCfgId){
    从这个派生类// prevent配置:
    如果(的getClass()。等于(LineAndPointFormatter.class)){
        Configurator.configure(CTX,对此,xmlCfgId);
    }
}

这样我就可以使用colors.xml资源ID,并通过设置colorID,然后顺便后来管理所有的图表颜色(colorID被显示为在日志中-65536或取决于一些变化)。这code是的片段扩展一个类里面,所以我想知道如果我得到了正确的上下文。

下面是一些我尝试过的事情:

  LineAndPointFormatter seriesFormat =新LineAndPointFormatter(getActivity()getApplicationContext(),getResources()的getColor(R.color.blue));LineAndPointFormatter seriesFormat =新LineAndPointFormatter(getActivity()getApplicationContext(),colorID。);LineAndPointFormatter seriesFormat =新LineAndPointFormatter(getActivity()getApplicationContext(),getResources()的getColor(colorID));

等..

我会继续撑着了,但如果有人使用LineAndPointFormatter背景下的版本,也许你已经遇到了类似的问题?

唯一的例外是这样的:

12月5日至14日:47:25.917:E / AndroidRuntime(14023):$ android.content.res.Resources NotFoundException:资源ID#0xff0000ff

我一定要我的色彩改写成#FF0000FF格式??

编辑:添加colors.xml

 <?XML版本=1.0编码=UTF-8&GT?;
<资源>
<颜色名称=白色>#FFFFFF< /彩色>
<颜色名称=transparent_white>#66FFFFFF< /彩色>
<颜色名称=黄色>#FFFF00< /彩色>
<颜色名称=倒挂金钟>#FF00FF< /彩色>
<颜色名称=红>#FF0000< /彩色>
<颜色名称=银>#C0C0C0< /彩色>
<颜色名称=灰色>#808080< /彩色>
<颜色名称=橄榄型>#808000< /彩色>
<颜色名称=紫色>#800080< /彩色>
<颜色名称=栗色>#800000< /彩色>
<颜色名称=AQUA>#00FFFF< /彩色>
<颜色名称=石灰>#00FF00< /彩色>
<颜色名称=水鸭>#008080< /彩色>
<颜色名称=绿色>#008000< /彩色>
<颜色名称=蓝色>#0000FF< /彩色>
<颜色名称=海军>#000080< /彩色>
<颜色名称=黑>#000000< /彩色>
< /资源>


解决方案

我认为这个问题是您正在使用,其中包含的配置器被配置不相关的对象元素的XML资源。配置使用反射弄清楚如何将XML值映射到相关对象的成员。为此,重要的是xml文件的包含对象的实际成员被配置元素,而这些元素共享的相同这些成员的名字。

在你上面的例子您尝试使用含有如银的名字元素的XML文件来配置一个LineAndPointFormatter,灰色等,它们不存在。相反,你应该你的配置存储在一个单独的XML文件看起来像这样:

 <?XML版本=1.0编码=UTF-8&GT?;
&所述;配置vertexPaint.color =#00FF00
        linePaint.color =#00FF00
        linePaint.strokeWidth =2DP/>

一旦你定义了这个文件的东西,你可以试试(我没有亲自试过,但认为它应该工作)就是这样,当您调整颜色灰白例如定义引用colors.xml你的价值观,它在全球范围内调整。

I'm trying to avoid using rgb values, so this method:

public LineAndPointFormatter(Integer lineColor, Integer vertexColor, Integer fillColor) {
     this(lineColor, vertexColor, fillColor, FillDirection.BOTTOM);
}

where I'm supposed to pass in:

LineAndPointFormatter lpf = new LineAndPointFormatter(Color.rgb(0, 0, 200), null, Color.rgb(0, 0, 80));

is working just fine, but I want to use this method:

public LineAndPointFormatter(Context ctx, int xmlCfgId) {
    // prevent configuration of classes derived from this one:
    if (getClass().equals(LineAndPointFormatter.class)) {
        Configurator.configure(ctx, this, xmlCfgId);
    }
}

so I can use the colors.xml resource ID and manage all graph colors by setting "colorID" then passing it in later (colorID is showing up as -65536 in the log or some variation of that depending on ). This code is inside a class that's extending Fragment, so I'm wondering if I'm getting the Context correct..

Here are some of the things I've tried:

LineAndPointFormatter seriesFormat = new LineAndPointFormatter(getActivity().getApplicationContext(), getResources().getColor(R.color.blue));

LineAndPointFormatter seriesFormat = new LineAndPointFormatter(getActivity().getApplicationContext(), colorID);

LineAndPointFormatter seriesFormat = new LineAndPointFormatter(getActivity().getApplicationContext(), getResources().getColor(colorID));

and so on..

I'll keep pluggin away, but if anyone is using the Context version of LineAndPointFormatter, maybe you've run into a similar problem?

The exception looks like this:

05-14 12:47:25.917: E/AndroidRuntime(14023): android.content.res.Resources$NotFoundException: Resource ID #0xff0000ff

Do I have to rewrite my colors into the #ff0000ff format??

EDIT: Added colors.xml

<?xml version="1.0" encoding="UTF-8"?>
<resources>
<color name="white">#FFFFFF</color>
<color name="transparent_white">#66FFFFFF</color>
<color name="yellow">#FFFF00</color>
<color name="fuchsia">#FF00FF</color>
<color name="red">#FF0000</color>
<color name="silver">#C0C0C0</color>
<color name="gray">#808080</color>
<color name="olive">#808000</color>
<color name="purple">#800080</color>
<color name="maroon">#800000</color>
<color name="aqua">#00FFFF</color>
<color name="lime">#00FF00</color>
<color name="teal">#008080</color>
<color name="green">#008000</color>
<color name="blue">#0000ff</color>
<color name="navy">#000080</color>
<color name="black">#000000</color>
</resources>

解决方案

I think the problem is that you are using an xml resource which contains elements not related to the object being configured by the "configurator". Configurator uses reflection to figure out how to map your xml values to the members of the associated object. To this end, it is important that xml file only contain elements for actual members of the object being configured and that those elements share the same name of those members.

In your example above you are trying to configure a LineAndPointFormatter using an xml file containing elements with names like silver, gray etc. which do not exist. Instead you should store your config in a separate xml file looking something like this:

<?xml version="1.0" encoding="utf-8"?>
<config vertexPaint.color="#00FF00"
        linePaint.color="#00FF00"
        linePaint.strokeWidth="2dp"/>

Once you've defined this file something you can try (I've not personally tried this but assume it should work) is to reference your values in colors.xml so that when you adjust your definition of the color gray for example, it is adjusted globally.

这篇关于AndroidPlot:通过在上下文和colors.xml资源ID时LineAndPointFormatter导致崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆