android问题中的圆环图

时间:2013-12-12 08:51:52

标签: android donut-chart

嗨我想在android中制作圆环图。我得到了一个很好的例子。我添加了库,项目显示没有错误。但是在运行时,它在logcat中给出了以下错误。

12-12 08:46:53.083: E/AndroidRuntime(1467): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.artfulbits.aiCharts.DoughnutSample/com.artfulbits.aiCharts.DoughnutSample.MainActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class com.artfulbits.aiCharts.ChartView
12-12 08:46:53.083: E/AndroidRuntime(1467):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-12 08:46:53.083: E/AndroidRuntime(1467):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-12 08:46:53.083: E/AndroidRuntime(1467):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-12 08:46:53.083: E/AndroidRuntime(1467):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-12 08:46:53.083: E/AndroidRuntime(1467):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-12 08:46:53.083: E/AndroidRuntime(1467):     at android.os.Looper.loop(Looper.java:137)
12-12 08:46:53.083: E/AndroidRuntime(1467):     at android.app.ActivityThread.main(ActivityThread.java:5041)
12-12 08:46:53.083: E/AndroidRuntime(1467):     at java.lang.reflect.Method.invokeNative(Native Method)
12-12 08:46:53.083: E/AndroidRuntime(1467):     at java.lang.reflect.Method.invoke(Method.java:511)
12-12 08:46:53.083: E/AndroidRuntime(1467):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-12 08:46:53.083: E/AndroidRuntime(1467):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-12 08:46:53.083: E/AndroidRuntime(1467):     at dalvik.system.NativeStart.main(Native Method)
12-12 08:46:53.083: E/AndroidRuntime(1467): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class com.artfulbits.aiCharts.ChartView
12-12 08:46:53.083: E/AndroidRuntime(1467):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:698)
12-12 08:46:53.083: E/AndroidRuntime(1467):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
12-12 08:46:53.083: E/AndroidRuntime(1467):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
12-12 08:46:53.083: E/AndroidRuntime(1467):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
12-12 08:46:53.083: E/AndroidRuntime(1467):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
12-12 08:46:53.083: E/AndroidRuntime(1467):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
12-12 08:46:53.083: E/AndroidRuntime(1467):     at android.app.Activity.setContentView(Activity.java:1881)
12-12 08:46:53.083: E/AndroidRuntime(1467):     at com.artfulbits.aiCharts.DoughnutSample.MainActivity.onCreate(MainActivity.java:18)
12-12 08:46:53.083: E/AndroidRuntime(1467):     at android.app.Activity.performCreate(Activity.java:5104)
12-12 08:46:53.083: E/AndroidRuntime(1467):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-12 08:46:53.083: E/AndroidRuntime(1467):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-12 08:46:53.083: E/AndroidRuntime(1467):     ... 11 more

并且主要活动的代码是

import com.artfulbits.aiCharts.ChartView;
import com.artfulbits.aiCharts.Base.ChartSeries;
import com.artfulbits.aiCharts.Types.ChartDoughnutType;
import com.artfulbits.aiCharts.Types.ChartPieType.LabelStyle;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ChartView chartView = (ChartView) findViewById(R.id.chartView);
        ChartSeries series = chartView.getSeries().get(0); 

        String[] labels = { "milk", "yeast", "flour", 
                "eggs", "butter", "salt", "water", "sugar" };
        int[] weights = {180, 12, 450, 100, 100, 10, 250, 250 };

        for (int i = 0; i < weights.length; i++)
            series.getPoints().addXY(i, weights[i]).setLabel(labels[i]);

        series.setAttribute(ChartDoughnutType.LABEL_STYLE, LabelStyle.OutsideColumn);
        series.setAttribute(ChartDoughnutType.OPTIMIZE_POINTS, Boolean.FALSE);
    }
}

main.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

  <com.artfulbits.aiCharts.ChartView
    android:id="@+id/chartView"
    chart="@xml/chart"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

请确保您的源代码中有一个res / xml目录,并且它包含一个xml文件,如下所示。

chart.xml

<?xml version="1.0" encoding="utf-8"?>
<ai:chart xmlns:ai="http://www.artfulbits.com/android/aiCharts">
    <ai:area />
    <ai:series type="Doughnut" showLabel="true"/>
    <ai:title text="Doughnut ingredients"/>
</ai:chart>

请参阅ArtfulBits

上的完整示例