gpt4 book ai didi

java - Android 应用程序在第 54 行(Integer.parseInt)处崩溃,并且不完全确定原因

转载 作者:行者123 更新时间:2023-12-03 05:32:52 24 4
gpt4 key购买 nike

我一直在用手机调试我的应用程序,我得到的所有 logcat 错误都引用我的 Activity 中的第 54 行,其中我将 String 解析为 Int。该应用程序的基本思想是一个便士转换器,用户可以在其中输入他们想要转换的便士数量,然后将其从四分之一到剩余的便士进行划分。此时,我不确定我是否正确捕获了该事件,并反复使用匿名内部类并在类中实现。

这是 Java 应用程序的代码:

public class PennyConverterActivity extends Activity
{

EditText et;
TextView tv;

int cents;
int remaining;
int quarters;
int dimes;
int nickels;
int pennies;
String result;

@Override

public void onCreate(Bundle b)
{
super.onCreate(b);
setContentView(R.layout.main);

et = (EditText) findViewById(R.id.penny);

et.setText(result);

et.setOnKeyListener(new View.OnKeyListener()
{

@Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER))
{
result = et.getText().toString();
return (true);
}

return (true);
}
});


cents = Integer.parseInt(result); //LogCat error refers to this line

tv = (TextView) findViewById(R.id.quarters);

quarters = cents / 25;
tv.setText(R.string.quarters);
remaining = cents % 25;

tv = (TextView) findViewById(R.id.dimes);

dimes = remaining / 10;
tv.setText(R.string.dimes);
remaining = remaining % 10;

tv = (TextView) findViewById(R.id.nickels);

nickels = remaining / 5;
tv.setText(R.string.nickles);

tv = (TextView) findViewById(R.id.pennies);

pennies = remaining % 5;
tv.setText(R.string.pennies);

}

}

和我的 Logcat 错误

02-26 15:26:30.602: E/AndroidRuntime(25020): FATAL EXCEPTION: main
02-26 15:26:30.602: E/AndroidRuntime(25020): java.lang.RuntimeException: Unable to start activity ComponentInfo{CS211D.HW03.PennyConverter/CS211D.HW03.PennyConverter.PennyConverterActivity}: java.lang.NumberFormatException: unable to parse 'null' as integer
02-26 15:26:30.602: E/AndroidRuntime(25020): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-26 15:26:30.602: E/AndroidRuntime(25020): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-26 15:26:30.602: E/AndroidRuntime(25020): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-26 15:26:30.602: E/AndroidRuntime(25020): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-26 15:26:30.602: E/AndroidRuntime(25020): at android.os.Handler.dispatchMessage(Handler.java:99)
02-26 15:26:30.602: E/AndroidRuntime(25020): at android.os.Looper.loop(Looper.java:130)
02-26 15:26:30.602: E/AndroidRuntime(25020): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-26 15:26:30.602: E/AndroidRuntime(25020): at java.lang.reflect.Method.invokeNative(Native Method)
02-26 15:26:30.602: E/AndroidRuntime(25020): at java.lang.reflect.Method.invoke(Method.java:507)
02-26 15:26:30.602: E/AndroidRuntime(25020): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-26 15:26:30.602: E/AndroidRuntime(25020): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-26 15:26:30.602: E/AndroidRuntime(25020): at dalvik.system.NativeStart.main(Native Method)
02-26 15:26:30.602: E/AndroidRuntime(25020): Caused by: java.lang.NumberFormatException: unable to parse 'null' as integer
02-26 15:26:30.602: E/AndroidRuntime(25020): at java.lang.Integer.parseInt(Integer.java:356)
02-26 15:26:30.602: E/AndroidRuntime(25020): at java.lang.Integer.parseInt(Integer.java:332)
02-26 15:26:30.602: E/AndroidRuntime(25020): at CS211D.HW03.PennyConverter.PennyConverterActivity.onCreate(PennyConverterActivity.java:52)
02-26 15:26:30.602: E/AndroidRuntime(25020): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-26 15:26:30.602: E/AndroidRuntime(25020): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-26 15:26:30.602: E/AndroidRuntime(25020): ... 11 more

最佳答案

问题是您没有初始化结果并且当您调用 Integer.parseInt(result) 时,它无法解析 null。从您得到的异常中可以明显看出这一点:

Caused by: java.lang.NumberFormatException: unable to parse 'null' as integer

关于java - Android 应用程序在第 54 行(Integer.parseInt)处崩溃,并且不完全确定原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9458635/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com