我刚刚在我的项目中实现了Pushbots库。如果我的应用程序正在运行,推送通知效果很好。但是如果应用程序没有运行它总是崩溃。 我使用了logcat,它说:
09-26 02:40:28.143: E/AndroidRuntime(18464): FATAL EXCEPTION:
IntentService[GCMIntentService--1]
09-26 02:40:28.143: E/AndroidRuntime(18464): java.lang.NullPointerException
09-26 02:40:28.143: E/AndroidRuntime(18464): at
com.pushbots.push.Pushbots.regID(Pushbots.java:409)
09-26 02:40:28.143: E/AndroidRuntime(18464): at
com.pushbots.push.Pushbots.sendStatsToServer(Pushbots.java:282)
09-26 02:40:28.143: E/AndroidRuntime(18464): at
com.pushbots.push.GCMIntentService.onMessage(GCMIntentService.java:112)
09-26 02:40:28.143: E/AndroidRuntime(18464): at com.google.android.gcm.GCMBaseIntentService.onHandleIntent(GCMBaseIntentService.java:191)
09-26 02:40:28.143: E/AndroidRuntime(18464): at
android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
09-26 02:40:28.143: E/AndroidRuntime(18464): at
android.os.Handler.dispatchMessage(Handler.java:99)
09-26 02:40:28.143: E/AndroidRuntime(18464): at
android.os.Looper.loop(Looper.java:137)
09-26 02:40:28.143: E/AndroidRuntime(18464): at
android.os.HandlerThread.run(HandlerThread.java:60)
09-26 02:40:28.143: E/PowerManagerService(2116): WakeLock : binderDied()
如何修复此NullPointerException?
答案 0 :(得分:1)
实际上这是一个错误。
您可以通过创建单独的Application类来解决此问题。
package com.pusbots.androidpushbot;
import android.app.Application;
import com.pushbots.push.Pushbots;
public class SampleApplication extends Application {
private static final String SENDER_ID ="YOUR_SENDER_ID";
private static final String PUSHBOTS_APPLICATION_ID ="PUSHBOT_APP_ID";
@Override
public void onCreate(){
super.onCreate();
Pushbots.init(this, SENDER_ID, PUSHBOTS_APPLICATION_ID);
}
}
请参阅此视频教程:Send GCM Android Push Notifications