Android Question Stream closed

Emerito

Member
Licensed User
Perdón por mi ignorancia para publicar.

obtengo el siguiente error en la primera línea de grabación: tw.WriteLine(Loc.Latitude)

java.io.IOException: Stream closed
at java.io.BufferedWriter.ensureOpen(BufferedWriter.java:116)
at java.io.BufferedWriter.write(BufferedWriter.java:221)
at java.io.Writer.write(Writer.java:157)
at anywheresoftware.b4a.objects.streams.File$TextWriterWrapper.WriteLine(File.java:579)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:777)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:354)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:176)
at anywheresoftware.b4a.gps.GPS$1.onLocationChanged(GPS.java:65)
at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:297)
at android.location.LocationManager$ListenerTransport.-wrap0(LocationManager.java)
at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:242)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

B4X:
Sub GPS_LocationChanged (Location1 As Location)
    If GPSonoff=True Then       
        Loc.Initialize
         LblLatitud.Text = NumberFormat2(Location1.Latitude, 1, 6, 0, False)
          LblLongitud.Text = NumberFormat2(Location1.Longitude, 1, 6, 0, False)
        LblVelocidad.Text=NumberFormat2(Location1.Speed*3.6, 1, 6, 0, False)  & " Km/h"
        
        Loc.Time = Location1.Time
        Loc.Longitude = Location1.Longitude
        Loc.Latitude = Location1.Latitude
        Loc.Bearing=Location1.Bearing
        Loc.Speed=Location1.speed
        Loc.Altitude=Location1.Altitude
    
        
        Año= DateTime.Getyear(Loc.Time)
        Mes=DateTime.GetMonth(Loc.Time)
        Dia=DateTime.GetDayOfMonth(Loc.Time)
        HoraGPS= DateTime.GetHour(Loc.Time)
        MinutoGPS= DateTime.GetMinute(Loc.Time)
        SegundoGPS= DateTime.GetSecond(Loc.Time)
            
        Main.LATITUD=Location1.Latitude
        Main.LONGITUD=Location1.Longitude
        LstLatitudLongitud.Add(Location1.Latitude)
        LstLatitudLongitud.Add(Location1.Longitude)
        
        If tw.IsInitialized=False Then
         tw.Initialize( File.OpenOutput(Main.DirTrackLog,Main.TrackLogFichero,True))
        Else
        'escribe en el fichero .TRL
        If Main.Tracklog=True Then
                If (File.exists(Main.DirTrackLog,Main.TrackLogFichero)) = True Then
                    tw.WriteLine(Loc.Latitude)
                    tw.WriteLine(Loc.Longitude)
                    tw.WriteLine(Año)
                    tw.WriteLine(Mes)
                    tw.WriteLine(Dia)
                    tw.WriteLine(HoraGPS)
                    tw.WriteLine(MinutoGPS)
                    tw.WriteLine(SegundoGPS)
                    tw.WriteLine(Loc.Bearing)
                    tw.WriteLine(Loc.Speed)
                    tw.WriteLine(Loc.Altitude)
                End If
        End If
    End If
 

Erel

Administrator
Staff member
Licensed User
Where are you closing the TextWriter? Set it to Null after closing to make IsInitialized return False.
 

Emerito

Member
Licensed User
Mis disculpas, faltaba la última línea en el código:
tw.Close
¿Cómo lo establezco en Null?
 

Emerito

Member
Licensed User
B4X:
If tw.IsInitialized=False Then
         tw.Initialize( File.OpenOutput(Main.DirTrackLog,Main.TrackLogFichero,True))
        Else
        'escribe en el fichero .TRL
            If Main.Tracklog=True Then
                    If (File.exists(Main.DirTrackLog,Main.TrackLogFichero)) = True Then
                        tw.WriteLine(Loc.Latitude)
                        tw.WriteLine(Loc.Longitude)
                        tw.WriteLine(Año)
                        tw.WriteLine(Mes)
                        tw.WriteLine(Dia)
                        tw.WriteLine(HoraGPS)
                        tw.WriteLine(MinutoGPS)
                        tw.WriteLine(SegundoGPS)
                        tw.WriteLine(Loc.Bearing)
                        tw.WriteLine(Loc.Speed)
                        tw.WriteLine(Loc.Altitude)
                    End If
            End If
        End If
        tw.Close
Continua el mismo error.
 

Erel

Administrator
Staff member
Licensed User
B4X:
tw.Close
tw = Null
Better in this case:
B4X:
 If Main.Tracklog=True Then
             tw.Initialize( File.OpenOutput(Main.DirTrackLog,Main.TrackLogFichero,True))
                        tw.WriteLine(Loc.Latitude)
                        tw.WriteLine(Loc.Longitude)
                        tw.WriteLine(Año)
                        tw.WriteLine(Mes)
                        tw.WriteLine(Dia)
                        tw.WriteLine(HoraGPS)
                        tw.WriteLine(MinutoGPS)
                        tw.WriteLine(SegundoGPS)
                        tw.WriteLine(Loc.Bearing)
                        tw.WriteLine(Loc.Speed)
                        tw.WriteLine(Loc.Altitude)
                        tw.Close
            End If
 

Emerito

Member
Licensed User
Gracias. Tienes razón. Ya había corregido el código en el sentido que me indicas.
Saludos.
 
Top