简体   繁体   English

Java / Android-崩溃-Apache commons-io-2.4.jar

[英]Java/Android - Crash - Apache commons-io-2.4.jar

I am trying to save values in a string to a file on my local system by using the org.apache.commons.io.FileUtils static method writeStringToFile . 我正在尝试使用org.apache.commons.io.FileUtils静态方法writeStringToFile将字符串中的值保存到本地系统上的文件中。

So, first I downloaded the commons-io-2.4.jar, along with its javadocs and source and imported it into my Eclipse project through the Java Build Path. 因此,首先,我下载commons-io-2.4.jar以及其javadocs和源代码,并通过Java Build Path将其导入到我的Eclipse项目中。 Everything compiles just fine. 一切都可以编译。

However, when I add the simple line: 但是,当我添加简单的行时:

org.apache.commons.io.FileUtils.writeStringToFile(new java.io.File(Environment.getExternalStorageDirectory().toString() + "/logt.txt"), rslt.toString());

The program crashes. 程序崩溃。 But, it doesn't crash anywhere near that statement. 但是,它不会在该语句附近崩溃。 Instead, it crashes at the constructor of the Object which contains the method which calls this function. 相反,它在包含调用此函数的方法的Object的构造函数处崩溃。

In other words 换一种说法

  • Earlier in my code I create an object TranslateTask, 在我的代码前面,我创建了一个对象TranslateTask,
    • Which happens to contain a function call doTranslate(). 碰巧包含一个函数调用doTranslate()。
    • The writeStringToFile call is made within the doTranslate() call, 在doTranslate()调用中进行了writeStringToFile调用,
  • However the actual crash takes place when I instantiate a TranslateTask object. 但是,当我实例化TranslateTask对象时,实际发生崩溃。
  • But, when I comment out the call to writeStringToFile(), the crash never happens 但是,当我注释掉对writeStringToFile()的调用时,崩溃永远不会发生
    • Even though the crash doesn't take place in the doTranslate() call... 即使崩溃不会发生在doTranslate()调用中...
    • So, just the mere mention of writeStringToFile() makes my program crash when I instantiate an object which contains it. 因此,仅实例化包含writeStringToFile()的对象会使我的程序崩溃。

To make it more eary, I instantiate the object within a Try, catch (RejectedExecutionException e) block but instead the program crashes at this part: 为了使它更加清晰,我在一个Try, catch (RejectedExecutionException e)块中实例化了该对象Try, catch (RejectedExecutionException e)但是程序在此部分崩溃了:

PathClassLoader.findClass(String) line: 243 **Last call in the stack**
PathClassLoader(ClassLoader).loadClass(String, boolean) line: 573   
PathClassLoader(ClassLoader).loadClass(String) line: 532    
Translate$4.run() line: 159  **Crash happens here where I instantiate a TranslateTask object**

So PathClassLoader... Not sure how to approach debugging this. 因此,PathClassLoader ...不确定如何调试此方法。 All I know is that if I commend out the org.apache.commons.io.FileUtils.writeStringToFile() call, the error never happens and the code runs fine everywhere. 我所知道的是,如果我赞扬org.apache.commons.io.FileUtils.writeStringToFile()调用,该错误就永远不会发生,并且代码到处都能正常运行。

Running this on the API8/10 of Android, using Eclipse Indigo. 使用Eclipse Indigo在Android的API8 / 10上运行此代码。

EDIT- Logcat: 编辑-Logcat:

08-07 18:40:11.409: I/System.out(1395): debugger has settled (1363) 08-07 18:40:11.409:I / System.out(1395):调试器已安装(1363)

08-07 18:40:14.399: W/KeyCharacterMap(1395): No keyboard for id 0 08-07 18:40:14.399:W / KeyCharacterMap(1395):没有ID为0的键盘

08-07 18:40:14.399: W/KeyCharacterMap(1395): Using default keymap: /system/usr/keychars/qwerty.kcm.bin 08-07 18:40:14.399:W / KeyCharacterMap(1395):使用默认键盘映射:/system/usr/keychars/qwerty.kcm.bin

Don't think it gives any hint, but this is all that logcat gives after the debugger has settled. 不要以为它提供了任何提示,但这只是调试器解决后logcat给出的全部内容。

EDIT2 - For good measure, I just added <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> But still no go EDIT2-好吧,我只是添加了<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />但还是没有

EDIT3 - I am starting to think the apache commons-io-2.4.jar doesn't work with Android inherently. EDIT3-我开始认为apache commons-io-2.4.jar本质上不适用于Android。 Could that be it? 可以吗?

EDIT4 - In case anyone wants to take a crack at it, here is my code. EDIT4-如果有人想破解它,这是我的代码。 It is made in Windows 7, Eclipse Indigo. 它是在Windows 7,Eclipse Indigo中制成的。 The code is based on the "Hello Android" translate section. 该代码基于“ Hello Android”翻译部分。 All I am trying to do is extract some JSON into a string and save it into my sdcard. 我要做的就是将一些JSON提取到字符串中并将其保存到我的sdcard中。 But, it has evolved into this mystery here... https://dl.dropbox.com/u/10790286/Translate.zip (See TranslateTask.java line 111) 但是,它在这里已经演变成一个谜... https://dl.dropbox.com/u/10790286/Translate.zip (请参阅TranslateTask.java第111行)

EDIT5 - Interesting update. EDIT5-有趣的更新。 When I manually put FileUtils.writeStringToFile(new java.io.File("/mnt/sdcard/logt.txt"), "test"); 当我手动放置FileUtils.writeStringToFile(new java.io.File("/mnt/sdcard/logt.txt"), "test"); into the eclipse expressions box during a debug, I get the following error 在调试过程中进入eclipse表达式框,出现以下错误

An exception occurred: java.lang.NoSuchMethodError 发生异常:java.lang.NoSuchMethodError

I don't understand why I would get this error because 1) the WriteStringToFile() function is in the commons-io source. 我不明白为什么会收到此错误,因为1)WriteStringToFile()函数位于commons-io源代码中。 2) I can instantiate a FileUtils object 3) I imported it into my program. 2)我可以实例化FileUtils对象。3)将其导入到程序中。

Whats going on here? 这里发生了什么?

Writing to the path "c:\\\\temp\\\\logt.txt" isn't going to work on Android. 在Android上无法写入路径"c:\\\\temp\\\\logt.txt" You can use something like Environment.getExternalStorageDirectory().toString() + "/log.txt" to write to the SD card. 您可以使用Environment.getExternalStorageDirectory().toString() + "/log.txt"来写入SD卡。

EDIT: The follow worked on my Galaxy Nexus: 编辑:以下在我的Galaxy Nexus上有效:

try {
  FileUtils.writeStringToFile(new File(Environment.getExternalStorageDirectory(),"log2.txt"), "HelloNow");
} catch (IOException e) {
  Log.e(TAG,e.getMessage())
}

Though the programme runs succesfully in complier ,the programme crashes while it runs in android device .Because during the run time it checks over jar files within android dependancies only. 尽管该程序在complier中成功运行,但在android设备中运行时该程序崩溃。因为在运行时,它仅检查android相关性中的jar文件。 I too had similar type of problem .I got it solved when in preferences/properties -> java build .Tick the external jar file . 我也有类似类型的问题。当我在preferences / properties-> java build .Tick外部jar文件时解决了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 构建Android Bundle的问题-引发错误“文件&#39;root / lib / commons-io-2.4.jar&#39;使用保留的文件或目录名称&#39;lib&#39;” - Issue with building Android Bundle - throws error “File 'root/lib/commons-io-2.4.jar' uses reserved file or directory name 'lib'” Java:使用Apache commons-IO 2.4在子目录中递归列出文件 - Java: list files recursively in subdirectories with Apache commons-IO 2.4 Android上的Apache Commons IO - Apache Commons IO on Android 如何使用Textpad将org.apache.commons.io jar文件用于Java项目 - how to use org.apache.commons.io jar file into Java project using Textpad 使用Java删除文件时出现问题(apache commons io) - Problems deleting a file with Java (apache commons io) 如何在Eclipse中包含Apache Commons IO? (JAVA) - How to include Apache Commons IO in Eclipse? (Java) WebLogic-Jar存在,但NoClassDefFoundError:org / apache / commons / io / IOUtils - WebLogic - Jar is there but NoClassDefFoundError: org/apache/commons/io/IOUtils 如何导入commons-io.jar jsp / java - How to import commons-io.jar jsp/java 如何使用FileUtils中的copyDirectory继续复制错误(Apache Commons IO 2.4) - How to continue copying on error with copyDirectory from FileUtils (Apache Commons IO 2.4) Android等效于org.apache.commons.io.IOUtils.toByteArray - Android equivalent for org.apache.commons.io.IOUtils.toByteArray
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM