我的android应用程序抛出错误,在线搜索仍然不明白错误是什么!,任何帮助表示赞赏..
03-14 15:46:02.760: E/AndroidRuntime(5163): FATAL EXCEPTION: IntentService[GCMIntentService-DynamicSenderIds-3]
03-14 15:46:02.760: E/AndroidRuntime(5163): java.lang.ArrayIndexOutOfBoundsException: length=2; index=3
03-14 15:46:02.760: E/AndroidRuntime(5163): at com.Test.Messaging.GCMIntentService.onMessage(GCMIntentService.java:43)
03-14 15:46:02.760: E/AndroidRuntime(5163): at com.google.android.gcm.GCMBaseIntentService.onHandleIntent(GCMBaseIntentService.java:223)
03-14 15:46:02.760: E/AndroidRuntime(5163): at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
03-14 15:46:02.760: E/AndroidRuntime(5163): at android.os.Handler.dispatchMessage(Handler.java:99)
03-14 15:46:02.760: E/AndroidRuntime(5163): at android.os.Looper.loop(Looper.java:176)
03-14 15:46:02.760: E/AndroidRuntime(5163): at android.os.HandlerThread.run(HandlerThread.java:61)
我的GCMIntentService类,它抛出异常:
protected void onMessage(Context context, Intent intent) {
Log.i(TAG, "Recieved");
String message = intent.getExtras().getString("price");
String oo[] = message.split("-");
String kk = "R";
String msg = oo[3];
String che = oo[2];
see = oo[1];
if (kk.equals(oo[0])) {
RandomMsg.test = msg;
RandomMsg.pool= see;
} else {
display(context, message);
}
}
答案 0 :(得分:0)
生成该问题是因为oo[]
数组仅包含2个值。在调用oo [index]之前,你必须检查数组长度。您可以更新您的方法,如下所示:
protected void onMessage(Context context, Intent intent) {
Log.i(TAG, "Recieved");
String message = intent.getExtras().getString("price");
String oo[] = message.split("-");
int length = oo.length;
String kk = "R";
String msg = length > 3 ? oo[3] : "";
String che = length > 2 ? oo[2] : "";
see = length >1 oo[1] : "";
if (kk.equals(oo[0])) {
RandomMsg.test = msg;
RandomMsg.pool= see;
} else {
display(context, message);
}
}
问题将会消失,但问题可能是最大的问题。我想您应该检查调试的message
值,并确保它是您想要获得的值。