Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse.com runtime crash - android

i am getting a lot of reports from users about my app crashing. The constant error appears to be associated with my parse.com initialisation, however, i have set it up as outlined in the parse tutorial.

here is the Stack Trace:

java.lang.RuntimeException: Unable to start receiver com.parse.ParseBroadcastReceiver: java.lang.RuntimeException: applicationContext is null. You must call Parse.initialize(context, applicationId, clientKey) before using the Parse library.
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2580)
at android.app.ActivityThread.access$1700(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: applicationContext is null. You must call Parse.initialize(context, applicationId, clientKey) before using the Parse library.
at com.parse.Parse.checkContext(Parse.java:606)
at com.parse.Parse.getApplicationContext(Parse.java:214)
at com.parse.ManifestInfo.getContext(ManifestInfo.java:322)
at com.parse.ManifestInfo.getPackageManager(ManifestInfo.java:330)
at com.parse.ManifestInfo.getPackageInfo(ManifestInfo.java:356)
at com.parse.ManifestInfo.deviceSupportsGcm(ManifestInfo.java:441)
at com.parse.ManifestInfo.getPushType(ManifestInfo.java:210)
at com.parse.PushService.startServiceIfRequired(PushService.java:168)
at com.parse.ParseBroadcastReceiver.onReceive(ParseBroadcastReceiver.java:19)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2573)
... 10 more

and here is my initialisation code:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_screen);
        Parse.initialize(this, "hGG5RdgNVdI7eCeZynV32lWYXywQRHkpp5zLdY7Q", "TwmNbpBYEt4u3euE3lzNIgwyroSl8RPGF2dJFsPv");

        ParseInstallation.getCurrentInstallation().saveInBackground();

Can anybody see what is causing this error, and how to fix it?

below is my receiver code:

public static class Receiver extends ParsePushBroadcastReceiver {
        private String notificationText;
        private Boolean notificationreceived = false;
        public Receiver(){
        }
        private static final String TAG = "MyNotificationsReceiver";

        @Override
        public void onPushOpen(Context context, Intent intent) {
            Log.e("Push", "Clicked");
            Intent i = new Intent(context, HomeScreen.class);
            notificationreceived = true;
            i.putExtra("alert",notificationText);
            i.putExtra("alertreceived", notificationreceived);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
            Scb998.scb988b=true;

                try {
                    JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));

                    Scb998.msg = json.getString("alert");


                } catch (JSONException e) {
                    Log.d(TAG, "JSONException: " + e.getMessage());

            }
        }

    }
like image 277
scb998 Avatar asked Dec 18 '14 02:12

scb998


1 Answers

Move you Parse initialization into your App class (extended from Application)

public class App extends Application {
   @Override
   public void onCreate() {
        super.onCreate();
        Parse.initialize(this, "hGG5RdgNVdI7eCeZynV32lWYXywQRHkpp5zLdY7Q", "TwmNbpBYEt4u3euE3lzNIgwyroSl8RPGF2dJFsPv");
        ParseInstallation.getCurrentInstallation().saveInBackground();
    }
}

And of course refer to it your AndroidManifest.xml

<application
    android:name=".app.App"
    ....
</application>

Reason of crash is next. When your app is at background, it can be killed by system. From Google guide

A process holding an activity that's not currently visible to the user (the activity's onStop() method has been called). These processes have no direct impact on the user experience, and the system can kill them at any time to reclaim memory for a foreground, visible, or service process. Usually there are many background processes running, so they are kept in an LRU (least recently used) list to ensure that the process with the activity that was most recently seen by the user is the last to be killed. If an activity implements its lifecycle methods correctly, and saves its current state, killing its process will not have a visible effect on the user experience, because when the user navigates back to the activity, the activity restores all of its visible state. See the Activities document for information about saving and restoring state.

When you app receives push notification, then parse will not be initialized, because you initialize it at activity onCreate method, which won't be called.

like image 60
gio Avatar answered Oct 21 '22 12:10

gio