App崩溃显示NumberFormatException:无法解析Android中的整数?

时间:2013-04-23 10:31:29

标签: android matcher numberformatexception

在我的应用程序中,我使用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;

logcat的:

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)

如果有任何方法可以防止该应用崩溃并解决此问题,请告诉我们吗? 谢谢。

1 个答案:

答案 0 :(得分:0)

方法m.group(1)返回''空字符串。因此parseInt()方法显示NumberFormatException