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


显示条形图。图例可见。
显示饼图。图例不可见。
显示条形图。图例可见。
崩溃!


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

chart.getLegend().setVisible(true);


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

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


TChart专家在那里,如何避免此崩溃?

最佳答案

这对我来说并不崩溃。我可以几次从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));添加到int上方,则他case 1

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


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

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

09-04 18:49
查看更多