所以我试图让我的Android应用程序创建一个CSV文件,但它似乎没有工作和崩溃。我已经测试了其余的代码并且它工作正常,但这似乎也导致了崩溃:/一个csv文件是在我的手机根目录上创建的,这是空的
更新了我的logcat 我的代码:
FileWriter writefile;
File root = Environment.getExternalStorageDirectory();
File MesurementFile= new File(root, "Data.csv");
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = df.format(c.getTime());
String date = "Current date";
String a= "a size : ";
String b="b Size : ";
public void writeCsvHeader() throws IOException {
String line = String.format("%s,%s,%s\n",date,a,b);
writer = new FileWriter(csvfile);
writer.write(line);
}
public void writeCsvData(long a, long b)throws IOException {
String line = String.format("%f,%f,%f,%f\n", formattedDate ,a,b);
writer.write(line);
try {
writer = new FileWriter(MesurementFile);
writeCsvData(a,b);
} catch (IOException e) {
e.printStackTrace();
}
}}
Stacktrace:
07-25 14:07:54.874: E/AndroidRuntime(9482): FATAL EXCEPTION: main
07-25 14:07:54.874: E/AndroidRuntime(9482): Process: com.extras, PID: 9482
07-25 14:07:54.874: E/AndroidRuntime(9482): java.lang.RuntimeException: Unable to start service com.extras.MyService@41e9c500 with Intent { cmp=com.extras/.MyService (has extras) }: java.util.IllegalFormatConversionException: %f can't format java.lang.String arguments
07-25 14:07:54.874: E/AndroidRuntime(9482): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2737)
07-25 14:07:54.874: E/AndroidRuntime(9482): at android.app.ActivityThread.access$2100(ActivityThread.java:144)
07-25 14:07:54.874: E/AndroidRuntime(9482): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1302)
07-25 14:07:54.874: E/AndroidRuntime(9482): at android.os.Handler.dispatchMessage(Handler.java:102)
07-25 14:07:54.874: E/AndroidRuntime(9482): at android.os.Looper.loop(Looper.java:136)
07-25 14:07:54.874: E/AndroidRuntime(9482): at android.app.ActivityThread.main(ActivityThread.java:5140)
07-25 14:07:54.874: E/AndroidRuntime(9482): at java.lang.reflect.Method.invokeNative(Native Method)
07-25 14:07:54.874: E/AndroidRuntime(9482): at java.lang.reflect.Method.invoke(Method.java:515)
07-25 14:07:54.874: E/AndroidRuntime(9482): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
07-25 14:07:54.874: E/AndroidRuntime(9482): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
07-25 14:07:54.874: E/AndroidRuntime(9482): at dalvik.system.NativeStart.main(Native Method)
07-25 14:07:54.874: E/AndroidRuntime(9482): Caused by: java.util.IllegalFormatConversionException: %f can't format java.lang.String arguments
07-25 14:07:54.874: E/AndroidRuntime(9482): at java.util.Formatter.badArgumentType(Formatter.java:1489)
07-25 14:07:54.874: E/AndroidRuntime(9482): at java.util.Formatter.transformFromFloat(Formatter.java:2038)
07-25 14:07:54.874: E/AndroidRuntime(9482): at java.util.Formatter.transform(Formatter.java:1465)
07-25 14:07:54.874: E/AndroidRuntime(9482): at java.util.Formatter.doFormat(Formatter.java:1081)
07-25 14:07:54.874: E/AndroidRuntime(9482): at java.util.Formatter.format(Formatter.java:1042)
07-25 14:07:54.874: E/AndroidRuntime(9482): at java.util.Formatter.format(Formatter.java:1011)
07-25 14:07:54.874: E/AndroidRuntime(9482): at java.lang.String.format(String.java:1999)
07-25 14:07:54.874: E/AndroidRuntime(9482): at java.lang.String.format(String.java:1973)
07-25 14:07:54.874: E/AndroidRuntime(9482): at com.extras.MyService.writeCsvData(MyService.java:83)
07-25 14:07:54.874: E/AndroidRuntime(9482): at com.extras.MyService.onStartCommand(MyService.java:46)
07-25 14:07:54.874: E/AndroidRuntime(9482): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2720)
07-25 14:07:54.874: E/AndroidRuntime(9482): ... 10 more
07-25 14:24:25.994: E/AndroidRuntime(13219): FATAL EXCEPTION: main
07-25 14:24:25.994: E/AndroidRuntime(13219): Process: com.extras, PID: 13219
07-25 14:24:25.994: E/AndroidRuntime(13219): java.lang.StackOverflowError
07-25 14:24:25.994: E/AndroidRuntime(13219): at java.lang.Thread.currentThread(Thread.java:470)
07-25 14:24:25.994: E/AndroidRuntime(13219): at java.lang.ThreadLocal.get(ThreadLocal.java:53)
07-25 14:24:25.994: E/AndroidRuntime(13219): at java.util.Formatter.getDecimalFormat(Formatter.java:594)
07-25 14:24:25.994: E/AndroidRuntime(13219): at java.util.Formatter.transformF(Formatter.java:2215)
07-25 14:24:25.994: E/AndroidRuntime(13219): at java.util.Formatter.transformFromFloat(Formatter.java:2055)
07-25 14:24:25.994: E/AndroidRuntime(13219): at java.util.Formatter.transform(Formatter.java:1465)
07-25 14:24:25.994: E/AndroidRuntime(13219): at java.util.Formatter.doFormat(Formatter.java:1081)
07-25 14:24:25.994: E/AndroidRuntime(13219): at java.util.Formatter.format(Formatter.java:1042)
07-25 14:24:25.994: E/AndroidRuntime(13219): at java.util.Formatter.format(Formatter.java:1011)
07-25 14:24:25.994: E/AndroidRuntime(13219): at java.lang.String.format(String.java:1999)
07-25 14:24:25.994: E/AndroidRuntime(13219): at java.lang.String.format(String.java:1973)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:83)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writeCsvData(MyService.java:88)
07-25 14:24:25.994: E/AndroidRuntime(13219): at com.extras.MyService.writ
答案 0 :(得分:0)
您正在使用%f格式化字符串,因此它正在抱怨。还有四个%f,你传递3个值(formattedDate,a,b)
String line = String.format("%f,%f,%f,%f\n", formattedDate ,a,b);
答案 1 :(得分:0)
%f无法格式化java.lang.String参数
基本上说出你需要的一切。您尝试编写字符串,但您的占位符只接受%f
float
(如果我没记错的话)。
因此,只需检查您在此处拥有的参数类型,并将%f
更改为%s
或更改参数类型。
// at least one %f is wrong here...
String line = String.format("%f,%f,%f,%f\n", formattedDate ,a,b);
更新
您现在看到StackOverflowException
因为您在同一方法中调用自己的方法。这是一个无限循环,通常称为#34;递归"。递归需要有一些明确定义和防弹的结束条件。堆栈大小是递归可以执行的循环的限制,超出了该限制。
public void writeCsvData(long a, long b)throws IOException {
String line = String.format("%f,%f,%f,%f\n", formattedDate ,a,b);
writer.write(line);
try {
writer = new FileWriter(MesurementFile);
writeCsvData(a,b); // THIS cause the loop
} catch (IOException e) {
e.printStackTrace();
}
}
您需要找到一种方法来只写一行并在循环中重复调用它。要了解如何做到这一点取决于你:)
答案 2 :(得分:0)
引起:
java.util.IllegalFormatConversionException: %f
无法格式化java.lang.String
个参数
String line = String.format("%f,%f,%f,%f\n", formattedDate ,a,b);
我在这里看到三个问题:
%f
值(无浮点)时使用long
(浮点格式化)formattedDate
是一个String
,它不能与%f
格式化,用于浮点数。