0

嗨,我想在 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>
4

1 回答 1

0

Please make sure that you have a res/xml directory in your source code and that it contains a xml file like below.

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>

See the full example at ArtfulBits

于 2013-12-12T09:51:55.143 回答