使用libphonenumber时使用Iterator.next()获取java.util.NoSuchElementException

使用libphonenumber时使用Iterator.next()获取java.util.NoSuchElementException,java,android,Java,Android,代码: 在日志中,我可以得到期望的结果,但当我添加此行时出现异常gotPhone.setText(existsPhone.next().rawString()) 理想的结果是能够使用提取的数字。您在一次迭代中三次调用next(),强制迭代器移动到不存在的元素 而不是: 2019-06-11 16:23:43.059 11176-11176/com.example.cardscaning E/AndroidRuntime: FATAL EXCEPTION: main Process: co

代码:

在日志中,我可以得到期望的结果,但当我添加此行时出现异常
gotPhone.setText(existsPhone.next().rawString())

理想的结果是能够使用提取的数字。

您在一次迭代中三次调用
next()
,强制迭代器移动到不存在的元素

而不是:

2019-06-11 16:23:43.059 11176-11176/com.example.cardscaning E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.cardscaning, PID: 11176
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.cardscaning/com.example.cardscaning.Activity.ProcessImage}: java.util.NoSuchElementException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2678)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2743)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1490)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6165)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)
     Caused by: java.util.NoSuchElementException
        at com.google.i18n.phonenumbers.PhoneNumberMatcher.next(PhoneNumberMatcher.java:710)
        at com.google.i18n.phonenumbers.PhoneNumberMatcher.next(PhoneNumberMatcher.java:43)
        at com.example.cardscaning.Activity.ProcessImage.extractPhoneNumber(ProcessImage.java:291)
        at com.example.cardscaning.Activity.ProcessImage.onCreate(ProcessImage.java:97)
        at android.app.Activity.performCreate(Activity.java:6687)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2631)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2743) 
        at android.app.ActivityThread.-wrap12(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1490) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:154) 
        at android.app.ActivityThread.main(ActivityThread.java:6165) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778) 
使用类似于:

while (existsPhone.hasNext()){
     System.out.println("Phone == " + existsPhone.next().number());
     Log.d("existsPhone",":"+existsPhone.next().rawString());
     //...
}

从您呼叫的位置
提取电话号码
?它可能引用了视图项,但活动尚未初始化。
while (existsPhone.hasNext()){
     System.out.println("Phone == " + existsPhone.next().number());
     Log.d("existsPhone",":"+existsPhone.next().rawString());
     //...
}
while (existsPhone.hasNext()){
   PhoneNumberMatch phone = existsPhone.next();

   System.out.println("Phone == " + phone.number());
   Log.d("existsPhone",":"+phone.rawString());
   //....
}