java - AndroidPlot:LineAndPointFormatter 在传入 Context 和 Colors.xml 资源 ID 时导致崩溃

标签 java android android-fragments androidplot

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

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

我应该传入的地方:

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

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

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

所以我可以使用colors.xml资源ID并通过设置“colorID”然后将其传递来管理所有图形颜色(colorID在日志中显示为-65536或取决于的一些变体)。这段代码位于扩展 Fragment 的类中,所以我想知道上下文是否正确......

以下是我尝试过的一些方法:

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));

等等..

我会继续使用插件,但如果有人使用 LineAndPointFormatter 的 Context 版本,也许您遇到了类似的问题?

异常看起来像这样:

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

我必须将颜色重写为#ff0000ff 格式吗?

编辑:添加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>

最佳答案

我认为问题在于您使用的 xml 资源包含与“配置器”配置的对象无关的元素。配置器使用反射来确定如何将 xml 值映射到关联对象的成员。为此,xml 文件包含所配置对象的实际成员的元素并且这些元素与这些成员相同名称非常重要。

在上面的示例中,您尝试使用包含银色、灰色等不存在名称的元素的 xml 文件配置 LineAndPointFormatter。相反,您应该将配置存储在一个单独的 xml 文件中,如下所示:

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

一旦你定义了这个文件,你可以尝试(我个人没有尝试过这个,但假设它应该有效)是引用colors.xml中的值,这样当你调整颜色灰色的定义时,全局调整。

关于java - AndroidPlot:LineAndPointFormatter 在传入 Context 和 Colors.xml 资源 ID 时导致崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16551799/

相关文章:

android - 从 Activity 完成异步后在 Fragment 上填充 ListView

java - Spring架构问题: is Service needed?

android - 如何区分Android中不同布局文件夹中的布局文件(.xml文件)

java - 带有外部相机应用程序的android相机 Intent ,返回原始 Activity ?

java - 将位图图像转换为文件并传递给 FileInputStream?

android - 从子 fragment 本身动态替换子 fragment

java - 尝试/捕捉无限循环?

java - 如何在android中重新安装appcompatv7_11包?

java - 使 EntityManager 可序列化

android - 底部的元素使用 AppBarLayout 在导航栏后面绘制元素