Android Question Stream closed

Discussion in 'Android Questions' started by Emerito, Apr 23, 2019.

  1. Emerito

    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)

    Code:
    Sub GPS_LocationChanged (Location1 As Location)
        
    If GPSonoff=True Then       
            Loc.Initialize
             LblLatitud.Text = 
    NumberFormat2(Location1.Latitude, 160False)
              LblLongitud.Text = 
    NumberFormat2(Location1.Longitude, 160False)
            LblVelocidad.Text=
    NumberFormat2(Location1.Speed*3.6160False)  & " 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
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Where are you closing the TextWriter? Set it to Null after closing to make IsInitialized return False.
     
  3. Emerito

    Emerito Member Licensed User

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

    Emerito Member Licensed User

    Code:
    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.
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    Code:
    tw.Close
    tw = 
    Null
    Better in this case:
    Code:
    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
     
  6. Emerito

    Emerito Member Licensed User

    Gracias. Tienes razón. Ya había corregido el código en el sentido que me indicas.
    Saludos.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice