Question

Hi I am fairly an amateur at android so I might not be realizing something obvious.

I have a method that populates a global File Array variable with a list of flies in a specific directory. Problem is everything works fine if the directory has been made before by using my app to save a file there however when the user hasn't done that an error message is suppose to pop up saying they haven't saved a file yet.

I do a check if the directory exist but the app crashes when the directory has not been created.

This is what my code looks like any assistance would be appreciated

    private void getTemplates()
{       
    //Gets file directory for saved templates
    File finalMarkTemplateDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Final Mark Templates");

    //Checks if path exists in other word if any templates have been saved before
    if(finalMarkTemplateDir.exists())
    {
        templatePaths = finalMarkTemplateDir.listFiles();
    }
    else
    {
        Toast.makeText(this, "No previous templates have been saved.", Toast.LENGTH_LONG).show();
        setResult(RESULT_CANCELED);
        finish();
    }
}
Était-ce utile?

La solution 2

I managed to solve my problem when I call the setResult and finish methods I did not realize the flow of the program is returned to my onCreate method which meant the rest of my method calls in onCreate was still being called and they require the templatePaths array.

So basically I thought finish would stop the processing and move back to the calling class(using startActivityForResult). Instead I now call finish from my onCreate and use a boolean to determine if I could successfully access the directory.

    @Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.dialog_load_template);

    boolean fileLoadStatus = getTemplates();

    if(fileLoadStatus)
    {
        populateTemplateList(templatePaths);
    }
    else
    {
        setResult(RESULT_CANCELED);
        finish();
    }
}

    private boolean getTemplates()
{   
    boolean fileLoadStatus = false;

    //Gets file directory for saved templates
    File finalMarkTemplateDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Final Mark Templates");

    //Checks if path exists in other word if any templates have been saved before
    if(finalMarkTemplateDir.isDirectory())
    {
        templatePaths = finalMarkTemplateDir.listFiles();
        fileLoadStatus = true;
    }
    else
    {
        Toast.makeText(this, "No previous templates have been saved.", Toast.LENGTH_LONG).show();           
    }

    return fileLoadStatus;

}

Autres conseils

I am too an amateur, you have not created a file in your code, calling a new file() method does not create a file. Pls check that out

try {
            finalMarkTemplateDir.createNewFile();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top