gpt4 book ai didi

android - 匹配用户名和密码时出现空异常

转载 作者:行者123 更新时间:2023-11-30 03:18:09 25 4
gpt4 key购买 nike

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

这是我的代码,

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

这是我的数据库类 Activity ,

创建表时,

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)

我哪里错了?

请帮帮我,

谢谢。

最佳答案

试试这个

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

返回密码前必须关闭光标

关于android - 匹配用户名和密码时出现空异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19674865/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com