缺少返回声明公共长

时间:2016-03-30 18:06:52

标签: java android return return-type

我目前正在为我的最后一年项目编写代码,而且我遇到了一个我似乎无法解决的问题。

我在我的DatabaseHandler中将我的public void更改为public(返回为id然后在其他地方使用),并且因为更改代码它坚持我有一个丢失的return语句,但我无法弄清楚或为什么。原始代码工作正常但只要我将public void更改为public long就会出现缺少的return语句错误。

原始代码:

    public void UpdateProfileData(String email, String forename, String surname, String password, String DOB, int height, float weight, boolean gender) {
    String query = "SELECT * FROM Profile";
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.rawQuery(query, null);
    int count = cursor.getCount();

    if (count == 0)
    {
        //No data stored - we need to insert the row.
        db = this.getWritableDatabase();

        ContentValues values = new ContentValues();

        values.put("Email", email);
        values.put("Forename", forename);
        values.put("Surname", surname);
        values.put("Password", password);
        values.put("DOB", DOB);
        values.put("Height", height);
        values.put("Weight", weight);
        values.put("Gender", gender);

        db.insert("Profile", null, values);

    }
    else if (count == 1)
    {
        db = this.getWritableDatabase();

        ContentValues values = new ContentValues();

        values.put("Email", email);
        values.put("Forename", forename);
        values.put("Surname", surname);
        values.put("Password", password);
        values.put("DOB", DOB);
        values.put("Height", height);
        values.put("Weight", weight);
        values.put("Gender", gender);


        db.update("Profile", values, "Id=1", null);

    }

    else
    {
        //Something went wrong.
    }
}

新代码:

    public long UpdateProfileData(String email, String forename, String surname, String password, String DOB, int height, float weight, boolean gender) {
    String query = "SELECT * FROM Profile";
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.rawQuery(query, null);
    int count = cursor.getCount();

    if (count == 0)
    {
        //No data stored - we need to insert the row.
        db = this.getWritableDatabase();

        ContentValues values = new ContentValues();

        values.put("Email", email);
        values.put("Forename", forename);
        values.put("Surname", surname);
        values.put("Password", password);
        values.put("DOB", DOB);
        values.put("Height", height);
        values.put("Weight", weight);
        values.put("Gender", gender);

        long id = db.insert("Profile", null, values);
        return id;
    }
    else if (count == 1)
    {
        db = this.getWritableDatabase();

        ContentValues values = new ContentValues();

        values.put("Email", email);
        values.put("Forename", forename);
        values.put("Surname", surname);
        values.put("Password", password);
        values.put("DOB", DOB);
        values.put("Height", height);
        values.put("Weight", weight);
        values.put("Gender", gender);


        long id = db.update("Profile", values, "Id=1", null);
        return id;
    }

    else
    {
        //Something went wrong.
    }
}

我是java和编码的新手,所以如果这是一个愚蠢的错误,请记住善待并用简单的语言解释!提前感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

这里:

else
    {
        //Something went wrong.
    } 

是抱怨的原因......

你的方法应该返回很长时间,不管是什么......并且在每一个if / ifelse中你做得对,你在这里都没有返回......

修复:

根据您应用中的逻辑添加回报...

else
    {
        //Something went wrong.
     return -1;
} 

答案 1 :(得分:0)

所有"路径"非void方法需要return语句。最后的else案例是一条没有return的路径。

除了简单地将return置于其他内容之外的另一种解决方案是在方法结束时预先初始化idreturn

long id = -1;

if (count == 0) {
   id = db.insert...
} else if (count == 1) {
   id = db.update...
} else {
  // Something went wrong
}

return id;