0

在我的应用程序中,我使用 match.group(int group) 方法返回与给定正则表达式组匹配的文本,其中“match”是 Matcher 对象。但是在运行应用程序后,我得到了 NumberFormatException。在使用 match.group() 方法之前,我尝试使用 match.find() 和 match.matches() 方法,但后来我收到 IllegalStateException 并且应用程序再次崩溃。

这是我的代码:

public int getProgramNumberInt () {
    Matcher m = PROGRAM_NUMBER_INT_PATTERN.matcher(programNumber);
    return m.matches() ? Integer.parseInt(m.group(1)) : 0;
}

崩溃发生在这一行:

return m.matches() ? Integer.parseInt(m.group(1)) : 0;

日志猫:

04-23 15:45:25.096: E/AndroidRuntime(16260): FATAL EXCEPTION: main
04-23 15:45:25.096: E/AndroidRuntime(16260): java.lang.NumberFormatException: unable to parse '' as integer
04-23 15:45:25.096: E/AndroidRuntime(16260):    at java.lang.Integer.parseInt(Integer.java:412)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at java.lang.Integer.parseInt(Integer.java:382)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at com.example.Matcher.getProgramNumberInt(Starter.java:101)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at com.example.Matcher.results.ResultsActivity$Adapter.getPayoffView(ResultsActivity.java:127)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at com.example.Matcher.results.ResultsActivity$Adapter.getView(ResultsActivity.java:62)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at android.widget.AbsListView.obtainView(AbsListView.java:1294)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1198)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at android.widget.ListView.onMeasure(ListView.java:1109)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at android.view.View.measure(View.java:8171)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at android.view.View.measure(View.java:8171)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at android.view.View.measure(View.java:8171)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at android.view.View.measure(View.java:8171)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at android.view.View.measure(View.java:8171)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at android.view.View.measure(View.java:8171)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at android.view.View.measure(View.java:8171)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at android.view.View.measure(View.java:8171)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at android.view.View.measure(View.java:8171)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at android.view.View.measure(View.java:8171)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at android.view.View.measure(View.java:8171)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at android.view.View.measure(View.java:8171)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at android.os.Looper.loop(Looper.java:123)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at android.app.ActivityThread.main(ActivityThread.java:4627)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at java.lang.reflect.Method.invokeNative(Native Method)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at java.lang.reflect.Method.invoke(Method.java:521)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-23 15:45:25.096: E/AndroidRuntime(16260):    at dalvik.system.NativeStart.main(Native Method)

您能否让我知道是否有任何方法可以防止应用程序崩溃并解决此问题?谢谢你。

4

1 回答 1

0

该方法m.group(1)返回 '' 空字符串。所以parseInt()方法会抛出NumberFormatException

于 2013-04-23T10:33:54.237 回答