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

时间:2012-02-27 00:20:37

标签: java android crash integer parseint

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

3 个答案:

答案 0 :(得分:2)

问题是您没有初始化result,当您致电Integer.parseInt(result)时,它无法解析null。从你获得的例外中可以看出这一点:

  

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

答案 1 :(得分:1)

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

答案 2 :(得分:0)

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

result = 0;

这也不会有效:result = ""