Loading if txt is empty on android (FATAL EXCEPTION: Animation Thread error)

edited January 2017 in Android Mode

I've been trying to make a program that tests whether it is the first time the program is launched. The idea is that upon launch, a 'setup' function is run that reads a .txt file located in the data folder, and if the .txt file is empty, it should write something into the .txt file(so that the next time the program is launched, the .txt file is no longer empty) and perform some function. (for testing i've set it up to draw a red circle) If there is something within the .txt file, (the program has been launched before) it should perform some other function (here i've set it up to draw a green circle)

The problem is, that while the testing for whether the folder empty, runs fine in java mode on a PC, and does what it is supposed to, it crashes and provides me with this error in android mode;

Udklip

Here is the setupcode:

void setup() {
  size(displayWidth,displayHeight);
  //size(200,200);
  orientation(PORTRAIT);
  gt(); //Checks if it is the first time the program is launched
}

And the main code:

//BufferedReader
BufferedReader reader;
String tjek;
//PrintWriter
PrintWriter foerstetjek;
int findgtjek=1;

void gt() { 
  reader = createReader("findgangtjek.txt"); //Establishes a reader for the findgangtjek.txt file, which reads the data folder.
  //
  try { 
    tjek = reader.readLine(); //reading the findgangtjek.txt doc
  } catch (IOException e) { //If findgangtjek.txt is empty
    tjek = null; //If the text file is empty the String 'tjek' is set as null
  }      
  if (tjek == null) { //If the String 'tjek' is null (doc is empty, first time program is launched)
    //indicator in the form of a red circle
    fill(#F50A0A); 
    ellipse(30,30,30,30); 

    // // // // // // // // This is the part that probably doesn't work!
    foerstetjek = createWriter("data/findgangtjek.txt"); // creating a writer that writes to findgangtjek.txt within the data folder
    foerstetjek.println(findgtjek); // prints the value (1) of int variable 'findgtjek' into the txt file
    foerstetjek.flush(); //flushes PrintWriter
    //foerstetjek.close(); // doesn't seem to change much for me
         // // // // // // // //

  } else { //If there is something in the txt file (program has been launched before)
     fill(#0CEA23); //Green... 
    ellipse(30,30,30,30); //...Indicator circle
  }
}

It is willing to show a green circle if there is something in the txt file, with the writer disabled, it is also able to show a red circle.

But with the writer still enabled and the '/' in foerstetjek = createWriter("data/findgangtjek.txt"); changed to a backslash ('\') it is willing to run the entire program without complaining, though it doesn't change the .txt file, and therefore doesn't amount to anything useful. I've tried giving permission to read from and write to external storage, writing the code into 'APDE', loading the program from the phonesstorage instead of from dropbox.

So the problem is that in the code shown above, it provides me with an (FATAL EXCEPTION: Animation Thread) error, while with the backslash 'fix' it doesn't write something into .txt, or writes it to somewhere it can't read it from next time the program is launched.

Any ideas on what is wrong, and what can be done to fix it? Thanks

This thread is similar to these: https://forum.processing.org/two/discussion/18766/saving-loading https://forum.processing.org/two/discussion/2643/get-the-error-fatal-exception-animation-thread-everytime-i-compile-my-app

Answers

  • Did you try adding data\ or data/ to createReader() path?

  • @randomusername===

    see the error log:: you cannot use a file separator inside what is supposed to be a file; so it s easy to change the code::

        import android.os.Environment;
    
        BufferedReader reader;
        String tjek;
        //PrintWriter
        PrintWriter foerstetjek;
        int findgtjek=1;
    
    
    
        void setup() {
          size(displayWidth,displayHeight);
          //size(200,200);
          orientation(PORTRAIT);
          String directory = new String(Environment.getExternalStorageDirectory().getAbsolutePath() );
          gt(directory); //Checks if it is the first time the program is launched
        }
    
        //BufferedReader
    
    
        void gt(String d) { 
          reader = createReader("essai.txt"); //Establishes a reader for the findgangtjek.txt file, which reads the data folder.
          //
          try { 
            tjek = reader.readLine(); //reading the findgangtjek.txt doc
          } catch (IOException e) { //If findgangtjek.txt is empty
            tjek = null; //If the text file is empty the String 'tjek' is set as null
          }      
          if (tjek == null) { //If the String 'tjek' is null (doc is empty, first time program is launched)
            //indicator in the form of a red circle
            fill(#F50A0A); 
            ellipse(30,30,30,30); 
    
            // // // // // // // // This is the part that probably doesn't work!
            foerstetjek = createWriter(d +"/" + "essai.txt"); // creating a writer that writes to findgangtjek.txt within the data folder
            foerstetjek.println(findgtjek); // prints the value (1) of int variable 'findgtjek' into the txt file
            foerstetjek.flush(); //flushes PrintWriter
            //foerstetjek.close(); // doesn't seem to change much for me
                 // // // // // // // //
    
          } else { //If there is something in the txt file (program has been launched before)
             fill(0,255,0); //Green... 
            ellipse(30,30,30,30); //...Indicator circle
          }
        }
    
  • While none of the above suggestions were able to solve the problem on their own, ''combined'' the program is working, here is the final code:

    import android.os.Environment;
    
    BufferedReader reader;
    String tjek;
    PrintWriter foerstetjek;
    int findgtjek=7;
    
    void setup() {
      size(displayWidth,displayHeight);
      //size(200,200);
      orientation(PORTRAIT);
      gtf(); //Checks if it is the first time the program is launched
    }
    
    void gtf() { 
      String hovedmappe = new String(Environment.getExternalStorageDirectory().getAbsolutePath() ); //gets me to the internal storage folder (/storage/emulated/0)
      String gemmappe=("gemprojekt"); //Creating a string to further procede into a folder i've created within the internal storage folder, called "gemprojekt".
      reader = createReader(hovedmappe + "/" + gemmappe + "/" + "gt.txt"); //Establishes a reader for the gt.txt file, which reads from /storage/emulated/0/gemprojekt
      //
      try { 
        tjek = reader.readLine(); //reading the gt.txt file
      } catch (IOException e) { //If gt.txt is empty
        tjek = null; //If the text file is empty the String 'tjek' is set as null
      }      
      if (tjek == null) { //If the String 'tjek' is null (doc is empty, first time program is launched)
        //indicator in the form of a red circle
        fill(#F50A0A); 
        ellipse(30,30,30,30); 
        //
        foerstetjek = createWriter(hovedmappe + "/" + gemmappe + "/" + "gt.txt"); // creating a writer that writes to gt.txt within the 'gemprojekt' folder
        foerstetjek.println(findgtjek); // prints the value (7) of int variable 'findgtjek' into the .txt file
        foerstetjek.flush(); //flushes PrintWriter
        foerstetjek.close(); //closes PrintWriter
      } else { //If there is something in the .txt file (program has been launched before)
         fill(0,255,0); //Green... 
        ellipse(30,30,30,30); //...Indicator circle
      }
    }
    

    I'm not quite sure why Environment.getExternalStorageDirectory().getAbsolutePath() ) Gets me into the internal storage folder, but I figured I should atleast branch out once, to have somewhere to properly save the gt.txt file.

    Thanks to both Lord_of_the_Galaxy and akenaton for their help!

Sign In or Register to comment.