segmentio / analytics-android

The hassle-free way to add analytics to your Android app.
https://segment.com/docs/sources/mobile/android/
MIT License
377 stars 214 forks source link

NullPointerException in MoEngageIntegration #358

Closed Nizam-Accedo closed 9 years ago

Nizam-Accedo commented 9 years ago

Integrated library though gradle : com.segment.analytics.android:analytics-integration-moengage:3.2.1+

In my Android Application class, I have this

        Analytics analytics = new Analytics.Builder(this, BuildConfig.SEGMENT_WRITEKEY).build();
        Analytics.setSingletonInstance(analytics);
        ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
        ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        activityManager.getMemoryInfo(mi);
        long availableMegs = mi.availMem / 1048576L;
        Analytics.with(this).track("app_start", new Properties().putValue("free_mem",availableMegs+"MB"));

This works fine in Lollipop, but got crash in Kitkat(4.4.4) The crash log is as follows:

10-01 17:29:14.396  23402-23921/tv.accedo.airtel.wynk E/AndroidRuntime﹕ FATAL EXCEPTION: SegmentAnalytics-IntegrationManager
Process: tv.accedo.airtel.wynk, PID: 23402
java.lang.NullPointerException
        at com.segment.analytics.internal.integrations.MoEngageIntegration.track(MoEngageIntegration.java:143)
        at com.segment.analytics.IntegrationOperation$10.run(IntegrationOperation.java:171)
        at com.segment.analytics.IntegrationManager.run(IntegrationManager.java:391)
        at com.segment.analytics.IntegrationManager.performEnqueue(IntegrationManager.java:371)
        at com.segment.analytics.IntegrationManager.performEnqueue(IntegrationManager.java:366)
        at com.segment.analytics.IntegrationManager$IntegrationManagerHandler.handleMessage(IntegrationManager.java:470)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.os.HandlerThread.run(HandlerThread.java:61)
f2prateek commented 9 years ago

Weird that it ran fine on Lollipop, but the issue is basically that MoEngage requires an activity to be started (https://github.com/segmentio/analytics-android/blob/master/analytics-integrations/moengage/src/main/java/com/segment/analytics/internal/integrations/MoEngageIntegration.java#L67) before tracking events.

f2prateek commented 9 years ago

Also related: http://docs.moengage.com/docs/android-coding-it-up

Atrix1987 commented 9 years ago

@nizamcs where are you calling Analytics.with(context).track() ?

Nizam-Accedo commented 9 years ago

Hi, The call was from Application class, immediately after the library initialization. So, I have to move the line of code to the launcher activity, leaving the initialization behind. Right? Regards, Nizam On Oct 2, 2015 11:34 PM, "Abhishek Nandi" notifications@github.com wrote:

@nizamcs https://github.com/nizamcs where are you calling Analytics.with(context).track() ?

— Reply to this email directly or view it on GitHub https://github.com/segmentio/analytics-android/issues/358#issuecomment-145106939 .

f2prateek commented 9 years ago

@nizamcs Yes, that would work for now.

I think we should try to handle it in the Segment integration, and defer calling MoEngage until an activity is initialized — but I'm unsure what the behaviour should be when there isn't an Activity created (e.g. say this happened in a background service). @Atrix1987 any suggestions?

Atrix1987 commented 9 years ago

@nizamcs call track in your activity. @f2prateek i'll change the implementation, to handle this. Had tied up MoEHelper to activity because it is required for InApps and some other features. But yes both can be handled. When is your next release, would send a PR before that ?

Nizam-Accedo commented 9 years ago

Hi,

I moved the 'Track' to the launcher activity and is working fine now. Also, waiting for the updated library, so we can use it even without an activity. Thank you.

Regards, Nizam CS

Thanks & Regards, Nizam CS

On 3 October 2015 at 10:26, Abhishek Nandi notifications@github.com wrote:

@nizamcs https://github.com/nizamcs call track in your activity. @f2prateek https://github.com/f2prateek i'll change the implementation, to handle this. Had tied up MoEHelper to activity because it is required for InApps and some other features. But yes both can be handled. When is your next release, would send a PR before that ?

— Reply to this email directly or view it on GitHub https://github.com/segmentio/analytics-android/issues/358#issuecomment-145200580 .

f2prateek commented 9 years ago

@Atrix1987 anytime is fine, we've already made 2 releases since this issue was opened