绑定适配器无效元素问题

时间:2017-01-21 21:53:09

标签: android data-binding mpandroidchart android-databinding

@BindingAdapter合作一直运作良好。我得到一个不寻常的问题,我没有看到如何解决它的明确路径。

堆栈跟踪

@BindingAdapter on invalid element: setPieChartData(com.github.mikephil.charting.charts.PieChart,java.util.HashMap<java.lang.String,java.lang.Integer>,java.lang.String)
    at com.sun.tools.javac.main.Main.compile(Main.java:553)
    at com.sun.tools.javac.api.JavacTaskImpl.doCall(JavacTaskImpl.java:129)
    at com.sun.tools.javac.api.JavacTaskImpl.call(JavacTaskImpl.java:138)
    at org.gradle.api.internal.tasks.compile.JdkJavaCompiler.execute(JdkJavaCompiler.java:46)
    at org.gradle.api.internal.tasks.compile.JdkJavaCompiler.execute(JdkJavaCompiler.java:33)
    at org.gradle.api.internal.tasks.compile.NormalizingJavaCompiler.delegateAndHandleErrors(NormalizingJavaCompiler.java:104)
    at org.gradle.api.internal.tasks.compile.NormalizingJavaCompiler.execute(NormalizingJavaCompiler.java:53)
    at org.gradle.api.internal.tasks.compile.NormalizingJavaCompiler.execute(NormalizingJavaCompiler.java:38)
    at org.gradle.api.internal.tasks.compile.CleaningJavaCompilerSupport.execute(CleaningJavaCompilerSupport.java:35)
    at org.gradle.api.internal.tasks.compile.CleaningJavaCompilerSupport.execute(CleaningJavaCompilerSupport.java:25)
    at org.gradle.api.tasks.compile.JavaCompile.performCompilation(JavaCompile.java:206)
    at org.gradle.api.tasks.compile.JavaCompile.compile(JavaCompile.java:187)
    at org.gradle.api.tasks.compile.JavaCompile.compile(JavaCompile.java:130)
    at com.android.build.gradle.tasks.factory.AndroidJavaCompile.compile(AndroidJavaCompile.java:49)
    at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:73)
    at org.gradle.api.internal.project.taskfactory.DefaultTaskClassInfoStore$IncrementalTaskAction.doExecute(DefaultTaskClassInfoStore.java:163)
    at org.gradle.api.internal.project.taskfactory.DefaultTaskClassInfoStore$StandardTaskAction.execute(DefaultTaskClassInfoStore.java:134)
    at org.gradle.api.internal.project.taskfactory.DefaultTaskClassInfoStore$StandardTaskAction.execute(DefaultTaskClassInfoStore.java:123)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:95)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:76)

我的功能

@BindingAdapter({"bind:alternatives","bind:topic"})
private static void setPieChartData(PieChart pieChart, HashMap<String,Integer> alternatives, String topic) {
    PieChartHelper.initializePieChart(pieChart,alternatives,topic);
}

我的XML布局

     <com.github.mikephil.charting.charts.PieChart
        android:id="@+id/pie_chart"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        app:alternatives="@{gameFeedItem.alternatives}"
        app:topic="@{gameFeedItem.topic}"/>

奇怪的是,我为其他观点做了类似的解决方案。但这是唯一一个完整的自定义视图。那么PieChart类不能以这种方式支持数据绑定吗?

使用的库可以在https://github.com/PhilJay/MPAndroidChart

找到

1 个答案:

答案 0 :(得分:11)

您的BindingAdapter采用private方法。设为public

@BindingAdapter({"bind:alternatives","bind:topic"})
public static void setPieChartData(PieChart pieChart, HashMap<String,Integer> alternatives, String topic) {
    PieChartHelper.initializePieChart(pieChart,alternatives,topic);
}