Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在匹配用户ID和密码时获取null异常_Android_Sqlite - Fatal编程技术网

Android 在匹配用户ID和密码时获取null异常

Android 在匹配用户ID和密码时获取null异常,android,sqlite,Android,Sqlite,我正在创建一个基于登录活动的应用程序。当我进行注册(注册)时,所有数据都会成功地进入数据库,但当用户进行登录活动并尝试登录时,异常情况下会出现null 这是我的密码 loginuserid=etloginuserid.getText().toString(); loginpassword=etloginpassword.getText().toString(); try { String storedPass

我正在创建一个基于登录活动的应用程序。当我进行注册(注册)时,所有数据都会成功地进入数据库,但当用户进行登录活动并尝试登录时,异常情况下会出现null

这是我的密码

        loginuserid=etloginuserid.getText().toString();
        loginpassword=etloginpassword.getText().toString();
        try
        {
            String storedPassword=db.getPasswordOfThisUser(loginuserid);

            if(loginpassword.equals(storedPassword))
            {
                Toast.makeText(getApplicationContext(), "Login Successfull", Toast.LENGTH_SHORT).show();
                Intent iwelpage=new Intent(this,WelcomeActivity.class);
                startActivity(iwelpage);
                finish();
                break;
            }
            else
            {
                Toast.makeText(getApplicationContext(), "Username or Password does not match!", Toast.LENGTH_SHORT).show();
            }
        }
        catch(Exception e)
        {
            Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
        }
这是我的数据库类活动

创建表时

public void onCreate(SQLiteDatabase db) 
    {
        try
        {
            String CREATE_POLLINGDATA_TABLE="CREATE TABLE pollingdata(username TEXT, userid PRIMARY KEY, password TEXT)";
            db.execSQL(CREATE_POLLINGDATA_TABLE);
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
    }
检索输入的用户ID的密码时

public String getPasswordOfThisUser(String loginuserid)
{
    Cursor cursor=db.query("pollingdata", null, " userid=?" , new String[]{loginuserid}, null, null, null);
    if(cursor.getCount()<1)
    {
        cursor.close();
        return "Not Exist";
    }
    cursor.moveToFirst();
    String password= cursor.getString(cursor.getColumnIndex(KEY_PASSWORD));
    return password;
}
我哪里做错了

请帮帮我

谢谢。

我想

"CREATE TABLE pollingdata(username TEXT, userid PRIMARY KEY, password TEXT)" 
这是不正确的。 也许你应该换成

"CREATE TABLE pollingdata(username TEXT, userid INTEGER PRIMARY KEY, password TEXT)"
我想

"CREATE TABLE pollingdata(username TEXT, userid PRIMARY KEY, password TEXT)" 
这是不正确的。 也许你应该换成

"CREATE TABLE pollingdata(username TEXT, userid INTEGER PRIMARY KEY, password TEXT)"
试试这个

public String getPasswordOfThisUser(String loginuserid) {
    Cursor cursor = db.query("pollingdata", new String[] { KEY_PASSWORD },
            " userid=?", new String[] { username }, null, null, null);
    if (cursor.getCount() < 1) {
        cursor.close();
        return "Not Exist";
    }
    cursor.moveToFirst();
    String password = cursor.getString(cursor.getColumnIndex(KEY_PASSWORD));
    return password;
}
此用户的公共字符串getPasswordof(字符串loginuserid){
Cursor Cursor=db.query(“pollingdata”,新字符串[]{KEY\u PASSWORD},
“userid=?”,新字符串[]{username},null,null,null);
if(cursor.getCount()<1){
cursor.close();
返回“不存在”;
}
cursor.moveToFirst();
字符串密码=cursor.getString(cursor.getColumnIndex(KEY_password));
返回密码;
}
在返回密码之前必须关闭光标

public String getPasswordOfThisUser(String loginuserid) {
    Cursor cursor = db.query("pollingdata", new String[] { KEY_PASSWORD },
            " userid=?", new String[] { username }, null, null, null);
    if (cursor.getCount() < 1) {
        cursor.close();
        return "Not Exist";
    }
    cursor.moveToFirst();
    String password = cursor.getString(cursor.getColumnIndex(KEY_PASSWORD));
    return password;
}
此用户的公共字符串getPasswordof(字符串loginuserid){
Cursor Cursor=db.query(“pollingdata”,新字符串[]{KEY\u PASSWORD},
“userid=?”,新字符串[]{username},null,null,null);
if(cursor.getCount()<1){
cursor.close();
返回“不存在”;
}
cursor.moveToFirst();
字符串密码=cursor.getString(cursor.getColumnIndex(KEY_password));
返回密码;
}

在create table语句中返回密码之前必须关闭光标。您没有指定用户id的类型。

在create table语句中,您没有指定用户id的类型。

更改

if(cursor.getCount()<1)
{
    cursor.close();
    return "Not Exist";
}
cursor.moveToFirst();
改变

if(cursor.getCount()<1)
{
    cursor.close();
    return "Not Exist";
}
cursor.moveToFirst();
问题已解决:)

我得到了答案

我没有写过这个

db=new DBAdapter(LoginActivity.this);
db.open();
String storedPassword=db.getPasswordOfThisUser(loginuserid);
db.close();
谢谢大家的帮助。

问题已解决:)

我得到了答案

我没有写过这个

db=new DBAdapter(LoginActivity.this);
db.open();
String storedPassword=db.getPasswordOfThisUser(loginuserid);
db.close();

谢谢大家的帮助。

您可以发布异常和堆栈跟踪以获取更多信息。您能否清楚地说明从何处获取空指针异常..即活动或数据库
String storedPassword=db.getpasswordof此用户(loginuserid)此处请提供错误日志。@Shiv第47行是什么
LoginActivity
?您可以发布异常和堆栈跟踪以了解更多信息。您能否清楚地说明从何处获取空指针异常..即,活动或数据库
字符串storedPassword=db.GetPasswordOfThis用户(loginuserid)此处请提供错误日志。@Shiv第47行是什么
LoginActivity
?您的数据库未初始化,因此您得到NPE@Raghunandan:嗯..是的:)你的数据库没有初始化,所以你得到了NPE@Raghunandan:嗯..是的:)如果
主键
文本
?如果
主键
文本