我如何克服NoSuchElementFoundException?

时间:2015-09-06 16:42:31

标签: java android nosuchelementexception

我有NoSuchElementFoundException,但我无法找到pb所在的行。每当我的程序收到SMS时,都会抛出此异常。

Bundle bundle = intent.getExtras();
Object[] messages = (Object[]) bundle.get("pdus");
SmsMessage[] sms = new SmsMessage[messages.length];
for(int n = 0; n < messages.length; n++) {
    sms[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
}
for(SmsMessage msg : sms) {
    if(msg.getOriginatingAddress().equals("+923313552256") 
        && Data.login == true 
        && !(msg.getMessageBody().contains("OK"))) {
        MainActivity.startAlarm(msg.getMessageBody(),context);
    }
    if(msg.getOriginatingAddress().equals("+923313552256") 
        && msg.getMessageBody().contains("OK")) {   
        LoginPage.redirect();
    }
}

MainActivity中的StartAlarm

public static void startAlarm(String msg,Context context)
{
    Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
    mp = MediaPlayer.create(context, notification);
    mp.start();
    btstop.setVisibility(1);    
    btmap.setVisibility(1); 
    StringTokenizer tokens = new StringTokenizer(msg, " ");
    plat = tokens.nextToken();
    plong = tokens.nextToken();
}

logcat的:

09-06 20:04:21.619: E/AndroidRuntime(16819): FATAL EXCEPTION: main
09-06 20:04:21.619: E/AndroidRuntime(16819): java.lang.RuntimeException: Unable to start receiver com.example.hurryapppointalert_client.TextMessageReceiver: java.util.NoSuchElementException
09-06 20:04:21.619: E/AndroidRuntime(16819):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2140)
09-06 20:04:21.619: E/AndroidRuntime(16819):    at android.app.ActivityThread.access$1500(ActivityThread.java:128)
09-06 20:04:21.619: E/AndroidRuntime(16819):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1211)
09-06 20:04:21.619: E/AndroidRuntime(16819):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-06 20:04:21.619: E/AndroidRuntime(16819):    at android.os.Looper.loop(Looper.java:137)
09-06 20:04:21.619: E/AndroidRuntime(16819):    at android.app.ActivityThread.main(ActivityThread.java:4517)
09-06 20:04:21.619: E/AndroidRuntime(16819):    at java.lang.reflect.Method.invokeNative(Native Method)
09-06 20:04:21.619: E/AndroidRuntime(16819):    at java.lang.reflect.Method.invoke(Method.java:511)
09-06 20:04:21.619: E/AndroidRuntime(16819):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
09-06 20:04:21.619: E/AndroidRuntime(16819):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
09-06 20:04:21.619: E/AndroidRuntime(16819):    at dalvik.system.NativeStart.main(Native Method)
09-06 20:04:21.619: E/AndroidRuntime(16819): Caused by: java.util.NoSuchElementException
09-06 20:04:21.619: E/AndroidRuntime(16819):    at java.util.StringTokenizer.nextToken(StringTokenizer.java:208)
09-06 20:04:21.619: E/AndroidRuntime(16819):    at com.example.hurryapppointalert_client.MainActivity.startAlarm(MainActivity.java:156)
09-06 20:04:21.619: E/AndroidRuntime(16819):    at com.example.hurryapppointalert_client.TextMessageReceiver.onReceive(TextMessageReceiver.java:26)
09-06 20:04:21.619: E/AndroidRuntime(16819):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2133)
09-06 20:04:21.619: E/AndroidRuntime(16819):    ... 10 more

1 个答案:

答案 0 :(得分:0)

看起来您的错误发生了,因为msg的{​​{1}}参数中没有多个标记;你打电话给startAlarm两次,但是如果没有空间,它将在第二个上失败。

您应该检查nextToken()的值(可能是msg),并确保它符合您的想法。