Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8348561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:43:42+00:00 2026-06-09T07:43:42+00:00

Here’s my current situation: Eclipse Win7 Samsung SII (for testing) I am writing an

  • 0

Here’s my current situation:
Eclipse
Win7
Samsung SII (for testing)

I am writing an Application that will read from a file stored privately on the device when it starts, and then, i want to write to that same file (overwrite basically) when the application’s onDestroy() is called.

I first tried to extend the application with my own class, and then read and write to that file on the constructor and destroy of that class, however, reading or writing within that class seems to not be allowed as my application simply crashes every time as it starts. So i thought about it for a while and resigned myself to doing this at the main menu, since this is the only activity that i do not "destroy" (or ActivityName.this.finish();). but my programmer brain would like to make this happen at the application level, and not the activity level.

So is it possible for me to read from the file in the Application class constructor i created, and write to the file in the onDestroy()? if so, how? My application just constantly crashes/won’t even start…

This is the code i use. this effectively reads a file, and stores some strings into global memory (spare me the “no no no”‘s please… Global vars are very useful to me), then overwrites the file when onDestroy() is called with the values in Global vars (i know this because it works at the activity level):

CONSTRUCTOR:
super();

    try 
    {
        String origprefs =",0,1-1";

        File f = new File(this.getFilesDir(),FILENAME);

        if(!f.exists())
        {
            FileOutputStream fos2 = new FileOutputStream(f);
            fos2.write(origprefs.getBytes());
            fos2.close();
        }

        FileInputStream fis;
        fis = openFileInput(FILENAME);
        StringBuffer fileContent = new StringBuffer("");

        byte[] buffer = new byte[1024];
        int length;
        while ((length = fis.read(buffer)) != -1) {
            fileContent.append(new String(buffer));
        }
        fis.close();

        origprefs = fileContent.toString();

        //set our globals
        Username = origprefs.split(",")[0];
        Score = origprefs.split(",")[1];
        Stage = origprefs.split(",")[2];

    } 
    catch (IOException e) 
    {
        throw new RuntimeException("Unable to either Read or Write to Match Two file.");
    }

onDestroy:
if(GameRunning)
    {
        try
        {
            FileOutputStream fis;
                fis = openFileOutput(FILENAME, this.MODE_PRIVATE);
                String fileContent = Username + "," + Score + "," + Stage;
                fis.write(fileContent.getBytes());
                fis.close();
        }
        catch(IOException e)
        {
            throw new RuntimeException("Unable to either Read or Write to Match Two file.");
        }
    }

Now, this is the same process, minus a few alterations to get the proper context, inside the MainMenu class. I just can’t wrap my head around why this would crash my app when i do it at the application level? thanks a ton for any help!

here’s the logcat entries
logcat is cryptic i find…

08-08 02:03:58.362: E/AndroidRuntime(10360): FATAL EXCEPTION: main
08-08 02:03:58.362: E/AndroidRuntime(10360): java.lang.RuntimeException: Unable to instantiate application maxovrdrv.FirstApp.MatchTwo: java.lang.NullPointerException
08-08 02:03:58.362: E/AndroidRuntime(10360):    at android.app.LoadedApk.makeApplication(LoadedApk.java:466)
08-08 02:03:58.362: E/AndroidRuntime(10360):    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3268)
08-08 02:03:58.362: E/AndroidRuntime(10360):    at android.app.ActivityThread.access$2200(ActivityThread.java:117)
08-08 02:03:58.362: E/AndroidRuntime(10360):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:973)
08-08 02:03:58.362: E/AndroidRuntime(10360):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-08 02:03:58.362: E/AndroidRuntime(10360):    at android.os.Looper.loop(Looper.java:130)
08-08 02:03:58.362: E/AndroidRuntime(10360):    at android.app.ActivityThread.main(ActivityThread.java:3691)
08-08 02:03:58.362: E/AndroidRuntime(10360):    at java.lang.reflect.Method.invokeNative(Native Method)
08-08 02:03:58.362: E/AndroidRuntime(10360):    at java.lang.reflect.Method.invoke(Method.java:507)
08-08 02:03:58.362: E/AndroidRuntime(10360):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
08-08 02:03:58.362: E/AndroidRuntime(10360):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
08-08 02:03:58.362: E/AndroidRuntime(10360):    at dalvik.system.NativeStart.main(Native Method)
08-08 02:03:58.362: E/AndroidRuntime(10360): Caused by: java.lang.NullPointerException
08-08 02:03:58.362: E/AndroidRuntime(10360):    at android.content.ContextWrapper.getFilesDir(ContextWrapper.java:178)
08-08 02:03:58.362: E/AndroidRuntime(10360):    at maxovrdrv.FirstApp.MatchTwo.<init>(MatchTwo.java:26)
08-08 02:03:58.362: E/AndroidRuntime(10360):    at java.lang.Class.newInstanceImpl(Native Method)
08-08 02:03:58.362: E/AndroidRuntime(10360):    at java.lang.Class.newInstance(Class.java:1409)
08-08 02:03:58.362: E/AndroidRuntime(10360):    at android.app.Instrumentation.newApplication(Instrumentation.java:957)
08-08 02:03:58.362: E/AndroidRuntime(10360):    at android.app.Instrumentation.newApplication(Instrumentation.java:942)
08-08 02:03:58.362: E/AndroidRuntime(10360):    at android.app.LoadedApk.makeApplication(LoadedApk.java:461)
08-08 02:03:58.362: E/AndroidRuntime(10360):    ... 11 more

Matt

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T07:43:44+00:00Added an answer on June 9, 2026 at 7:43 am

    You Should read the docs of onDestroy.. you should use onPause instead. read below for more detail

    do not count on this(onDestroy()) method being called as a place for saving data! For example, if an activity is editing data in a content provider, those edits should be committed in either onPause() or onSaveInstanceState(Bundle),

    protected void onDestroy ()
    Since: API Level 1
    

    Perform any final cleanup before an activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method.

    Note: do not count on this method being called as a place for saving data! For example, if an activity is editing data in a content provider, those edits should be committed in either onPause() or onSaveInstanceState(Bundle), not here. This method is usually implemented to free resources like threads that are associated with an activity, so that a destroyed activity does not leave such things around while the rest of its application is still running. There are situations where the system will simply kill the activity’s hosting process without calling this method (or any others) in it, so it should not be used to do things that are intended to remain around after the process goes away.
    Derived classes must call through to the super class’s implementation of this method. If they do not, an exception will be thrown.

    reference link :- Android onDestroy

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the scenario: I'm writing an app that will watch for any changes
Here's a query that works fine: SELECT rowid as msg_rowid, a, b, c FROM
Here is my work environment: Eclipse Juno as IDE with maven2 plugin on it
Here is the script I'm using, copied directly from Google: <script type=text/javascript> var _gaq
Here is the scenario. I'm writing my geo-ruby oracle adapter for Ruby On Rails
Here's what I'm trying to accomplish with this program: a recursive method that checks
Here is the problem that I am trying to solve. I have two folders
Here's a piece of code I copied from http://www.schillmania.com/content/projects/javascript-animation-1/demo/ Very simple JS animation: function
Here is my query: SELECT * FROM [GeoName] WHERE ((-26.3665122100029-Lat)*(-26.3665122100029-Lat))+((27.5978928658078-Long)*(27.5978928658078-Long)) < 0.005 ORDER BY
Here is my code (Say we have a single button on the page that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.