-5

我一直在用我的手机调试我的应用程序,我得到的所有 logcat 错误都参考了我的活动中的第 54 行,在那里我将一个字符串解析为一个 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
4

3 回答 3

2

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

引起:java.lang.NumberFormatException:无法将“null”解析为整数

于 2012-02-27T00:25:33.383 回答
1

日志清楚地指出 Java 无法将“null”解析为整数。

于 2012-02-27T00:23:36.683 回答
0

就像上面的人说你正在解析null,尝试初始化你的变量,以防你的方法没有给你结果,可能这个结果不是你想要的,但你的代码不会崩溃。

result = 0;

这也行不通:result = ""

于 2013-02-20T02:22:18.610 回答