我正在制作一个 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)
改变
result.setText(decimalValue); // int param
收件人:
result.setText(Integer.toString(decimalValue)); // string param
它会工作得很好。
函数 View.setText() 为 String 和 integer 类型的参数重载。
当编译器检测到一个整数参数时,就像你的情况一样,它会尝试找到一个该 ID 的字符串资源。
记住 - 永远不要将整数值传递给 setText(),除非它是资源 ID。
我是一名优秀的程序员,十分优秀!