-1

我是 Android 新手,我必须在 android Tab 或 Phone 上绘制 ECG WAVES,我尝试使用 achartengine 库在 lineGrap 上获取我的数据,但我得到了 NullPointerException。你能帮帮我吗!!!

这是我的 GraphActivity.java

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class GraphActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button strt = (Button) findViewById(R.id.LineGraph);

        strt.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                lineGraph line = new lineGraph();
                Intent lineIntent = line.getIntent(getApplicationContext());
                startActivity(lineIntent);

            }
        });
    }

这是我的 lineGraph.java

public class lineGraph {


    static BufferedReader reader ;
    static DataInputStream data_in;

    static int[] x = new int[6000]; // x values
    static int[] y1= new int[6000]; // y1 values
    static int[] y2= new int[6000]; // y2 values
    static int[] y3= new int[6000]; // y3 values
    static int[] y4= new int[6000]; // y4 values
    static int[] y5= new int[6000]; // y5 values
    static int[] y6= new int[6000]; // y6 values
    static int[] y7= new int[6000]; // y7 values
    static int[] y8= new int[6000]; // y8 values

    static int ch =0;

    public  Intent getIntent(Context context){

        try {
            data_in = new DataInputStream(
                        new BufferedInputStream(
                                new FileInputStream(new File("src/data.txt"))));
        } catch (FileNotFoundException e) {

            e.printStackTrace();
        }
        try {
             while(ch<6000){
                 Byte LSB = data_in.readByte();
                 Byte MSB = data_in.readByte();
                  int current= ((MSB<<8)| (LSB & 0xFF));
                y1[ch]=  current; 
                ch++;
                }
                while(ch>=6000 && ch<12000){
                    Byte LSB = data_in.readByte();
                    Byte MSB = data_in.readByte();
                   int current= ((MSB<<8)| (LSB & 0xFF));
                   y2[ch-6000]= current;
                   ch++;
                    }


         }
         catch(EOFException eof) {
             System.out.println ("End of File read");
//             break;
         } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       try {
        data_in.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }  
//   }
        for(int i=0;i<6000;i++){
         x[i]=i;
        }

        TimeSeries series1 = new TimeSeries("line1");
        for(int i=0;i<x.length;i++){
            series1.add(x[i], y1[i]);
        }
        TimeSeries series2 = new TimeSeries("line2");
        for(int i=0;i<x.length;i++){
            series1.add(x[i], y2[i]);
        }
        TimeSeries series3 = new TimeSeries("line3");
        for(int i=0;i<x.length;i++){
            series1.add(x[i], y3[i]);
        }
        TimeSeries series4 = new TimeSeries("line4");
        for(int i=0;i<x.length;i++){
            series1.add(x[i], y4[i]);
        }
        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
        dataset.addSeries(series1);
        dataset.addSeries(series2);
        dataset.addSeries(series3);
        dataset.addSeries(series4);

        XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
        XYSeriesRenderer renderer= new XYSeriesRenderer();
        renderer.setColor(Color.GREEN);
        renderer.setPointStyle(PointStyle.CIRCLE);
        renderer.setFillPoints(true);
        mRenderer.addSeriesRenderer(renderer);

        Intent intent = ChartFactory.getLineChartIntent(context, dataset, mRenderer, "Line Graph");
        return intent;

        }
    }

这是我的 Android 清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.praktikum.graph"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".GraphActivity"
            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.GraphicalActivity"/>
    </application>

</manifest>

和我的 LogCat

    05-24 13:03:41.309: E/AndroidRuntime(21551): FATAL EXCEPTION: main
05-24 13:03:41.309: E/AndroidRuntime(21551): java.lang.NullPointerException
05-24 13:03:41.309: E/AndroidRuntime(21551):    at com.praktikum.graph.lineGraph.getIntent(lineGraph.java:58)
05-24 13:03:41.309: E/AndroidRuntime(21551):    at com.praktikum.graph.GraphActivity$1.onClick(GraphActivity.java:22)
05-24 13:03:41.309: E/AndroidRuntime(21551):    at android.view.View.performClick(View.java:3127)
05-24 13:03:41.309: E/AndroidRuntime(21551):    at android.view.View$PerformClick.run(View.java:12025)
05-24 13:03:41.309: E/AndroidRuntime(21551):    at android.os.Handler.handleCallback(Handler.java:587)
05-24 13:03:41.309: E/AndroidRuntime(21551):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-24 13:03:41.309: E/AndroidRuntime(21551):    at android.os.Looper.loop(Looper.java:132)
05-24 13:03:41.309: E/AndroidRuntime(21551):    at android.app.ActivityThread.main(ActivityThread.java:4126)
05-24 13:03:41.309: E/AndroidRuntime(21551):    at java.lang.reflect.Method.invokeNative(Native Method)
05-24 13:03:41.309: E/AndroidRuntime(21551):    at java.lang.reflect.Method.invoke(Method.java:491)
05-24 13:03:41.309: E/AndroidRuntime(21551):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
05-24 13:03:41.309: E/AndroidRuntime(21551):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
05-24 13:03:41.309: E/AndroidRuntime(21551):    at dalvik.system.NativeStart.main(Native Method)
05-24 13:03:43.319: I/dalvikvm(21551): threadid=4: reacting to signal 3
05-24 13:03:43.319: I/dalvikvm(21551): Wrote stack traces to '/data/anr/traces.txt'

预先感谢您的帮助

4

1 回答 1

0

在android中,您需要将此类文本文件放入asset或raw ....

并以这种方式阅读

http://www.javacodegeeks.com/2012/02/android-read-file-from-assets.html

 // To load text file
        InputStream input;
  try {
   input = assetManager.open("helloworld.txt");

          int size = input.available();
          byte[] buffer = new byte[size];
          input.read(buffer);
          input.close();

          // byte buffer into a string
          String text = new String(buffer);

          txtContent.setText(text);
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

阅读一个简单的文本文件

http://android-er.blogspot.in/2010/07/display-text-file-in-resraw_01.html

private String readTxt(){

     InputStream inputStream = getResources().openRawResource(R.raw.hello);

     ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

     int i;
  try {
   i = inputStream.read();
   while (i != -1)
      {
       byteArrayOutputStream.write(i);
       i = inputStream.read();
      }
      inputStream.close();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

     return byteArrayOutputStream.toString();
    }

http://androidgps.blogspot.in/2008/10/dealing-with-large-resources.html

于 2012-05-24T12:33:43.433 回答