当我在不同的TChart
系列之间切换时,如果在运行之间切换了图例可见性,则会出现异常。 E.g。
这是在重新绘制控件时导致未捕获异常的行:
chart.getLegend().setVisible(true);
在每次运行之间,我执行以下操作:
chart.setAutoRepaint(false);
chart.removeAllSeries();
// Build chart...
chart.setAutoRepaint(true);
chart.refreshControl();
TChart
那里的专家,我怎样才能避免这次崩溃?
答案 0 :(得分: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系列时收到错误消息。 这是因为我们将图例设置为使用Pie系列(我们选择Pie的fisrt时间),我们在选择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()
。