AndroidPlot: LineAndPointFormatter causing crash when passing in Context and colors.xml resource ID

拥有回忆 提交于 2019-12-13 04:28:48

问题


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>

回答1:


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.



来源:https://stackoverflow.com/questions/16551799/androidplot-lineandpointformatter-causing-crash-when-passing-in-context-and-col

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!