【问题标题】:Cannot process converting csv file to arrays in android无法处理将csv文件转换为android中的数组
【发布时间】:2012-06-06 20:45:04
【问题描述】:

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

【问题讨论】:

  • 在android开发中你应该拥有的实用程序是“adb.exe”,运行“adb.exe logcat”查看日志,它将为您提供确切的异常类型,在哪个文件和甚至问题出在哪一行!
  • 通常调试信息(如来自 LogCat 的信息)包含源文件中发生异常的行数。如果不知道那条线,就很难找到问题所在。尝试找到堆栈跟踪并将其发布在此处。
  • 感谢您的回复,我检查了 LogCat,虽然那里没什么,但是在 DDMS 中报告了一些错误:- 启动 logcat 时出现意外错误。尝试重新选择设备。] 找不到设备 com.android.ddmlib.AdbCommandRejectedException:在 com.android.ddmlib.AdbHelper.executeRemoteCommand(AdbHelper. java:373) at com.android.ddmlib.Device.executeShellCommand(Device.java:397) at com.android.ddmuilib.logcat.LogCatReceiver$1.run(LogCatReceiver.java:102) at java.lang.Thread.run( Thread.java:636)
  • 使用@SuppressWarnings("null") 不是一个好主意!如果您在编译期间收到空警告,那么您可以确定它在运行时不会工作!

标签: java android csv


【解决方案1】:

当运行 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];,其中10 是数组的大小。我确实建议使用 ArrayList,因为它可以具有动态大小。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-12
    • 2020-03-19
    • 1970-01-01
    • 2019-09-12
    • 2019-02-02
    • 2018-07-11
    相关资源
    最近更新 更多