执行catch块时,Android应用程序意外崩溃

时间:2014-11-24 11:52:13

标签: android eclipse sqlite

我是Android的新手,我不知道为什么会这样。

在我的数据库中,我使用rollno作为主键。

此代码完美地工作并将数据插入数据库但是当我尝试插入数据库中已存在的卷号时,它应该给出“数据已存在”的消息,但它会意外崩溃。

if (v.getId()==R.id.b1) {
    int rollno,marks;
    String name,m1,rno;
    rno=t1.getText().toString();
    name=t2.getText().toString();
    m1=t3.getText().toString();
    rollno=Integer.parseInt(rno);
    marks=Integer.parseInt(m1);

    try {
        db.execSQL("insert into stud values('" + rollno + "','" + name + "','" + marks + "');");
        Toast.makeText(getApplicationContext(), "Data saved", Toast.LENGTH_LONG).show();
    } catch(Exception e) {
        Toast.makeText(getApplicationContext(), "Data already present", Toast.LENGTH_LONG).show();
    }
}

logcat的

D/SensorManager﹕ unregisterListener:: Trklfufi 9 budiwrd5mrfo5WirfulblrwuFmfulTrklfufi$KfukwiFmfulTrklfufiRvht@,)+*cf+(
D/Sensors﹕ Remain listener = Sending .. normal delay 200ms
I/Sensors﹕ sendDelay --- 200000000
D/SensorManager﹕ JNI - sendDelay
I/SensorManager﹕ Set normal delay = true
D/SensorManager﹕ registerListener :: handle = 4  name= BMA222 3-axis Accelerometer delay= 200000 Trklfufi 9 budiwrd5mrfo5WirfulblrwuFmfulTrklfufi$KfukwiFmfulTrklfufiRvht@,)+*fa88
W/IInputConnectionWrapper﹕ getSelectedText on inactive InputConnection
W/IInputConnectionWrapper﹕ setComposingText on inactive InputConnection
D/SensorManager﹕ unregisterListener:: Trklfufi 9 budiwrd5mrfo5WirfulblrwuFmfulTrklfufi$KfukwiFmfulTrklfufiRvht@,)+*fa88
D/Sensors﹕ Remain listener = Sending .. normal delay 200ms
I/Sensors﹕ sendDelay --- 200000000
D/SensorManager﹕ JNI - sendDelay
I/SensorManager﹕ Set normal delay = true
D/SensorManager﹕ registerListener :: handle = 4  name= BMA222 3-axis Accelerometer delay= 200000 Trklfufi 9 budiwrd5mrfo5WirfulblrwuFmfulTrklfufi$KfukwiFmfulTrklfufiRvht@,)+)(+/(
W/IInputConnectionWrapper﹕ getSelectedText on inactive InputConnection
W/IInputConnectionWrapper﹕ setComposingText on inactive InputConnection
D/SensorManager﹕ unregisterListener:: Trklfufi 9 budiwrd5mrfo5WirfulblrwuFmfulTrklfufi$KfukwiFmfulTrklfufiRvht@,)+)(+/(
D/Sensors﹕ Remain listener = Sending .. normal delay 200ms
I/Sensors﹕ sendDelay --- 200000000
D/SensorManager﹕ JNI - sendDelay
I/SensorManager﹕ Set normal delay = true
D/SensorManager﹕ registerListener :: handle = 4  name= BMA222 3-axis Accelerometer delay= 200000 Trklfufi 9 budiwrd5mrfo5WirfulblrwuFmfulTrklfufi$KfukwiFmfulTrklfufiRvht@,)+*cf+(
W/IInputConnectionWrapper﹕ getSelectedText on inactive InputConnection
W/IInputConnectionWrapper﹕ setComposingText on inactive InputConnection
D/dalvikvm﹕ GC_CONCURRENT freed 333K, 11% free 9617K/10695K, paused 14ms+4ms, total 72ms
D/SensorManager﹕ unregisterListener:: Trklfufi 9 budiwrd5mrfo5WirfulblrwuFmfulTrklfufi$KfukwiFmfulTrklfufiRvht@,)+*cf+(
D/Sensors﹕ Remain listener = Sending .. normal delay 200ms
I/Sensors﹕ sendDelay --- 200000000
D/SensorManager﹕ JNI - sendDelay
I/SensorManager﹕ Set normal delay = true
D/SensorManager﹕ registerListener :: handle = 4  name= BMA222 3-axis Accelerometer delay= 200000 Trklfufi 9 budiwrd5mrfo5WirfulblrwuFmfulTrklfufi$KfukwiFmfulTrklfufiRvht@,)+*fa88
W/IInputConnectionWrapper﹕ getSelectedText on inactive InputConnection
W/IInputConnectionWrapper﹕ setComposingText on inactive InputConnection
D/SensorManager﹕ unregisterListener:: Trklfufi 9 budiwrd5mrfo5WirfulblrwuFmfulTrklfufi$KfukwiFmfulTrklfufiRvht@,)+*fa88
D/Sensors﹕ Remain listener = Sending .. normal delay 200ms
I/Sensors﹕ sendDelay --- 200000000
D/SensorManager﹕ JNI - sendDelay
I/SensorManager﹕ Set normal delay = true
D/SensorManager﹕ registerListener :: handle = 4  name= BMA222 3-axis Accelerometer delay= 200000 Trklfufi 9 budiwrd5mrfo5WirfulblrwuFmfulTrklfufi$KfukwiFmfulTrklfufiRvht@,)+)(+/(
W/IInputConnectionWrapper﹕ getSelectedText on inactive InputConnection
W/IInputConnectionWrapper﹕ setComposingText on inactive InputConnection
D/SensorManager﹕ unregisterListener:: Trklfufi 9 budiwrd5mrfo5WirfulblrwuFmfulTrklfufi$KfukwiFmfulTrklfufiRvht@,)+)(+/(
D/Sensors﹕ Remain listener = Sending .. normal delay 200ms
I/Sensors﹕ sendDelay --- 200000000
D/SensorManager﹕ JNI - sendDelay
I/SensorManager﹕ Set normal delay = true
W/IInputConnectionWrapper﹕ showStatusIcon on inactive InputConnection
D/SensorManager﹕ registerListener :: handle = 4  name= BMA222 3-axis Accelerometer delay= 200000 Trklfufi 9 budiwrd5mrfo5WirfulblrwuFmfulTrklfufi$KfukwiFmfulTrklfufiRvht@,)+-+88(
D/AndroidRuntime﹕ Shutting down VM
W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x419222a0)
E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NumberFormatException: Invalid int: ""
            at java.lang.Integer.invalidInt(Integer.java:138)
            at java.lang.Integer.parseInt(Integer.java:359)
            at java.lang.Integer.parseInt(Integer.java:332)
            at com.example.raju.myapp1.MyActivity.onClick(MyActivity.java:111)
            at android.view.View.performClick(View.java:4162)
            at android.view.View$PerformClick.run(View.java:17082)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4856)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
            at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

makeText方法的第一个参数必须是ActivityApplication
使用对Activity对象的引用作为makeText方法的第一个参数。

答案 1 :(得分:0)

您希望此Exception会被catch阻止,但这是实际上没有发生的事情


我猜测数据库中的rollNo字段被指定为主键,当您尝试再次重新插入相同的卷号时,会出现此SQLITE异常。因此,您需要在数据库中插入任何记录之前检查是否存在特定的卷号。

我要给你一些伪代码

rollnumber=enteredRolNumber

if(enteredRollNumberisInDatabase)
{
   //No insertion and a message that this roll number already exists
}
else
{
   //Do your insertion here
}

在你的代码中,没有像这样的支票,你就是这样做的

 try {
            db.execSQL("insert into stud values('" + rollno + "','" + name + "','" + marks + "');");
            Toast.makeText(getApplicationContext(), "Data saved", Toast.LENGTH_LONG).show();
        }
        catch(Exception e)
        {
            Toast.makeText(getApplicationContext(), "Data already present", Toast.LENGTH_LONG).show();

        }