【发布时间】:2013-05-07 10:58:27
【问题描述】:
我在这段代码的末尾得到一个 NullPointerException :
@Override
public final void onHandleIntent(Intent intent) {
try {
Context context = getApplicationContext();
String action = intent.getAction();
if (action.equals(INTENT_FROM_GCM_REGISTRATION_CALLBACK)) {
handleRegistration(context, intent);
} else if (action.equals(INTENT_FROM_GCM_MESSAGE)) {
// checks for special messages
String messageType =
intent.getStringExtra(EXTRA_SPECIAL_MESSAGE);
if (messageType != null) {
if (messageType.equals(VALUE_DELETED_MESSAGES)) {
String sTotal =
intent.getStringExtra(EXTRA_TOTAL_DELETED);
if (sTotal != null) {
try {
int total = Integer.parseInt(sTotal);
Log.v(TAG, "Received deleted messages " +
"notification: " + total);
onDeletedMessages(context, total);
} catch (NumberFormatException e) {
Log.e(TAG, "GCM returned invalid number of " +
"deleted messages: " + sTotal);
}
}
} else {
// application is not using the latest GCM library
Log.e(TAG, "Received unknown special message: " +
messageType);
}
} else {
onMessage(context, intent);
}
} else if (action.equals(INTENT_FROM_GCM_LIBRARY_RETRY)) {
String token = intent.getStringExtra(EXTRA_TOKEN);
if (!TOKEN.equals(token)) {
// make sure intent was generated by this class, not by a
// malicious app.
Log.e(TAG, "Received invalid token: " + token);
return;
}
// retry last call
if (GCMRegistrar.isRegistered(context)) {
GCMRegistrar.internalUnregister(context);
} else {
GCMRegistrar.internalRegister(context, mSenderId);
}
}
} finally {
// Release the power lock, so phone can get back to sleep.
// The lock is reference-counted by default, so multiple
// messages are ok.
// If onMessage() needs to spawn a thread or do something else,
// it should use its own lock.
synchronized (LOCK) {
// sanity check for null as this is a public method
if (sWakeLock != null) {
Log.v(TAG, "Releasing wakelock");
if(sWakeLock.isHeld()){
sWakeLock.release();
}
} else {
// should never happen during normal workflow
Log.e(TAG, "Wakelock reference is null");
}
}
} // NullPointerException apparently thrown here
}
这是 LogCat:
05-07 12:55:06.775: V/GCMBroadcastReceiver(19555): onReceive: com.google.android.c2dm.intent.RECEIVE
05-07 12:55:06.775: V/GCMBroadcastReceiver(19555): GCM IntentService class: com.predictoo.whimbee.GCMIntentService
05-07 12:55:06.775: V/GCMBaseIntentService(19555): Acquiring wakelock
05-07 12:55:06.815: D/GCMIntentService(19555): onMessage - context: android.app.Application@41aecd40
05-07 12:55:06.815: V/GCMBaseIntentService(19555): Releasing wakelock
这是错误截图:
添加 catch 块后,我得到了异常的实际堆栈跟踪:
05-07 14:15:38.216: W/System.err(26532): java.lang.NullPointerException: println needs a message
05-07 14:15:38.226: W/System.err(26532): at android.util.Log.println_native(Native Method)
05-07 14:15:38.226: W/System.err(26532): at android.util.Log.v(Log.java:119)
05-07 14:15:38.226: W/System.err(26532): at com.predictoo.whimbee.GCMIntentService.onMessage(GCMIntentService.java:63)
05-07 14:15:38.226: W/System.err(26532): at com.google.android.gcm.GCMBaseIntentService.onHandleIntent(GCMBaseIntentService.java:179)
05-07 14:15:38.226: W/System.err(26532): at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
05-07 14:15:38.236: W/System.err(26532): at android.os.Handler.dispatchMessage(Handler.java:99)
05-07 14:15:38.236: W/System.err(26532): at android.os.Looper.loop(Looper.java:137)
05-07 14:15:38.236: W/System.err(26532): at android.os.HandlerThread.run(HandlerThread.java:60)
【问题讨论】:
-
在哪一行抛出异常?
-
@VKSingla 我在抛出异常的行上添加了引号。它是最后一行之一,在最后一个括号之前。
-
在 finally 块“之后”实际上并没有抛出异常。 finally 块将始终执行,即使在 try 块中抛出未声明的异常也是如此。所以实际的异常发生在try块内部,可以通过堆栈跟踪中的行号来识别。
-
@NilsH 说了什么,第 215 行是哪一行?
-
您引用的行不包含任何代码。只是一个括号。可能是您没有得到引发异常的正确行,或者您正在运行不同的文件并且此文件不同。
标签: java android nullpointerexception google-cloud-messaging try-catch-finally