Search code examples
androidparse-platform

Parse query with no current user


I have a working app on android and IOS.

The first activity on both is a log in screen. The second activity is a search function that allows you to find nearby users using parse.

However I am trying the make the app allow users to search without having to login.

This was easy to do with IOS.

In android I have changed my startup activity to activity 2 (the search activity) in the manifest file and it loads fine but obviously there is no current user.

When I try to run the query now, the app just stalls and in the debugger it says the user is null. I am guessing this is the problem here.

So I then tried to create an anonous user in the oncreate method using the lines..

  ParseAnonymousUtils.logIn(new LogInCallback() {
        @Override
        public void done(ParseUser user, ParseException e) {
            if (e != null) {
                Log.d("MyApp", "Anonymous login failed.");
            } else {
                Log.d("MyApp", "Anonymous user logged in.");
            }
        }
    });

This then fails with the message

java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File com.parse.ParsePlugins.getParseDir()' on a null object reference at com.parse.Parse.getParseDir(Parse.java:499)

Oddly enough though. This bit of code does work if i put it in activity 1, but the app then gets stuck when it tries to launch activity 2.

Does anyone know what is happening? Or how I can solve my original original problem?

Thanks


Solution

  • This error appears when Parse is not initialized. I'm guessing you put your Parse.initialize() code in your activity. When you go to another one, the initialization is lost. You need to extend your Application class so that it holds a single initialization for the whole app.

    public class Application extends android.app.Application {
    
        @Override
        public void onCreate() {
            super.onCreate();
            Parse.initialize(new Parse.Configuration.Builder(this)
                    .applicationId("id")
                    .clientKey("key")
                    .server("server")
                    .enableLocalDataStore()
                    .build()
            );
            ParseUser.enableAutomaticUser();
       }
    }
    

    And this in your AndroidManifest.xml

    <application
            android:name=".Application"
            android:allowBackup="true"
            android:icon="@drawable/logo"
            android:label="@string/app_name"
            android:theme="@style/AppTheme">