我想使用this example
中的一个类我正在显示折线图
在我的情况下,x轴代表时间,所以我想通过调用执行函数bellow每秒更新图表并执行以下操作:1 - 添加更多点2 - 将平移移动到我当前时间
所以最后我希望看到情节上的线随时间移动
我尝试创建一个名为update的函数,它创建一个新线程并从onResume调用它,但是应用程序崩溃了,我得到了
AndroidRuntime(23861): FATAL EXCEPTION: Thread-11775
AndroidRuntime(23861): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
AndroidRuntime(23861): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
AndroidRuntime(23861): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:5908)
AndroidRuntime(23861): at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:837)
AndroidRuntime(23861): at android.view.View.requestLayout(View.java:15792)
AndroidRuntime(23861): at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:358)
AndroidRuntime(23861): at android.view.ViewGroup.removeAllViews(ViewGroup.java:3902)
这是我的主要课程
public class ChartDemo extends Activity {
private AverageCubicTemperatureChart TCh = new AverageCubicTemperatureChart();
RelativeLayout LayoutToDisplayChart;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutToDisplayChart=(RelativeLayout)findViewById(R.id.relative_layout1);
}
@Override
protected void onResume() {
super.onResume();
Intent achartIntent = TCh.execute(this,LayoutToDisplayChart);
AverageCubicTemperatureChart.update(this,LayoutToDisplayChart);
}
}
类 AverageCubicTemperatureChart
的代码/**
* Average temperature demo chart.
*/
public class AverageCubicTemperatureChart implements IDemoChart {
/**
* Returns the chart name.
*
* @return the chart name
*/
public String getName() {
return "Average temperature";
}
public static void update(Context c, RelativeLayout p)
{
thread = new Thread() {
public void run() {
while(true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
parent.removeAllViews();
execute(c,p);
}
}
};
thread.start();
}
/**
* Returns the chart description.
*
* @return the chart description
*/
public String getDesc() {
return "The average temperature in 4 Greek islands (cubic line chart)";
}
/**
* Executes the chart demo.
*
* @param context the context
* @return the built intent
*/
public Intent execute(Context context,RelativeLayout parent) {
String[] titles = new String[] { "Crete", "Corfu", "Thassos", "Skiathos" };
List<double[]> x = new ArrayList<double[]>();
for (int i = 0; i < titles.length; i++) {
x.add(new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 });
}
List<double[]> values = new ArrayList<double[]>();
values.add(new double[] { 12.3, 12.5, 13.8, 16.8, 20.4, 24.4, 26.4, 26.1, 23.6, 20.3, 17.2,
13.9 });
values.add(new double[] { 10, 10, 12, 15, 20, 24, 26, 26, 23, 18, 14, 11 });
values.add(new double[] { 5, 5.3, 8, 12, 17, 22, 24.2, 24, 19, 15, 9, 6 });
values.add(new double[] { 9, 10, 11, 15, 19, 23, 26, 25, 22, 18, 13, 10 });
int[] colors = new int[] { Color.BLUE, Color.GREEN, Color.CYAN, Color.YELLOW };
PointStyle[] styles = new PointStyle[] { PointStyle.CIRCLE, PointStyle.DIAMOND,
PointStyle.TRIANGLE, PointStyle.SQUARE };
XYMultipleSeriesRenderer renderer = AbstractDemoChart.buildRenderer(colors, styles);
int length = renderer.getSeriesRendererCount();
for (int i = 0; i < length; i++) {
((XYSeriesRenderer) renderer.getSeriesRendererAt(i)).setFillPoints(true);
}
AbstractDemoChart.setChartSettings(renderer, "Average temperature", "Month", "Temperature", 0, 100, 0, 100,
Color.LTGRAY, Color.LTGRAY);
renderer.setXLabels(12);
renderer.setYLabels(10);
renderer.setShowGrid(true);
renderer.setXLabelsAlign(Align.RIGHT);
renderer.setYLabelsAlign(Align.RIGHT);
renderer.setZoomButtonsVisible(true);
renderer.setPanLimits(new double[] { 0, 200, 0, 100 });
renderer.setZoomLimits(new double[] { 0, 200, 0, 100 });
Intent intent = ChartFactory.getCubicLineChartIntent(context, AbstractDemoChart.buildDataset(titles, x, values),
renderer, 0.33f, "Average temperature");
mChartView2=ChartFactory.getCubeLineChartView(context, AbstractDemoChart.buildDataset(titles, x, values), renderer, 0.33f);
parent.addView(mChartView2);
return intent;
}
}
答案 0 :(得分:2)
您必须使用runOnUiThread()
正如我在此已经指出的那样: How to transfer the value of the string from thread to another?
澄清:
您的Logcat错误表示您无法修改parent
- AverageCubicTemperatureChart
中的查看,因为此主题不是parent
的所有者。
更改线路
parent.addView(mChartView2)
来:
parent.runOnUiThread(new Runnable() {
@Override
public void run() {
parent.addView(mChartView2);
}
});
应解决您的问题