1

When I connect to the database initially the Parse.com data connection works well. However, when the app is closed and then re-opened the connection to Parse does not work and either causes the app to crash or the app runs but all the data needed from the database is not loaded. Any ideas?

This is my initialisation which take place in the MainActivity.java

public class MainActivity extends Activity  {


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

    Parse.enableLocalDatastore(this);
    Parse.initialize(this);
} }

I have tried taking the initialising statement out to another class but this did not connect at all to Parse.com.

This is the logcat error when the app crashes:

02-02 19:04:53.545: E/AndroidRuntime(14130): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lfg.fyp.taste_a_saurus/com.lfg.fyp.taste_a_saurus.MainActivity}: java.lang.IllegalStateException: Parse#enableLocalDatastore(Context) must be invoked before Parse#initialize(Context)

LFG
  • 33
  • 7
  • 1
    try to not to initialize it in the activity, create an `application class` and put it in the `oncreate` of the application – Rod_Algonquin Feb 02 '16 at 18:18
  • any logcat? Did you check here: http://stackoverflow.com/questions/32057618/parse-initialize-freezes-my-app – FMiscia Feb 02 '16 at 18:18
  • Initalize it in an `Application` class, like so: http://stackoverflow.com/a/33285372/1269953 side note: Parse is going to be shutting down next year, I suggest you use an alternative such as Firebase – Pztar Feb 02 '16 at 18:22
  • Hi @FMiscia I needed to do that anyway so thank you however, it has not sorted the problem. – LFG Feb 02 '16 at 18:27
  • So I have made the other class which initialises the Parse connection, do i need to call it anywhere? It is in my manifest but does do anything (as in isn't connecting to the database) @Pztar thank you for your concern, I just need this for a presentation next month :) – LFG Feb 02 '16 at 18:32
  • Is the other class an `Application` class?, make sure your app extends it, and place your code in the `onCreate` of the `Application` class – Pztar Feb 02 '16 at 18:41
  • yes @Pztar the second class is an application class. You can't have two extends though... so how do I do it? This is my second class. public class StartParse extends Application{ public void onCreate(){ super.onCreate(); Parse.enableLocalDatastore(this); Parse.initialize(this); } – LFG Feb 02 '16 at 18:44
  • What do you mean you can't have two `extends` you should only be extending `Application` did you add it to your manifest at the `application` level not `activity` – Pztar Feb 02 '16 at 18:53
  • Oh I had it as an activity in the manifest, how would I write it as an application? ps thank you for your help! – LFG Feb 02 '16 at 19:02

1 Answers1

3

Declare your Application class in the manifest like this:

<application
        android:name="YourApplicationClassName" >

Where your Application class looks like this:

public class YourApplicationClassName extends Application {

@Override
public void onCreate() {
    super.onCreate();

    // Enable Local Datastore.
    Parse.enableLocalDatastore(this);

    // Register any ParseObject subclass. Must be done before calling Parse.initialize()
    ParseObject.registerSubclass(YourClass.class);

    Parse.initialize(this, "APPLICATION_ID", "CLIENT_KEY");    
}

}

Pztar
  • 4,274
  • 6
  • 33
  • 39
  • thank you for laying this out for me clearly. My code does not like the ParseObject line as it says that StartParse.class (the name of my class) is not applicable to the parameters. Any ideas? – LFG Feb 02 '16 at 19:13
  • That's for `ParseObject` classes. If you don't have one, then take it out. – Pztar Feb 02 '16 at 19:26
  • Thank you for all your help! – LFG Feb 02 '16 at 19:27