java - 使用 libphonenumber 时使用 Iterator.next() 获取 java.util.NoSuchElementException
问题描述
代码 :
public void extractPhoneNumber(String input){
Iterator<PhoneNumberMatch> existsPhone= PhoneNumberUtil.getInstance().findNumbers(input, "IN").iterator();
while (existsPhone.hasNext()){
System.out.println("Phone == " + existsPhone.next().number());
Log.d("existsPhone",":"+existsPhone.next().rawString());
gotPhone.setText(existsPhone.next().rawString());
}
}
日志 :
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)
在日志里面我可以得到期望的结果,但是当我添加这一行时会发生异常gotPhone.setText(existsPhone.next().rawString())
理想的结果是能够使用提取的数字。
解决方案
您在一次迭代中调用next()
三次,迫使 Iterator 移动到一个不存在的元素。
代替:
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());
//....
}
推荐阅读
- r - R中的多参数梯度下降
- python - 使用正则表达式模式解析 URL 列表
- jenkins - 使用基于角色的策略时如何限制特定的詹金斯工作
- android - 下载管理器更新应用程序,但失败
- c# - 如何保存对象变量供 ASP.NET 应用程序的所有用户使用,该变量被触发一次
- svg - SVG 使用 - 旋转后无法重新定位多边形
- amazon-web-services - Amazon SageMaker 分解机器评级矩阵和终端节点
- sql - 如何附加列,分组且不重复?
- r - 如何在 R 中引导混合效应模型
- google-analytics - 如何使用 Tealium/GA 衡量使用 URL 片段的单页网站?