当我在不同的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));
加到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;
下一版本将在清除系列列表后在removeAllSeries()
中设置getLegend().setSeries(null)