Go Back   Android Forums > Android Development > Application Development
Application Development Dev Lounge for the Coder Folks
Gamers - Check out our new sister sites!
Nintendo Wii U!    |    OUYA - $99 Android System!

test: Reply
 
LinkBack Thread Tools
Old December 1st, 2010, 02:32 PM   #1 (permalink)
New Member
Thread Author (OP)
 
Join Date: Dec 2010
Posts: 1
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default Latent java.IOException:Write is closed errors when using BufferedWrite

OK, I'm just starting out with my first non-trivial app using Eclipse and I'm having some issues. I'm trying to write accelerometer data to a file on the sd-card.

In my Activity class I have a couple of things for file access:
BufferedWriter out;
FileWriter gpxwriter;

Inside onCreate I set them up and do a test write to the file:
Code:
Log.d("AccelerometerText", "onCreate ... Done");
File gpxfile = new File(root, "RawAccelData2.txt");
FileWriter gpxwriter = new FileWriter(gpxfile);
out = new BufferedWriter(gpxwriter);
out.write("Time (ms); X Value (g);Y Value(g);Z Value (g)");
This code works, and I can open the file and see the text. great. If I exit at this point all is fine and dandy.

But.... my activity class implements SensorEventListener, so I've implemented the following which gets called every time the sensor data changes. At that point I can see the accel data get updated in real time on the screen using settext calls. works great.....

Code:
public void onSensorChanged(SensorEvent sensorEvent) {
       synchronized (this) {
                 long mytime = 0; //System.currentTimeMillis();
                textAccelerationX.setText("X Acceleration time:" + mytime + " " + Float.toString(sensorEvent.values[0]));
                textAccelerationY.setText("Y Acceleration time:" + mytime + " " + Float.toString(sensorEvent.values[1]));
                textAccelerationZ.setText("Z Acceleration time:" + mytime + " " + Float.toString(sensorEvent.values[2]));
                  
                BigBuf[runningCount] = Float.toString(sensorEvent.values[0]);
                                  
                if(runningCount==99) {
                    runningCount=0;
                    EnterCount++;
                    Log.d("onSensorChanged", "EnterCount " + EnterCount);
                    Log.d("onSensorChanged", "out " + out.toString());
                    try {
                        out.write("Hello");
                        out.newLine();
                        } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } 
            }
            runningCount++;
       }
       }
I let the app go for a few minutes and hit the exit button which executes:
Code:
case R.id.ButtonExit:
            Log.d("AccelerometerText", "onClick ... buttonexit");
            RunningZaccel+=0.8;
            try {
                out.flush();
                out.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            this.finish(); 
            break;
        }


I can see all the events get updated in my LogCat window using DDMS.

The problem is that as soon as I hit my 'exit' button, I get a flurry on java.IO.BufferedWriter java.io.IOExceptions and these continue to scroll by even after the app has been exited and off the screen for minutes:

Code:
D/onSensorChanged(15160): EnterCount 1541
D/onSensorChanged(15160): out java.io.BufferedWriter@44763778
W/System.err(15160): java.io.IOException: Writer is closed.
W/System.err(15160):    at java.io.BufferedWriter.write(BufferedWriter.java:308)
W/System.err(15160):    at java.io.Writer.write(Writer.java:152)
W/System.err(15160):    at com.example.AccelerometerTest.AccelerometerTest.onSensorChanged(AccelerometerTest.java:202)
W/System.err(15160):    at android.hardware.SensorManager$ListenerDelegate$1.handleMessage(SensorManager.java:449)
W/System.err(15160):    at android.os.Handler.dispatchMessage(Handler.java:99)
W/System.err(15160):    at android.os.Looper.loop(Looper.java:123)
W/System.err(15160):    at android.app.ActivityThread.main(ActivityThread.java:4627)
W/System.err(15160):    at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err(15160):    at java.lang.reflect.Method.invoke(Method.java:521)
W/System.err(15160):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
W/System.err(15160):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
W/System.err(15160):    at dalvik.system.NativeStart.main(Native Method)
D/dalvikvm(15160): GC_FOR_MALLOC freed 7981 objects / 523992 bytes in 65ms
D/onSensorChanged(15160): EnterCount 1542
I obviously doing something wrong, any ideas?
Am I not closing the file correctly?
Why do I see IO errors even after the app is long gone and no longer running?
can I save data in this manner (there are going to of file accesses)?

scott1234 is offline  
Reply With Quote
Sponsors
Reply


Go Back   Android Forums > Android Development > Application Development
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 09:44 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.