2014-07-04 19 views
3

我正在製作一個android應用程序,並且該應用程序的組件之一被設計爲在二進制文件中將數字轉換爲十進制數。每次運行它都會崩潰。我如何從二進制轉換爲Java中的十進制(使用android studio)

public void Button0Clicked(View v) 
{ 
     TextView myText = (TextView)findViewById(R.id.textView); 
     if (firstType == true) 
    { 
     myText.setText(""); 
     firstType = false; 
    } 
    myText.setText(myText.getText() + "0"); 

} 
public void Button1Clicked(View v) 
{ 
     TextView myText = (TextView)findViewById(R.id.textView); 
     if (firstType == true) 
     { 
      myText.setText(""); 
      firstType = false; 
     } 
     myText.setText(myText.getText() + "1"); 
} 
public void Conversion(View view) 
{ 
     TextView myText = (TextView)findViewById(R.id.textView); 
     int decimalValue = Integer.parseInt(myText.getText().toString(),2); 
     TextView result = (TextView)findViewById(R.id.textView2); 
     result.setText(decimalValue); 

} 

我已經縮小問題到一些問題與的Integer.parseInt()函數,是我錯了執行?

堆棧跟蹤

07-04 09:43:17.806 29832-29832/com.example.jay.myapplication2 E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    Process: com.example.jay.myapplication2, PID: 29832 
    java.lang.IllegalStateException: Could not execute method of the activity 
      at android.view.View$1.onClick(View.java:3823) 
      at android.view.View.performClick(View.java:4438) 
      at android.view.View$PerformClick.run(View.java:18422) 
      at android.os.Handler.handleCallback(Handler.java:733) 
      at android.os.Handler.dispatchMessage(Handler.java:95) 
      at android.os.Looper.loop(Looper.java:136) 
      at android.app.ActivityThread.main(ActivityThread.java:5001) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:515) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
      at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.reflect.InvocationTargetException 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:515) 
      at android.view.View$1.onClick(View.java:3818) 
            at android.view.View.performClick(View.java:4438) 
            at android.view.View$PerformClick.run(View.java:18422) 
            at android.os.Handler.handleCallback(Handler.java:733) 
            at android.os.Handler.dispatchMessage(Handler.java:95) 
            at android.os.Looper.loop(Looper.java:136) 
            at android.app.ActivityThread.main(ActivityThread.java:5001) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:515) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
            at dalvik.system.NativeStart.main(Native Method) 
    Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x271a 
      at android.content.res.Resources.getText(Resources.java:244) 
      at android.widget.TextView.setText(TextView.java:3888) 
      at com.example.jay.myapplication2.MyActivity.Conversion(MyActivity.java:66) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:515) 
            at android.view.View$1.onClick(View.java:3818) 
            at android.view.View.performClick(View.java:4438) 
            at android.view.View$PerformClick.run(View.java:18422) 
            at android.os.Handler.handleCallback(Handler.java:733) 
            at android.os.Handler.dispatchMessage(Handler.java:95) 
            at android.os.Looper.loop(Looper.java:136) 
            at android.app.ActivityThread.main(ActivityThread.java:5001) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:515) 
+2

它崩潰時的錯誤是什麼?它拋出異常嗎?它是什麼? –

+0

在Logcat中,它表示「java.lang.IllegalStateException:無法執行活動的方法」 – user3806093

+4

您可以使用logcat添加一個堆棧跟蹤到問題中。 –

回答

6

變化

result.setText(decimalValue); // int param 

要:

result.setText(Integer.toString(decimalValue)); // string param 

,它會工作得很好。


功能View.setText()是重載絃樂和參數的整數類型。

當編譯器檢測到一個整數參數,就像你的情況那樣,它試圖通過該ID找到一個 字符串資源。

記住 - 除非是資源ID,否則不要將整數值傳遞給setText()。

+2

我建議使用'Integer.toString(decimalValue)'而不是'「」+ decimalValue';一眼就能看清楚發生了什麼。 –

+0

@TedHopp同意! – matiash

+0

我怎麼能說不:) :) –

0

您無法將整數傳遞給TextView的setText方法。將其轉換爲字符串,然後就可以開始了。

+2

其實,你可以傳遞一個整數給'setText';不幸的是對於OP,當你這樣做時,它被解釋爲一個字符串資源ID。 –

+0

我明白了。感謝評論@TedHopp –

相關問題