-1

私はAndroidが初めてで、Androidタブまたは電話で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では、そのようなテキストファイルをアセットまたは生に配置する必要があります....

そしてそのように読む

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 に答える