【问题标题】:Update Line Chart every second每秒更新折线图
【发布时间】:2013-12-09 07:16:19
【问题描述】:

我想使用this example 中的一个类

我正在显示折线图

在我的例子中,x 轴代表时间,所以我想通过调用下面的执行函数并执行以下操作每秒更新图表: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;
    }
}

【问题讨论】:

    标签: android multithreading achartengine linechart


    【解决方案1】:

    你必须使用runOnUiThread()

    正如我在这里已经指出的: How to transfer the value of the string from thread to another?

    澄清: 您的 Logcat 错误意味着您无法修改 AverageCubicTemperatureChart 中的 parent-View,因为此线程不是 parent 的所有者。

    换行 parent.addView(mChartView2)到:

    parent.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            parent.addView(mChartView2);
        }
    });
    

    应该能解决你的问题

    【讨论】:

    • 那么我应该把这个放在哪里?主要活动还是在 AverageCubicTemperatureChart 类中?如果我想调用函数每秒执行一次呢?
    • runOnUiThread() 必须在 AverageCubicTemperatureChart 中才能替换您的 execute 函数中的 parent.addView(mChartView2);。要重复您的任务,您可以使用例如:stackoverflow.com/a/6532298/995320
    • 你的意思是写一个与链接中的函数类似的函数,然后将 runOnUiThread 放在 try{} 中?我不想替换 execute() 我想调用它,因为它正在添加点并创建我想要每秒执行的图表,因为我每次都会添加点
    • 不,您只需要替换 execute() 方法中的 parent.addView(mChartView2); 行(它在您的 return intent; 之前的行并插入 runOnUiThread 而不是它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-26
    • 2013-01-26
    相关资源
    最近更新 更多