How to convert binary to decimal in java (using Android Studio)… here is a solution to the problem.
How to convert binary to decimal in java (using Android Studio)
I’m making an Android app with a component designed to take binary numbers and convert them 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 issue down to some issue 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)
Solution
Change
result.setText(decimalValue); int param
To:
result.setText(Integer.toString(decimalValue)); string param
It will work just fine.
The function View.setText() is overloaded with parameters of type String and integer.
When the compiler detects an integer argument, as in your case, it tries to find one
The string resource for that ID.
Remember – never pass an integer value to setText() unless it is a resource ID.