UrbanAirship NPE

2019-07-13 17:28发布

问题:

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();

回答1:

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.



回答2:

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.



回答3:

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();


回答4:

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.


回答5:

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.