문제

I am trying to develop an app which crashes when I include any sort of math parser. App works fine otherwise when I comment out the math parser. I tried other parsers, still the same problem. Any ideas why this is happening? Below are some screen shots.

  1. Manifest enter image description here

  2. MainActivity File enter image description here

  3. Project Explorer

enter image description here

LogCat

04-24 01:13:15.440: D/AndroidRuntime(3481): Shutting down VM
04-24 01:13:15.440: W/dalvikvm(3481): threadid=1: thread exiting with uncaught exception (group=0xb1a74ba8)
04-24 01:13:15.460: E/AndroidRuntime(3481): FATAL EXCEPTION: main
04-24 01:13:15.460: E/AndroidRuntime(3481): Process: com.example.calculator, PID: 3481
04-24 01:13:15.460: E/AndroidRuntime(3481): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.calculator/com.example.calculator.MainActivity}: java.util.regex.PatternSyntaxException: Syntax error in regexp pattern near index 1:
04-24 01:13:15.460: E/AndroidRuntime(3481): }
04-24 01:13:15.460: E/AndroidRuntime(3481):  ^
04-24 01:13:15.460: E/AndroidRuntime(3481):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-24 01:13:15.460: E/AndroidRuntime(3481):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-24 01:13:15.460: E/AndroidRuntime(3481):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-24 01:13:15.460: E/AndroidRuntime(3481):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-24 01:13:15.460: E/AndroidRuntime(3481):     at android.os.Handler.dispatchMessage(Handler.java:102)
04-24 01:13:15.460: E/AndroidRuntime(3481):     at android.os.Looper.loop(Looper.java:136)
04-24 01:13:15.460: E/AndroidRuntime(3481):     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-24 01:13:15.460: E/AndroidRuntime(3481):     at java.lang.reflect.Method.invokeNative(Native Method)
04-24 01:13:15.460: E/AndroidRuntime(3481):     at java.lang.reflect.Method.invoke(Method.java:515)
04-24 01:13:15.460: E/AndroidRuntime(3481):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-24 01:13:15.460: E/AndroidRuntime(3481):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-24 01:13:15.460: E/AndroidRuntime(3481):     at dalvik.system.NativeStart.main(Native Method)
04-24 01:13:15.460: E/AndroidRuntime(3481): Caused by: java.util.regex.PatternSyntaxException: Syntax error in regexp pattern near index 1:
04-24 01:13:15.460: E/AndroidRuntime(3481): }
04-24 01:13:15.460: E/AndroidRuntime(3481):  ^
04-24 01:13:15.460: E/AndroidRuntime(3481):     at java.util.regex.Pattern.compileImpl(Native Method)
04-24 01:13:15.460: E/AndroidRuntime(3481):     at java.util.regex.Pattern.compile(Pattern.java:411)
04-24 01:13:15.460: E/AndroidRuntime(3481):     at java.util.regex.Pattern.<init>(Pattern.java:394)
04-24 01:13:15.460: E/AndroidRuntime(3481):     at java.util.regex.Pattern.compile(Pattern.java:381)
04-24 01:13:15.460: E/AndroidRuntime(3481):     at java.lang.String.replaceAll(String.java:1785)
04-24 01:13:15.460: E/AndroidRuntime(3481):     at com.google.code.mathparser.constants.utils.MathParserUtils.transformGropCharsIntoParentheses(MathParserUtils.java:41)
04-24 01:13:15.460: E/AndroidRuntime(3481):     at com.google.code.mathparser.validators.ExpressionValidator.evalIfExpressionIsWellFormed(ExpressionValidator.java:44)
04-24 01:13:15.460: E/AndroidRuntime(3481):     at com.google.code.mathparser.validators.ExpressionValidator.validate(ExpressionValidator.java:38)
04-24 01:13:15.460: E/AndroidRuntime(3481):     at com.google.code.mathparser.parser.LexicalTokenizer.validateExpression(LexicalTokenizer.java:116)
04-24 01:13:15.460: E/AndroidRuntime(3481):     at com.google.code.mathparser.parser.LexicalTokenizer.assignNewExpression(LexicalTokenizer.java:59)
04-24 01:13:15.460: E/AndroidRuntime(3481):     at com.google.code.mathparser.parser.LexicalTokenizer.setExpression(LexicalTokenizer.java:47)
04-24 01:13:15.460: E/AndroidRuntime(3481):     at com.google.code.mathparser.parser.Parser.splitExpressionInTokens(Parser.java:206)
04-24 01:13:15.460: E/AndroidRuntime(3481):     at com.google.code.mathparser.parser.Parser.shuntingYardAlgorithm(Parser.java:69)
04-24 01:13:15.460: E/AndroidRuntime(3481):     at com.google.code.mathparser.parser.Parser.setExpression(Parser.java:50)
04-24 01:13:15.460: E/AndroidRuntime(3481):     at com.google.code.mathparser.impl.MathParserImpl.calculate(MathParserImpl.java:33)
04-24 01:13:15.460: E/AndroidRuntime(3481):     at com.example.calculator.MainActivity.onCreate(MainActivity.java:42)
04-24 01:13:15.460: E/AndroidRuntime(3481):     at android.app.Activity.performCreate(Activity.java:5231)
04-24 01:13:15.460: E/AndroidRuntime(3481):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-24 01:13:15.460: E/AndroidRuntime(3481):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-24 01:13:15.460: E/AndroidRuntime(3481):     ... 11 more
도움이 되었습니까?

해결책 2

After trying tonnes of parser, Expr seems to work for me. No crashes. Also refer to this.

다른 팁

It might help you to post the Activity.java instead of the screenshot.

Your app is crashing because Result and MathParser are being declared outside the scope of where it can be initialized. (''where it can live''). Right now you'd be getting a Null Pointer Exception and so the resultant crash.

Please check More Info on the same.

For now, you can just shift your lines of MathParser and Result inside onCreate() and that should work.

Use mxparser (http://mathparser.org/)

It generally works well for Java apps.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top