如何在java中将二进制转换为十进制(使用android studio)

时间:2014-07-04 16:34:13

标签: java android android-studio

我正在创建一个Android应用程序,该应用程序的一个组件旨在获取二进制数字并将其转换为十进制。每次我运行它都会崩溃。

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 :(得分:6)

更改

 result.setText(decimalValue); // int param

要:

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

它会正常工作。


函数View.setText()为String和整数类型的参数重载。

当编译器检测到整数参数时,就像你的情况一样,它试图找到一个 该ID的字符串资源。

记住 - 永远不要将整数值传递给setText(),除非它是资源ID。

答案 1 :(得分:0)

您无法将整数传递给TextView setText方法。将它转换为String,然后你就去了。