Void无法转换为字符串

时间:2016-03-07 14:19:04

标签: android

我正在尝试为我的移动应用创建一个简单的登录信息,但我正在尝试使用错误信息:

Login.java
public void onButtonClick(View v) {
    if (v.getId() == R.id.BLogin) {
        EditText a = (EditText) findViewById(R.id.TFusername);
        String str = a.getText().toString();
        EditText b = (EditText) findViewById(R.id.TFpassword);
        String pass = b.getText().toString();

        String password = helper.searchPass(str);
        if (pass.equals(password)) {
            Intent i = new Intent(LogIn.this, Display.class);
            i.putExtra("Username", str);
            startActivity(i);
        } else {
            Toast temp = Toast.makeText(LogIn.this, "Username and password don't match!", Toast.LENGTH_SHORT);
            temp.show();
        }

    }
    if (v.getId() == R.id.BSignup) {
        Intent i = new Intent(LogIn.this, Signup.class);
        startActivity(i);
    }
}

和DatabaseHandler

  public void searchPass(String uname)
{
    db = this.getReadableDatabase();
    String query = " select  uname, pass from "+TABLE_NAME;
    Cursor cursor = db.rawQuery(query , null);
    String a, b;
    b = "not found";
    if(cursor.moveToFirst())
    {
        do {
            a = cursor.getString(0);


            if(a.equals(uname))
            {
                b = cursor.getString(1);
                break;
            }
        }
        while (cursor.moveToNext());
    }
    return b;
}

我遇到了错误: 错误:(32,48)错误:不兼容的类型:void无法转换为String String password = helper.searchPass(s​​tr); 有谁知道我失踪了什么?

1 个答案:

答案 0 :(得分:3)

在方法searchPass中将返回类型替换为String

public String searchPass(String uname)
{
db = this.getReadableDatabase();
String query = " select  uname, pass from "+TABLE_NAME;
Cursor cursor = db.rawQuery(query , null);
String a, b;
b = "not found";
if(cursor.moveToFirst())
{
    do {
        a = cursor.getString(0);


        if(a.equals(uname))
        {
            b = cursor.getString(1);
            break;
        }
    }
    while (cursor.moveToNext());
}
return b;
}
相关问题