How do I convert from binary to decimal in java (using android studio)

android, android-studio, java

Solution

Change

 result.setText(decimalValue); // int param

To:

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

And it will work just fine.

Function View.setText() is overloaded for String and integer type of parameter.

When the compiler detected an integer param, as is your case, it attempts to find a String resource by that ID.

Remember - never pass an integer value to setText() unless it is a resource ID.

Problem

I am making an android app and one of the components of the app is designed to take a number in binary and convert it to decimal. Every time I run it, it crashes. ``` 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); } ``` I've narrowed the problem down to some problem with the Integer.parseint() function, is my implementation wrong? Stack Trace ``` 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) ```

Original source