1

我正在创建一个基于登录活动的应用程序。当我进行注册(注册)时,所有数据都成功进入数据库,但是当用户处于登录活动并尝试登录时,异常中为空。

这是我的代码,

        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;
}

日志猫,

10-30 06:03:13.066: E/AndroidRuntime(13692): FATAL EXCEPTION: main
10-30 06:03:13.066: E/AndroidRuntime(13692): java.lang.NullPointerException
10-30 06:03:13.066: E/AndroidRuntime(13692):    at com.example.polling.LoginActivity.onClick(LoginActivity.java:47)
10-30 06:03:13.066: E/AndroidRuntime(13692):    at android.view.View.performClick(View.java:4204)
10-30 06:03:13.066: E/AndroidRuntime(13692):    at android.view.View$PerformClick.run(View.java:17355)
10-30 06:03:13.066: E/AndroidRuntime(13692):    at android.os.Handler.handleCallback(Handler.java:725)
10-30 06:03:13.066: E/AndroidRuntime(13692):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-30 06:03:13.066: E/AndroidRuntime(13692):    at android.os.Looper.loop(Looper.java:137)
10-30 06:03:13.066: E/AndroidRuntime(13692):    at android.app.ActivityThread.main(ActivityThread.java:5041)
10-30 06:03:13.066: E/AndroidRuntime(13692):    at java.lang.reflect.Method.invokeNative(Native Method)
10-30 06:03:13.066: E/AndroidRuntime(13692):    at java.lang.reflect.Method.invoke(Method.java:511)
10-30 06:03:13.066: E/AndroidRuntime(13692):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-30 06:03:13.066: E/AndroidRuntime(13692):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-30 06:03:13.066: E/AndroidRuntime(13692):    at dalvik.system.NativeStart.main(Native Method)

我哪里错了?

请帮我,

谢谢。

4

5 回答 5

1

试试这个

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;
}

返回密码前必须关闭光标

于 2013-10-30T06:00:53.283 回答
0

我认为

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

是不正确的。也许你应该改为

"CREATE TABLE pollingdata(username TEXT, userid INTEGER PRIMARY KEY, password TEXT)"
于 2013-10-30T06:00:05.103 回答
0

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

于 2013-10-30T06:01:13.697 回答
0

改变

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

String password="";
if(cursor!=null)
{
    cursor.moveToFirst();
    password= cursor.getString("password");
}
return password;
于 2013-10-30T06:02:26.257 回答
0

问题解决了 :)

我得到了答案,

这个我没写

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

感谢大家的帮助。

于 2013-10-30T06:12:35.807 回答