开发者

XY Chart for Random generator

I am trying to create a dynamic chart for my random generator that keeps pumping values. For the graph I am using AChartEngine. I am clear about my approach in the sense of using the Async Task mechanism to do the update thorough the background thread for dynamic chart. But I am not able to get the basic XY chart using the AChartEngine libraries to work. The application crashes every time. Here is my code for the basic XY chart.

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();
    }
  } 
}

Here is the xml I have used:

<?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" />          

Can someone please tell me where I am going wrong? Here is the exception log:

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.jav开发者_JAVA技巧a: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>


You can checkout my complete Demo from here

Try this,

<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>

And make sure your Graph2Activity Activity is inside the com.example.graph2 package.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜