Java 无法在android中处理将csv文件转换为数组的过程
我对android和JAVA非常陌生,我正在努力学习自己编写android应用程序。当我试图从csv文件中读取值并将其打印出来时,遇到了一个问题。更具体地说,我试图修改Androidplot示例 最初的Androidplot示例有效,而我修改的示例无效…我试图找到问题,但失败了…每次我尝试调试代码时,就会弹出ActivityThread.java,并且有一个小箭头指向以下代码:Java 无法在android中处理将csv文件转换为数组的过程,java,android,csv,Java,Android,Csv,我对android和JAVA非常陌生,我正在努力学习自己编写android应用程序。当我试图从csv文件中读取值并将其打印出来时,遇到了一个问题。更具体地说,我试图修改Androidplot示例 最初的Androidplot示例有效,而我修改的示例无效…我试图找到问题,但失败了…每次我尝试调试代码时,就会弹出ActivityThread.java,并且有一个小箭头指向以下代码: catch (Exception e) { if (!mInstrumentation.onExcep
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();
}
}
包edu.ius.rwisman.androidplot示例;
导入android.app.Activity;
导入android.graphics.Color;
导入android.os.Bundle;
导入com.androidplot.xy.SimpleXYSeries;
导入com.androidplot.series.XYSeries;
导入com.androidplot.xy.*;
导入java.io.BufferedReader;
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.io.FileReader;
导入java.io.IOException;
导入java.text.DecimalFormat;
导入java.util.array;
公共类AndroidPlotExampleActivity扩展活动
{
私有XYPlot mySimpleXYPlot;
@抑制警告(“空”)
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//初始化XYPlot引用:
mySimpleXYPlot=(XYPlot)findViewById(R.id.mySimpleXYPlot);
编号[]系列1编号=空;
Number[]series2Numbers=null;
int行=0;
//创建两个要打印的y值数组:
试一试{
File File=新文件(“/home/hanrui/workspace/table.csv”);
BufferedReader reader=新的BufferedReader(新文件读取器(文件));
reader.readLine();
字符串行=null;
当运行Android应用程序时((line=reader.readLine())!=null&&row在手机上运行(模拟器或实际设备本身),因此很可能找不到此文件:
File File=new文件(“/home/hanrui/workspace/table.csv”);
在try-catch语句之后,检查series1Numbers
是否等于null
编辑:
我注意到,您从未初始化数字数组:
Number[]系列1numbers=null;
Java不是PHP,在PHP中数组将直接创建,但在Java中不是这样。您需要调用series1Numbers=new Number[10];
其中10
是数组的大小。我建议改用ArrayList,因为它可以有一个动态的大小。android开发应该使用的实用程序包括“adb.exe”,运行“adb.exe logcat”查看日志,该日志将为您提供确切的异常类型、文件中的异常以及问题所在的行!通常是调试信息(如logcat中的信息)包含发生异常的源文件中的行数。如果不知道该行,则很难找到问题。请尝试查找stacktrace并将其发布到此处。感谢您的回复,我检查了LogCat,虽然那里没有太多内容,但在DDMS中报告了一些错误:-启动LogCat时出现意外错误。请尝试resel正在检查设备。]未找到设备com.android.ddmlib.adbcommandejectedException:未在com.android.ddmlib.AdbHelper.setDevice(AdbHelper.java:752)中找到设备com.android.ddmlib.AdbHelper.executeMemoteCommand(AdbHelper.java:373)在com.android.ddmlib.device.executeShareCommand(device.java:397)中找到设备在com.android.ddmuilib.logcat.LogCatReceiver$1.run(LogCatReceiver.java:102)和java.lang.Thread.run(Thread.java:636)上,使用@SuppressWarnings(“null”)
不是一个好主意!如果您在编译时收到一个null警告,那么您可以非常确定它在运行时不会起作用!