Android 方法调用函数以返回值,然后跳过第一个函数的其余部分

Android 方法调用函数以返回值,然后跳过第一个函数的其余部分,android,Android,我有一个函数,它应该向SQLite数据库添加一条新记录。然后,该函数将调用一个函数以将int返回给变量,然后跳过其余代码并直接进入finally语句 下面是方法 SQLiteDatabase myDb = null; if (type.equals("Website")) { details = formatUrl(details); } try

我有一个函数,它应该向SQLite数据库添加一条新记录。然后,该函数将调用一个函数以将int返回给变量,然后跳过其余代码并直接进入finally语句

下面是方法

    SQLiteDatabase myDb = null;
            if (type.equals("Website"))
            {
                details = formatUrl(details);
            }
            try
            {
                myDb = context.openOrCreateDatabase("PasswordManager", Context.MODE_PRIVATE, null);

                int rowId = common.getNextID("password");

//ALL OF THIS CODE IS SKIPPED
                ContentValues cv = new ContentValues();
                cv.put("id", rowId);
                cv.put("category", category);
                cv.put("company", Encryption.encrypt(company));
                cv.put("loginAction", Encryption.encrypt(details));
                cv.put("username", Encryption.encrypt(username));
                cv.put("password", Encryption.encrypt(password));
                cv.put("type", type);
                cv.put("appName", "N/A");
                myDb.insert("password", null, cv);
            }
            catch (SQLiteException ex)
            {
                common.showBasicAlertDialog("Something has gone wrong.\n\nWe will fix this as soon as we can", false);
                Log.e("Database Errror", ex.toString());
                return false;
            }
            catch (SQLException ex)
            {
                common.showBasicAlertDialog("Something has gone wrong.\n\nWe will fix this as soon as we can", false);
                Log.e("SQL Error", ex.toString());
                return false;
            }
            finally
            {
//IT GOES STRAIGHT INTO THIS CODE AFTER THE GETNEXTID METHOD RETURNS
                if (myDb.isOpen())
                {
                    myDb.close();
                    return true;
                }
            }
            return false;
下面是getNextId()函数的代码


我不明白内容值被跳过了,它直接进入了finally

可能抛出了
SQLException
SQLiteException
以外的异常?如果你把catch(异常x){…}放进去,你可能会看到。

首先试着移动线

myDb = context.openOrCreateDatabase("PasswordManager", Context.MODE_PRIVATE, null);
线下

 int rowId = common.getNextID("password");
用你的方法。或者更好的做法是将myDb作为参数添加到getNextID函数中,这样您就可以使用相同的DB引用,而不用关闭该函数中的DB

捕获(异常x)
添加到异常中

然后最后在
getNextID
方法中放置一个断点,并找出它到底在哪一行上中断


干杯

您正在关闭
myDB
内部
getNextID()
然后尝试插入?我想这是不对的。为什么不放一个catch块来捕获所有其他异常(异常类),这样就可以看到异常的具体情况。没错,就是getNextId()函数抛出了一个不同的异常,在我添加catch(异常x)之前,该异常是隐藏的,这是因为我忘了检查游标是否至少有一行,所以它引发了cursorindexoutofbounds异常。
 int rowId = common.getNextID("password");