0

当我在不同TChart系列之间切换时,如果在运行之间切换图例可见性,则会出现异常。例如

  1. 显示条形图。图例可见。
  2. 显示饼图。传说无形。
  3. 显示条形图。图例可见。
  4. 碰撞!

这是重新绘制控件时导致未捕获异常的行:

chart.getLegend().setVisible(true);

在每次运行之间,我执行以下操作:

chart.setAutoRepaint(false);
chart.removeAllSeries();
// Build chart...
chart.setAutoRepaint(true);
chart.refreshControl();

TChart专家们,我怎样才能避免这种崩溃?

4

1 回答 1

1

这对我来说并不崩溃。我可以多次从 Bar 切换到 Pie 没有问题。

package com.steema.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.Spinner;

import com.steema.teechart.TChart;
import com.steema.teechart.styles.Bar;
import com.steema.teechart.styles.Pie;
import com.steema.teechart.themes.ThemesList;

public class AndroidTestActivity extends Activity implements OnItemSelectedListener{

    private TChart tChart1;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        LinearLayout group = (LinearLayout) findViewById(R.id.linearLayoutTchart);

        tChart1 = new TChart(this);
        group.addView(tChart1);
        ThemesList.applyTheme(tChart1.getChart(), 1);

        Spinner spinner = (Spinner) findViewById(R.id.series_spinner);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.series_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(this);
    }

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        tChart1.setAutoRepaint(false);
        tChart1.removeAllSeries();
        switch (arg2) {
        case 0:
            Bar bar1 = new Bar(tChart1.getChart());
            bar1.fillSampleValues();
            tChart1.getLegend().setVisible(true);
            break;
        case 1:
            Pie pie1 = new Pie(tChart1.getChart());
            pie1.fillSampleValues();
            tChart1.getLegend().setVisible(false);
            break;
        }
        tChart1.setAutoRepaint(true);
        tChart1.refreshControl();
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {

    }
}

这是我的 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">
    <TextView  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/hello"/>
    <Spinner android:id="@+id/series_spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <LinearLayout android:id="@+id/linearLayoutTchart" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"/>
</LinearLayout>

这里是我的strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">TeeChartJava for Android testing application!</string>
    <string name="app_name">AndroidTest</string>
    <string-array name="series_array">
        <item>Bar</item>
        <item>Pie</item>
    </string-array>
</resources>

更新1: 如果我在tChart1.getLegend().setSeries(tChart1.getSeries(0));上面添加他case 1

    case 1:
        Pie pie1 = new Pie(tChart1.getChart());
        pie1.fillSampleValues();
        tChart1.getLegend().setSeries(tChart1.getSeries(0));
        tChart1.getLegend().setVisible(false);
        break;

然后我在选择回 Bar 系列时收到一条错误消息。这是因为我们已将图例设置为使用饼系列(我们第一次选择饼图),当我们选择条形图时我们已删除该饼图系列,但图例仍引用已删除的饼图系列。请检查您是否为图例设置了有效的系列。IE:

    case 0:
        Bar bar1 = new Bar(tChart1.getChart());
        bar1.fillSampleValues();
        tChart1.getLegend().setSeries(tChart1.getSeries(0));
        tChart1.getLegend().setVisible(true);
        break;
    case 1:
        Pie pie1 = new Pie(tChart1.getChart());
        pie1.fillSampleValues();
        tChart1.getLegend().setSeries(tChart1.getSeries(0));
        tChart1.getLegend().setVisible(false);
        break;

清除系列列表后将设置getLegend().setSeries(null)下一个版本。removeAllSeries()

于 2013-01-07T13:37:49.503 回答