我在安装我的应用程序时获得了以下NPE。请帮忙。
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
即使我得到这个除外,我能够启动应用程序。在跑步时:
url -X POST -u "<app id>:<mastersecret>" -H "Content-Type: application/json" --data '{"android": {"alert": "TestPushtoAPID"}, "apids": ["****4"]}' https://go.urbanairship.com/api/push/
我收到以下回复:
{
"push_id": "33e95250-f2b9-11e2-a8d0-14feb5d31f47"
}
尽管如此,我仍无法在设备上看到任何通知。
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>
MainActivity的一部分:
UAirship.takeOff(this.getApplication(), options);
PushManager.enablePush();
PushManager.shared().setIntentReceiver(IntentReceiver.class);
String apid = PushManager.shared().getAPID();
答案 0 :(得分:3)
实际上问题在于正确实现库。
takeOff
以及其他配置代码应位于用于Application
的maintaining global state of the application类的OnCreate
。这是我们大多数人去的地方错误。我们把它放在前景Activity
s OnCreate
。
接下来,您需要以这种方式在应用标记中的Application
中提及AndroidManifest.xml
类的完全限定名称:
<application
android:name="com.popa.app.MyApplication"
..
是如original docs of UA中提到的那样实现的。
答案 1 :(得分:1)
问题似乎是因为特定的apid附加到我用于调试的不同UA应用程序。我创建了一个新的应用程序,并假设同样的apid也可以附加到这个应用程序。事实并非如此。我尝试使用新设备并且工作正常。
答案 2 :(得分:1)
我和你有相同的NPE,但有不同的理由/解决方案。在我的情况下,我在AndroidManifest.xml中有适当的UA服务/接收器,但忘了调用UAirship.takeOff()。在我包含以下代码之后,NPE消失了:
UAirship.takeOff(application, AirshipConfigOptions.loadDefaultOptions(application));
PushManager.enablePush();
答案 3 :(得分:0)
根据OP的评论和阅读documentation来源,它似乎被错误地实例化了。
如果IntentReceiver
是BroadcastReceiver
的子类,如文档中所示,那么以下内容应该这样做:
class IntentReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent argIntent) {
// Handle it here
}
}
它的实际实例化将采用以下形式:
IntentReceiver intentRcvr = new BroadcastReceiver();
PushManager.enablePush();
PushManager.shared().setIntentReceiver(intentRcvr); // TAKE NOTE!
// Rest of code.
答案 4 :(得分:0)
这让我怀疑您没有正确设置AndroidManifest.xml:
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)
您是否已将IntentReceiver的完全限定包名添加到AndroidManifest.xml?如果我发表评论,我会收到您的确切错误。
<!-- OPTIONAL, if you want to receive push, push opened and registration completed intents -->
<receiver android:name="com.XXXX.IntentReceiver" />
@ t0mm13b遗憾的是我无法让您的代码进行编译 - 您无法实例化BroadcastReceiver
,而PushManager.shared().setIntentReceiver()不接受实例化的对象,只有Class<? extends BroadcastReceiver>
- 因此{{ 1}}实际上是要通过的正确参数。