Android - 不幸的是,myapp已经停止了

时间:2015-04-04 09:23:28

标签: android

我正在学习开发Android应用程序,并且遇到了我的应用程序的问题。我创建了一个允许用户键入一些文本的应用程序,当按下按钮时,会调用一个函数来执行解析输入字符串以使其翻倍然后显示它。我没有收到任何错误,但每当我在任何模拟器上运行应用程序时,它都会显示"不幸的是,myapp已停止"

这是LogCat:

04-04 14:34:11.579: I/dalvikvm(657): Wrote stack traces to '/data/anr/traces.txt'
04-04 14:34:11.969: I/dalvikvm(657): threadid=3: reacting to signal 3
04-04 14:34:12.089: I/dalvikvm(657): Wrote stack traces to '/data/anr/traces.txt'
04-04 14:34:12.720: I/dalvikvm(657): threadid=3: reacting to signal 3
04-04 14:34:12.860: I/dalvikvm(657): Wrote stack traces to '/data/anr/traces.txt'
04-04 14:34:12.979: I/dalvikvm(657): threadid=3: reacting to signal 3
04-04 14:34:13.069: I/dalvikvm(657): Wrote stack traces to '/data/anr/traces.txt'
04-04 14:34:13.479: I/dalvikvm(657): threadid=3: reacting to signal 3
04-04 14:34:13.599: I/dalvikvm(657): Wrote stack traces to '/data/anr/traces.txt'
04-04 14:34:14.009: I/dalvikvm(657): threadid=3: reacting to signal 3
04-04 14:34:14.129: I/dalvikvm(657): Wrote stack traces to '/data/anr/traces.txt'
04-04 14:34:14.320: D/libEGL(657): loaded /system/lib/egl/libGLES_android.so
04-04 14:34:14.390: D/libEGL(657): loaded /system/lib/egl/libEGL_emulation.so
04-04 14:34:14.409: D/(657): HostConnection::get() New Host Connection established 0xd7820, tid 657
04-04 14:34:14.429: D/libEGL(657): loaded /system/lib/egl/libGLESv1_CM_emulation.so
04-04 14:34:14.449: D/libEGL(657): loaded /system/lib/egl/libGLESv2_emulation.so
04-04 14:34:14.519: I/dalvikvm(657): threadid=3: reacting to signal 3
04-04 14:34:14.579: I/dalvikvm(657): Wrote stack traces to '/data/anr/traces.txt'
04-04 14:34:14.719: W/EGL_emulation(657): eglSurfaceAttrib not implemented
04-04 14:34:14.769: D/OpenGLRenderer(657): Enabling debug mode 0
04-04 14:34:15.049: I/dalvikvm(657): threadid=3: reacting to signal 3
04-04 14:34:15.173: I/dalvikvm(657): Wrote stack traces to '/data/anr/traces.txt'
04-04 14:34:15.579: I/dalvikvm(657): threadid=3: reacting to signal 3
04-04 14:34:15.790: I/dalvikvm(657): Wrote stack traces to '/data/anr/traces.txt'
04-04 14:34:16.030: I/dalvikvm(657): threadid=3: reacting to signal 3
04-04 14:34:16.109: I/dalvikvm(657): Wrote stack traces to '/data/anr/traces.txt'
04-04 14:34:16.549: I/dalvikvm(657): threadid=3: reacting to signal 3
04-04 14:34:16.739: I/dalvikvm(657): Wrote stack traces to '/data/anr/traces.txt'
04-04 14:34:17.029: I/dalvikvm(657): threadid=3: reacting to signal 3
04-04 14:34:17.220: I/dalvikvm(657): Wrote stack traces to '/data/anr/traces.txt'
04-04 14:34:27.099: D/AndroidRuntime(657): Shutting down VM
04-04 14:34:27.099: W/dalvikvm(657): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
04-04 14:34:27.219: E/AndroidRuntime(657): FATAL EXCEPTION: main
04-04 14:34:27.219: E/AndroidRuntime(657): java.lang.IllegalStateException: Could not execute method of the activity
04-04 14:34:27.219: E/AndroidRuntime(657):  at android.view.View$1.onClick(View.java:3044)
04-04 14:34:27.219: E/AndroidRuntime(657):  at android.view.View.performClick(View.java:3511)
04-04 14:34:27.219: E/AndroidRuntime(657):  at android.view.View$PerformClick.run(View.java:14105)
04-04 14:34:27.219: E/AndroidRuntime(657):  at android.os.Handler.handleCallback(Handler.java:605)
04-04 14:34:27.219: E/AndroidRuntime(657):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-04 14:34:27.219: E/AndroidRuntime(657):  at android.os.Looper.loop(Looper.java:137)
04-04 14:34:27.219: E/AndroidRuntime(657):  at android.app.ActivityThread.main(ActivityThread.java:4424)
04-04 14:34:27.219: E/AndroidRuntime(657):  at java.lang.reflect.Method.invokeNative(Native Method)
04-04 14:34:27.219: E/AndroidRuntime(657):  at java.lang.reflect.Method.invoke(Method.java:511)
04-04 14:34:27.219: E/AndroidRuntime(657):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-04 14:34:27.219: E/AndroidRuntime(657):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-04 14:34:27.219: E/AndroidRuntime(657):  at dalvik.system.NativeStart.main(Native Method)
04-04 14:34:27.219: E/AndroidRuntime(657): Caused by: java.lang.reflect.InvocationTargetException
04-04 14:34:27.219: E/AndroidRuntime(657):  at java.lang.reflect.Method.invokeNative(Native Method)
04-04 14:34:27.219: E/AndroidRuntime(657):  at java.lang.reflect.Method.invoke(Method.java:511)
04-04 14:34:27.219: E/AndroidRuntime(657):  at android.view.View$1.onClick(View.java:3039)
04-04 14:34:27.219: E/AndroidRuntime(657):  ... 11 more
04-04 14:34:27.219: E/AndroidRuntime(657): Caused by: java.lang.NumberFormatException: Invalid double: "android.widget.EditText@40f7be40"
04-04 14:34:27.219: E/AndroidRuntime(657):  at java.lang.StringToReal.invalidReal(StringToReal.java:63)
04-04 14:34:27.219: E/AndroidRuntime(657):  at java.lang.StringToReal.initialParse(StringToReal.java:114)
04-04 14:34:27.219: E/AndroidRuntime(657):  at java.lang.StringToReal.parseDouble(StringToReal.java:263)
04-04 14:34:27.219: E/AndroidRuntime(657):  at java.lang.Double.parseDouble(Double.java:295)
04-04 14:34:27.219: E/AndroidRuntime(657):  at com.example.coinage.MainActivity.convert(MainActivity.java:25)
04-04 14:34:27.219: E/AndroidRuntime(657):  ... 14 more
04-04 14:34:28.069: I/dalvikvm(657): threadid=3: reacting to signal 3
04-04 14:34:28.179: I/dalvikvm(657): Wrote stack traces to '/data/anr/traces.txt'
04-04 14:34:29.729: I/Process(657): Sending signal. PID: 657 SIG: 9

执行计算的方法

public void convert(View view){
    int countOf2Po = 0, countOf1Po = 0, countOf50p = 0, countOf20p = 0, countOf2p = 0, countOf1p = 0;
    EditText amt = (EditText) findViewById (R.id.amount);
    TextView penny = (TextView) findViewById (R.id.penny);
    double amount = Double.parseDouble(amt.toString()) * 100;
    while(amount >= 200){
        amount = amount - 200;
        countOf2Po++;
    }
    while(amount >= 100){
        amount = amount - 100;
        countOf1Po++;
    }
    while(amount >= 50){
        amount = amount - 50;
        countOf50p++;
    }
    while(amount >= 20){
            amount = amount - 20;
            countOf20p++;
    }
    while(amount >= 2){
        amount = amount - 2;
        countOf2p++;
    }
    if(amount % 2 != 0){
        countOf1p++;
    }
    penny.setText("Converting " + penny + " into coins.");
    penny.setVisibility(View.VISIBLE);
}

1 个答案:

答案 0 :(得分:1)

您正在尝试将toString()的{​​{1}}值转换为double,这不是像您预期的那样输入EditText的文本,而是名称的名称该类与实例的哈希码连接:

  

Object类的toString方法返回一个由。组成的字符串   对象是实例的类的名称,at-sign   字符“@”,以及散列的无符号十六进制表示   对象的代码。换句话说,此方法返回一个相等的字符串   价值:

EditText

您要做的是使用getClass().getName() + '@' + Integer.toHexString(hashCode()) 检索输入的文字,并将其转换为getText()

String

请注意,如果输入不是正确的双倍,这可能会导致double amount = Double.parseDouble(amt.getText().toString()) * 100;