我正在开发一个应用程序,因为我必须接收来自server.i的通知我正在成功地向我的服务器发送注册ID,但是我无法从gcm server.i获取通知。我是gettin nullpointerexception请告诉我我犯了错误< / p>
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
ComponentName comp = new ComponentName(context.getPackageName(),
GcmIntentService.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
public class GcmIntentService extends IntentService{
Context context;
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
public static final String TAG = "GCM Demo";
public GcmIntentService() {
super("GcmIntentService");
// TODO Auto-generated constructor stub
}
@Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
Bundle extras = intent.getExtras();
String msg = intent.getStringExtra("message");
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) {
if (GoogleCloudMessaging.
MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
sendNotification("Send error: " + extras.toString());
} else if (GoogleCloudMessaging.
MESSAGE_TYPE_DELETED.equals(messageType)) {
sendNotification("Deleted messages on server: " +
extras.toString());
// If it's a regular GCM message, do some work.
} else if (GoogleCloudMessaging.
MESSAGE_TYPE_MESSAGE.equals(messageType)) {
// This loop represents the service doing some work.
for (int i=0; i<5; i++) {
Log.i(TAG, "Working... " + (i+1)
+ "/5 @ " + SystemClock.elapsedRealtime());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
}
Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
// Post notification of received message.
//sendNotification("Received: " + extras.toString());
sendNotification(msg);
Log.i(TAG, "Received: " + extras.toString());
}
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
private void sendNotification(String msg) {
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent myintent = new Intent(context, ReceiveActivity.class);
myintent.putExtra("message", msg);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
myintent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_stat_gcm)
.setContentTitle("GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
}
public class ReceiveActivity extends Activity {
TextView name;
TextView deal;
TextView valid;
TextView address;
JSONObject json;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_receive);
Intent intent = getIntent();
name = (TextView) findViewById(R.id.name);
deal = (TextView) findViewById(R.id.deal);
valid = (TextView) findViewById(R.id.valid);
address = (TextView)findViewById(R.id.address);
String message = intent.getExtras().getString("message");
try {
json = new JSONObject(message);
String stime = json.getString("name");
name.setText(stime);
String slecturename = json.getString("deal");
deal.setText(slecturename);
String sroom = json.getString("valid");
valid.setText(sroom);
String sfaculty = json.getString("address");
address.setText(sfaculty);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
log cat
异常
02-21 12:10:10.679: E/AndroidRuntime(30200): FATAL EXCEPTION: IntentService[GcmIntentService]
02-21 12:10:10.679: E/AndroidRuntime(30200): java.lang.NullPointerException
02-21 12:10:10.679: E/AndroidRuntime(30200): at android.app.PendingIntent.getActivity(PendingIntent.java:191)
02-21 12:10:10.679: E/AndroidRuntime(30200): at com.technowellServices.locationfind.GcmIntentService.sendNotification(GcmIntentService.java:74)
02-21 12:10:10.679: E/AndroidRuntime(30200): at com.technowellServices.locationfind.GcmIntentService.onHandleIntent(GcmIntentService.java:62)
02-21 12:10:10.679: E/AndroidRuntime(30200): at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:59)
02-21 12:10:10.679: E/AndroidRuntime(30200): at android.os.Handler.dispatchMessage(Handler.java:99)