-1

我对 android 和 JAVA 非常陌生,我正在尝试自己学习编写 android 应用程序。当我试图从 csv 文件中读取值并将它们绘制出来时,我遇到了一个问题。更具体地说,我试图修改 Androidplot 示例。

原始的 Androidplot 示例有效,而我修改的示例无法正常工作...我试图找到问题,但失败失败失败...每次我尝试调试代码时,ActivityThread.java 都会弹出,并且有一个小箭头指向以下代码:

catch (Exception e) {
        if (!mInstrumentation.onException(activity, e)) {
            throw new RuntimeException(
                "Unable to start activity " + component
                + ": " + e.toString(), e);
        }
    }

谁能告诉我真正的问题是什么?任何帮助将不胜感激 =)

以下是我修改后的代码:

package edu.ius.rwisman.AndroidPlotExample;

import android.app.Activity;  

import android.graphics.Color;
import android.os.Bundle;
import com.androidplot.xy.SimpleXYSeries;
import com.androidplot.series.XYSeries;
import com.androidplot.xy.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.Arrays;


public class AndroidPlotExampleActivity extends Activity
{

private XYPlot mySimpleXYPlot;

@SuppressWarnings("null")
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Initialize our XYPlot reference:
    mySimpleXYPlot = (XYPlot) findViewById(R.id.mySimpleXYPlot);
    Number[] series1Numbers = null;
    Number[] series2Numbers = null;
    int row = 0;
    // Create two arrays of y-values to plot:
    try {

        File file = new File("/home/hanrui/workspace/table.csv");
        BufferedReader reader = new BufferedReader(new FileReader(file));
        reader.readLine();
        String line = null;
        while((line=reader.readLine())!=null&&row<24){
            String item[] = line.split(",");

            String last = item[item.length-1];
            int value = Integer.parseInt(last);
            series1Numbers[row] = value;
            series2Numbers[row] = value;
            row++;
        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }



    // Turn the above arrays into XYSeries:
    XYSeries series1 = new SimpleXYSeries(
            Arrays.asList(series1Numbers),          // SimpleXYSeries takes a List so turn our array into a List
            SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, // Y_VALS_ONLY means use the element index as the x value
            "Series1");                             // Set the display title of the series

    // Same as above, for series2
    XYSeries series2 = new SimpleXYSeries(Arrays.asList(series2Numbers), SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, 
            "Series2");

    // Create a formatter to use for drawing a series using LineAndPointRenderer:
    LineAndPointFormatter series1Format = new LineAndPointFormatter(
            Color.rgb(0, 200, 0),                   // line color
            Color.rgb(0, 100, 0),                   // point color
            Color.rgb(150, 190, 150));              // fill color (optional)

    // Add series1 to the xyplot:
    mySimpleXYPlot.addSeries(series1, series1Format);

    // Same as above, with series2:
    mySimpleXYPlot.addSeries(series2, new LineAndPointFormatter(Color.rgb(0, 0, 200), Color.rgb(0, 0, 100),
            Color.rgb(150, 150, 190)));


    // Reduce the number of range labels
    mySimpleXYPlot.setTicksPerRangeLabel(3);

    // By default, AndroidPlot displays developer guides to aid in laying out your plot.
    // To get rid of them call disableAllMarkup():
    mySimpleXYPlot.disableAllMarkup();
}
}
4

1 回答 1

0

运行 Android 应用程序时,它会在手机(模拟器或实际设备本身)上运行,因此很可能找不到此文件:

File file = new File("/home/hanrui/workspace/table.csv");

在你的 try-catch-statement 之后,检查是否series1Numbers等于 null。

编辑:

我注意到了一些事情,你永远不会初始化你的 Number 数组:

Number[] series1Numbers = null;.

Java 不是 PHP,在 PHP 中将直接创建数组,但在 Java 中并非如此。您需要调用series1Numbers = new Number[10];where10是数组的大小。我建议改用 ArrayList,因为它可以具有动态大小。

于 2012-06-06T20:51:32.217 回答