2015-04-04 202 views
-3

我正在學習開發Android應用程序,並且遇到了與我的應用程序有關的問題。我創建了一個應用程序,允許用戶輸入一些文本,當按下按鈕時,會調用一個函數,將輸入字符串解析爲double,然後顯示它。我沒有收到任何錯誤,但是每當我在任何模擬器上運行應用程序時,它都會顯示「不幸的是,myapp已停止」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: "[email protected]" 
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); 
} 
+0

'由於:java.lang.NumberFormatException:無效的雙:「[email protected]」'無效的雙。發佈相關代碼 – Raghunandan 2015-04-04 09:24:06

+0

'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)'你需要顯示你的代碼。第25行是問題。 – Simon 2015-04-04 09:27:29

+0

那麼哪一條是第25行?我的猜測是'double amount = Double.parseDouble(amt.toString())* 100;'但是爲什麼不告訴我們而不是猜測呢? – Simon 2015-04-04 09:33:21

回答

1

你試圖將EditTexttoString()值轉換爲雙,這是不文本輸入EditText就像您期望的那樣,但名稱是與實例的哈希碼連接的類的名稱:

Object類的toString方法返回由 其中物體是一個實例,所述-SIGN 字符'@」的類的名稱的字符串,並且散列 的無符號十六進制表示法對象的代碼。換句話說,該方法返回一個字符串等於 ,它的值:

getClass().getName() + '@' + Integer.toHexString(hashCode()) 

你想要做什麼,是使用getText()檢索輸入的文本,並且將其轉換成一個String

double amount = Double.parseDouble(amt.getText().toString()) * 100; 

請注意,如果輸入不是適當的雙倍,這可能會導致NumberFormatException