gpt4 book ai didi

android - 从 sqlite 数据库中搜索用户并授予他登录权限?

转载 作者:行者123 更新时间:2023-11-29 01:23:40 26 4
gpt4 key购买 nike

我是 android 开发的新手,我已经创建了一个可以正常工作的数据库。但是当我想从该数据库搜索用户时,我的应用程序停止工作。我已经创建了三个类,一个用于创建并且工作正常,一个用于访问数据库以登录用户和一个数据库帮助类

这是我的数据库助手代码

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


public class DatabaseHelper extends SQLiteOpenHelper {

private static final int DATABASE_VERSION=1;
private static final String DATABASE_NAME="USER.db";
private static final String TABLE_NAME="USER";
private static final String COL_1="ID";
private static final String COL_2="NAME";
private static final String COL_3="EMAIL";
private static final String COL_4="PASS";
SQLiteDatabase db;



public DatabaseHelper(Context context)
{
super(context, DATABASE_NAME , null , DATABASE_VERSION );
}


@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, EMAIL TEXT, PASS TEXT)");

}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXIST "+TABLE_NAME);
onCreate(db);

}
public void insertData(String name, String email, String pass)
{
SQLiteDatabase db=this.getWritableDatabase();
ContentValues values=new ContentValues();
ContentValues contentValues=new ContentValues();
contentValues.put(COL_2,name);
contentValues.put(COL_3,email);
contentValues.put(COL_4, pass);
// long result=
db.insert(TABLE_NAME, null, contentValues);
//if (result==-1)
// return false;
//else
// return true;
db.close();
}
public String searchUser(String email){
SQLiteDatabase db=this.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from "+TABLE_NAME, null);
String a, b;
b = "not found";
if (cursor.moveToFirst()) {
do {
a = cursor.getString(0);

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

}
return b;

}

这是我想访问数据库的登录类

public class Login extends AppCompatActivity implements View.OnClickListener {
EditText editpassword, editemail;
TextView linkRegister;
Button buttonRegister;
DatabaseHelper myDB;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);

editemail = (EditText) findViewById(R.id.edit_email);
editpassword = (EditText) findViewById(R.id.edit_password);
linkRegister=(TextView)findViewById(R.id.LinkRegister);
buttonRegister=(Button)findViewById(R.id.ButtonSignin);

buttonRegister.setOnClickListener(this);
linkRegister.setOnClickListener(this);



}


@Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.LinkRegister:
startActivity(new Intent(Login.this, Register.class));
break;
case R.id.ButtonSignin:
//Toast mess = Toast.makeText(Login.this, "username and password don't match", Toast.LENGTH_SHORT);
//mess.show();
String email=editemail.getText().toString();
String password=editpassword.getText().toString();
String dbpassword=myDB.searchUser(email);
if(password.equals(dbpassword))
{
Toast mess = Toast.makeText(Login.this, "username and password matched", Toast.LENGTH_SHORT);
mess.show();

}
else{
Toast msg = Toast.makeText(Login.this, "username and password don't match", Toast.LENGTH_SHORT);
msg.show();
}
// startActivity(new Intent(Login.this, NewsFeed.class));
break;

}

}

这是我的日志

02-09 17:22:15.321 18955-18955/? D/dalvikvm: Late-enabling CheckJNI
02-09 17:22:15.491 18955-18955/? W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
02-09 17:22:15.491 18955-18955/? I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.view.WindowCallbackWrapper.onSearchRequested
02-09 17:22:15.491 18955-18955/? W/dalvikvm: VFY: unable to resolve interface method 17969: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
02-09 17:22:15.491 18955-18955/? D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
02-09 17:22:15.491 18955-18955/? I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode
02-09 17:22:15.491 18955-18955/? W/dalvikvm: VFY: unable to resolve interface method 17973: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
02-09 17:22:15.491 18955-18955/? D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
02-09 17:22:15.591 18955-18955/? I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.widget.TintTypedArray.getChangingConfigurations
02-09 17:22:15.591 18955-18955/? W/dalvikvm: VFY: unable to resolve virtual method 424: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
02-09 17:22:15.591 18955-18955/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
02-09 17:22:15.591 18955-18955/? I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.widget.TintTypedArray.getType
02-09 17:22:15.591 18955-18955/? W/dalvikvm: VFY: unable to resolve virtual method 446: Landroid/content/res/TypedArray;.getType (I)I
02-09 17:22:15.591 18955-18955/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
02-09 17:22:15.641 18955-18955/? I/dalvikvm: Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.widget.ResourcesWrapper.getDrawable
02-09 17:22:15.641 18955-18955/? W/dalvikvm: VFY: unable to resolve virtual method 387: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
02-09 17:22:15.641 18955-18955/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
02-09 17:22:15.641 18955-18955/? I/dalvikvm: Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.widget.ResourcesWrapper.getDrawableForDensity
02-09 17:22:15.641 18955-18955/? W/dalvikvm: VFY: unable to resolve virtual method 389: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
02-09 17:22:15.641 18955-18955/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
02-09 17:22:15.781 18955-18955/? D/OpenGLRenderer: Enabling debug mode 0
02-09 17:22:33.721 18955-18955/com.example.mubbasher.howdy D/AndroidRuntime: Shutting down VM
02-09 17:22:33.721 18955-18955/com.example.mubbasher.howdy W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41631c08)
02-09 17:22:33.721 18955-18955/com.example.mubbasher.howdy E/AndroidRuntime: FATAL EXCEPTION: main
02-09 17:22:33.721 18955-18955/com.example.mubbasher.howdy E/AndroidRuntime: Process: com.example.mubbasher.howdy, PID: 18955
02-09 17:22:33.721 18955-18955/com.example.mubbasher.howdy E/AndroidRuntime: java.lang.NullPointerException
02-09 17:22:33.721 18955-18955/com.example.mubbasher.howdy E/AndroidRuntime: at com.example.mubbasher.howdy.Login.onClick(Login.java:48)
02-09 17:22:33.721 18955-18955/com.example.mubbasher.howdy E/AndroidRuntime: at android.view.View.performClick(View.java:4661)
02-09 17:22:33.721 18955-18955/com.example.mubbasher.howdy E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:19498)
02-09 17:22:33.721 18955-18955/com.example.mubbasher.howdy E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:733)
02-09 17:22:33.721 18955-18955/com.example.mubbasher.howdy E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
02-09 17:22:33.721 18955-18955/com.example.mubbasher.howdy E/AndroidRuntime: at android.os.Looper.loop(Looper.java:146)
02-09 17:22:33.721 18955-18955/com.example.mubbasher.howdy E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5641)
02-09 17:22:33.721 18955-18955/com.example.mubbasher.howdy E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
02-09 17:22:33.721 18955-18955/com.example.mubbasher.howdy E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515)
02-09 17:22:33.721 18955-18955/com.example.mubbasher.howdy E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1288)
02-09 17:22:33.721 18955-18955/com.example.mubbasher.howdy E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1104)
02-09 17:22:33.721 18955-18955/com.example.mubbasher.howdy E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)

最佳答案

您忘记在登录类中初始化 myDB。

在登录类的 onCreate() 方法中写下一行。

 DatabaseHelper myDB;
myDB=new DatabaseHelper(LoginActivity.this);

关于android - 从 sqlite 数据库中搜索用户并授予他登录权限?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35291910/

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