随机发生器的XY图表

时间:2011-10-10 11:16:33

标签: android achartengine

我正在尝试为随机生成器创建动态图表,以保持泵送值。对于我正在使用AChartEngine的图表。我清楚我的方法是使用异步任务机制通过动态图表的后台线程进行更新。但是我无法使用AChartEngine库来获取基本的XY图表。应用程序每次都崩溃。这是我的基本XY图表的代码。

package com.example.graph2;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

public class Graph2Activity extends Activity {
  private XYMultipleSeriesDataset RNG_Dataset = new XYMultipleSeriesDataset();
  private XYMultipleSeriesRenderer RNG_Renderer = new XYMultipleSeriesRenderer();
  public XYSeries RNG_CurrentSeries;
  private GraphicalView RNG_ChartView;

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    RNG_Renderer.setAxisTitleTextSize(16);
    RNG_Renderer.setChartTitleTextSize(20);
    RNG_Renderer.setLabelsTextSize(15);
    RNG_Renderer.setLegendTextSize(15);
    RNG_Renderer.setMargins(new int[] {20, 30, 15, 0});
    RNG_Renderer.setAxesColor(Color.YELLOW);

    String seriesTitle = "RNG";
    XYSeries series = new XYSeries(seriesTitle);

    RNG_Dataset.addSeries(series);
    RNG_CurrentSeries = series;

    XYSeriesRenderer renderer = new XYSeriesRenderer();
    renderer.setColor(Color.RED);
    RNG_Renderer.addSeriesRenderer(renderer); 
    setContentView(R.id.RNGchart);
  }

  protected void onResume() {
    if (RNG_ChartView == null) {
      LinearLayout layout = (LinearLayout) findViewById(R.id.RNGchart);
      RNG_ChartView = ChartFactory.getLineChartView(this, RNG_Dataset, RNG_Renderer);

      layout.addView(RNG_ChartView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
//      boolean enabled = HRDataset.getSeriesCount() > 0;
//      setSeriesEnabled(enabled);
    } else {
      RNG_ChartView.repaint();
    }
  } 
}

这是我用过的xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/RNGchart" android:orientation="horizontal"
   android:layout_width="fill_parent" android:layout_height="300px" 
   android:layout_weight="1" />          

有人可以告诉我哪里出错了吗? 这是异常日志:

10-10 17:20:52.761: ERROR/AndroidRuntime(335): FATAL EXCEPTION: main
10-10 17:20:52.761: ERROR/AndroidRuntime(335): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.graph2/com.example.graph2.Graph2Activity}: android.content.res.Resources$NotFoundException: Resource ID #0x7f050000 type #0x12 is not valid
10-10 17:20:52.761: ERROR/AndroidRuntime(335):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
10-10 17:20:52.761: ERROR/AndroidRuntime(335):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-10 17:20:52.761: ERROR/AndroidRuntime(335):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-10 17:20:52.761: ERROR/AndroidRuntime(335):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-10 17:20:52.761: ERROR/AndroidRuntime(335):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-10 17:20:52.761: ERROR/AndroidRuntime(335):     at android.os.Looper.loop(Looper.java:123)
10-10 17:20:52.761: ERROR/AndroidRuntime(335):     at android.app.ActivityThread.main(ActivityThread.java:3683)
10-10 17:20:52.761: ERROR/AndroidRuntime(335):     at java.lang.reflect.Method.invokeNative(Native Method)
10-10 17:20:52.761: ERROR/AndroidRuntime(335):     at java.lang.reflect.Method.invoke(Method.java:507)
10-10 17:20:52.761: ERROR/AndroidRuntime(335):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-10 17:20:52.761: ERROR/AndroidRuntime(335):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-10 17:20:52.761: ERROR/AndroidRuntime(335):     at dalvik.system.NativeStart.main(Native Method)
10-10 17:20:52.761: ERROR/AndroidRuntime(335): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f050000 type #0x12 is not valid
10-10 17:20:52.761: ERROR/AndroidRuntime(335):     at android.content.res.Resources.loadXmlResourceParser(Resources.java:1874)
10-10 17:20:52.761: ERROR/AndroidRuntime(335):     at android.content.res.Resources.getLayout(Resources.java:731)
10-10 17:20:52.761: ERROR/AndroidRuntime(335):     at android.view.LayoutInflater.inflate(LayoutInflater.java:318)
10-10 17:20:52.761: ERROR/AndroidRuntime(335):     at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
10-10 17:20:52.761: ERROR/AndroidRuntime(335):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
10-10 17:20:52.761: ERROR/AndroidRuntime(335):     at android.app.Activity.setContentView(Activity.java:1657)
10-10 17:20:52.761: ERROR/AndroidRuntime(335):     at com.example.graph2.Graph2Activity.onCreate(Graph2Activity.java:44)
10-10 17:20:52.761: ERROR/AndroidRuntime(335):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-10 17:20:52.761: ERROR/AndroidRuntime(335):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
10-10 17:20:52.761: ERROR/AndroidRuntime(335):     ... 11 more


Here is the manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.graph2"
   android:versionCode="1"
   android:versionName="1.0">
 <uses-sdk android:minSdkVersion="10" />

   <application android:icon="@drawable/icon" android:label="@string/app_name">
      <activity android:name=".Graph2Activity"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
  <activity android:name="org.achartengine.chartdemo.demo.chart.XYChartBuilder" />
     <activity android:name="org.achartengine.GraphicalActivity" />
    <activity android:name=".GeneratedChartDemo" />
    </application>
  </manifest>

1 个答案:

答案 0 :(得分:2)

您可以从here

查看完整的Demo

试试这个,

<activity android:name="com.example.graph2.Graph2Activity"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

确保您的Graph2Activity Activity位于com.example.graph2包中。