2

I am getting the following NPE while installing my app itself. Please help.

07-22 16:27:06.380: E/UA AP(6071): Unable to takeOff automatically
07-22 16:27:06.385: D/AndroidRuntime(6071): Shutting down VM
07-22 16:27:06.385: W/dalvikvm(6071): threadid=1: thread exiting with uncaught exception (group=0x415db2a0)
07-22 16:27:06.385: E/AndroidRuntime(6071): FATAL EXCEPTION: main
07-22 16:27:06.385: E/AndroidRuntime(6071): java.lang.RuntimeException: Unable to start receiver com.urbanairship.push.GCMPushReceiver: java.lang.NullPointerException
07-22 16:27:06.385: E/AndroidRuntime(6071): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2277)
07-22 16:27:06.385: E/AndroidRuntime(6071): at android.app.ActivityThread.access$1500(ActivityThread.java:140)
07-22 16:27:06.385: E/AndroidRuntime(6071): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
07-22 16:27:06.385: E/AndroidRuntime(6071): at android.os.Handler.dispatchMessage(Handler.java:99)
07-22 16:27:06.385: E/AndroidRuntime(6071): at android.os.Looper.loop(Looper.java:137)
07-22 16:27:06.385: E/AndroidRuntime(6071): at android.app.ActivityThread.main(ActivityThread.java:4898)
07-22 16:27:06.385: E/AndroidRuntime(6071): at java.lang.reflect.Method.invokeNative(Native Method)
07-22 16:27:06.385: E/AndroidRuntime(6071): at java.lang.reflect.Method.invoke(Method.java:511)
07-22 16:27:06.385: E/AndroidRuntime(6071): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
07-22 16:27:06.385: E/AndroidRuntime(6071): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
07-22 16:27:06.385: E/AndroidRuntime(6071): at dalvik.system.NativeStart.main(Native Method)
07-22 16:27:06.385: E/AndroidRuntime(6071): Caused by: java.lang.NullPointerException
07-22 16:27:06.385: E/AndroidRuntime(6071): at com.urbanairship.UAirship.getPackageName(Unknown Source)
07-22 16:27:06.385: E/AndroidRuntime(6071): at com.urbanairship.push.GCMPushReceiver.onReceive(Unknown Source)
07-22 16:27:06.385: E/AndroidRuntime(6071): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2270)
07-22 16:27:06.385: E/AndroidRuntime(6071): ... 10 more

Even though I get this excepting I am able to start the app. On running :

url -X POST -u "<app id>:<mastersecret>" -H "Content-Type: application/json" --data '{"android": {"alert": "TestPushtoAPID"}, "apids": ["****4"]}' https://go.urbanairship.com/api/push/

I am getting the following response :

{
    "push_id": "33e95250-f2b9-11e2-a8d0-14feb5d31f47"
}

In spite of this, I am not able to see any notification on my device.

Part of Android Manifest 
<!-- REQUIRED for Urban Airship GCM--> 
<receiver android:name="com.urbanairship.CoreReceiver" /> 
<receiver android:name="com.urbanairship.push.GCMPushReceiver" android:permission="com.google.android.c2dm.permission.SEND"> 
    <intent-filter> 
        <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
        <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
    </intent-filter>
</receiver>

Part of MainActivity:

UAirship.takeOff(this.getApplication(), options); 
PushManager.enablePush(); 
PushManager.shared().setIntentReceiver(IntentReceiver.class); 
String apid = PushManager.shared().getAPID();
t0mm13b
  • 34,087
  • 8
  • 78
  • 110
Pradeep Vairamani
  • 4,004
  • 3
  • 36
  • 59
  • What have you tried? What have you uncovered? The null pointer exception is because you are using a variable that is not instantiated somewhere within `Unable to start receiver com.urbanairship.push.GCMPushReceiver: java.lang.NullPointerException 07-22 16:27:06.385: E/AndroidRuntime(6071): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2277)` Have you your broadcast receiver declared in your manifest or have you instantiated it? – t0mm13b Jul 22 '13 at 11:49
  • Please show the code for that to confirm. – t0mm13b Jul 22 '13 at 12:34
  • OP: Please stop posting code in the comments, re-edit your question as those comments make the code unreadable. – t0mm13b Jul 22 '13 at 14:29
  • Is that actually in your manifest? You're missing closing tags... – t0mm13b Jul 22 '13 at 15:09
  • OP: I have edited and reformatted your code, please delete those comments that has the code to clean this up. – t0mm13b Jul 22 '13 at 15:17

5 Answers5

3

Actually the problem was in implementation of the library correctly.

The takeOff as well as the other configuration code should be in the OnCreate of the Application class which is used for maintaining global state of the application.This is where most of us go wrong.We put it in the foreground Activitys OnCreate.

Next you need to mention the fully qualified name of the Application class which you are using in the AndroidManifest.xml within your application tag this way:

 <application
        android:name="com.popa.app.MyApplication"
        ..

This is how it is to be implemented as mentioned in the original docs of UA.

Nezam
  • 4,122
  • 3
  • 32
  • 49
1

The problem seems to have been because the particular apid got attached to a different UA app that I used for debugging. I created a new app and assumed that the same apid can get attached to this app too. That is not the case. I tried using a new device and it is working fine.

Pradeep Vairamani
  • 4,004
  • 3
  • 36
  • 59
1

I had the same NPE as you but had a different reason/solution. In my case I had the proper UA services/receivers in my AndroidManifest.xml, but forgot to call UAirship.takeOff(). After I included the following code, the NPE went away:

    UAirship.takeOff(application, AirshipConfigOptions.loadDefaultOptions(application));
    PushManager.enablePush();
Jo Jo
  • 7,245
  • 5
  • 34
  • 49
  • I found another (similar) cause to the above NPE. In order to improve performance, I delayed the call to takeOff() until 30 seconds after my app started and I'm seeing the NPE again. It seems to me that the UrbanAirship code registers its GCMPushReceiver even if takeOff() is not invoked. That GCMPushReceiver then reacts to GCM broadcasts and throws an NPE if takeOff() has not been invoked yet. This was probably either a mistake the UA folks made when coding their UA Android library or a compromise they made due to the way GCM works. – Jo Jo Mar 25 '14 at 13:46
0

From the OP's comments, and reading the documentation source, it appears it is incorrectly instantiated.

If IntentReceiver is a subclass of BroadcastReceiver, as per in the documentation, then the following should do it:

class IntentReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent argIntent) {
        // Handle it here
    }
}

The actual instantiation for it would be in this form:

IntentReceiver intentRcvr = new BroadcastReceiver();
PushManager.enablePush();
PushManager.shared().setIntentReceiver(intentRcvr); // TAKE NOTE!
// Rest of code.
t0mm13b
  • 34,087
  • 8
  • 78
  • 110
0

This makes me suspect that you haven't set up your AndroidManifest.xml correctly:

07-22 16:27:06.385: E/AndroidRuntime(6071): Caused by: java.lang.NullPointerException 07-22 16:27:06.385: E/AndroidRuntime(6071): at com.urbanairship.UAirship.getPackageName(Unknown Source)

Have you added the fully qualified package name of your IntentReceiver to the AndroidManifest.xml? If I comment mine out, I get your exact error.

<!-- OPTIONAL, if you want to receive push, push opened and registration completed intents -->
<receiver android:name="com.XXXX.IntentReceiver" />

@t0mm13b Unfortunately I can't get your code to compile - you can't instantiate BroadcastReceiver, and PushManager.shared().setIntentReceiver() doesn't accept instantiated objects, only Class<? extends BroadcastReceiver> - therefore IntentReceiver.class is actually the correct parameter to pass through.

Manifest
  • 46
  • 1
  • 3