我尝试使用WelcomeActivity或splash活动单击登录后,从SQLiteDbHelper检索我的电子邮件并在另一个活动的TextView中查看它,因为我只希望查看它4秒钟,但是idk是什么登录时出现问题,使我崩溃。
DataBaseHelper.java
public class DataBaseHelper extends SQLiteOpenHelper
{
private DataBaseHelper dbHelper;
public static SQLiteDatabase m_db;
public static final String DB_NAME = "users.dbHelper";
public static final int DB_VERSION = 1;
public static final String TABLE_USERS = "users";
public static final String COLUMN_EMAIL = "email";
public static final String COLUMN_PASSWORD = "password";
public static final String [] ALL_COLUMNS = {COLUMN_EMAIL, COLUMN_PASSWORD};
public static final String SQL_CREATE =
"CREATE TABLE IF NOT EXISTS " + TABLE_USERS + " (" +
COLUMN_EMAIL + " STRING PRIMARY KEY, " +
COLUMN_PASSWORD + " STRING);";
public static final String SQL_DROP = "DROP TABLE " + TABLE_USERS;
public DataBaseHelper(@Nullable Context context)
{
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(SQL_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL(SQL_DROP);
onCreate(db);
}
//---opens the database---
public DataBaseHelper open() throws SQLException
{
m_db = dbHelper.getWritableDatabase();
return this;
}
//---closes the database---
public void close()
{
if (m_db != null)
m_db.close();
if (dbHelper != null)
dbHelper.close();
}
// Inserting in database
public boolean insert(String email, String password)
{
m_db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("email", email);
contentValues.put("password", password);
long ins = m_db.insert(TABLE_USERS, null, contentValues);
if (ins == -1) return false;
else return true;
}
// Checking if email exists
public boolean checkEmail(String COLUMN_EMAIL)
{
m_db = this.getWritableDatabase();
Cursor cursor = m_db.rawQuery("select * from TABLE_USERS where COLUMN_EMAIL=?",
new String[]{COLUMN_EMAIL});
if (cursor.getCount() > 0) return false;
else return true;
}
// Checking the email and password
public boolean checkEmailPassword(String COLUMN_EMAIL, String COLUMN_PASSWORD)
{
m_db = this.getWritableDatabase();
Cursor cursor = m_db.rawQuery("select * from TABLE_USERS where COLUMN_EMAIL=?
and COLUMN_PASSWORD=?", new String[]{COLUMN_EMAIL, COLUMN_PASSWORD});
if (cursor.getCount() > 0) return true;
else return false;
}
public String getEmail(String COLUMN_EMAIL)
{
m_db = this.getReadableDatabase();
Cursor cursor = m_db.query(TABLE_USERS, new String[]{COLUMN_EMAIL}, null, null,
null, null, null);
cursor.moveToFirst();
String user = cursor.getString(cursor.getColumnIndex(COLUMN_EMAIL));
cursor.close();
return user;
}
}
WelcomeActivity.Java
public class WelcomeActivity extends AppCompatActivity
{
private static int SPLASH_TIME_OUT = 4000;
private DataBaseHelper db;
private SQLiteDatabase m_db;
private TextView tvEmail2;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
db = new DataBaseHelper(this);
TextView tvEmail = findViewById(R.id.tvEmail2);
String email = db.getEmail(DataBaseHelper.COLUMN_EMAIL);
String user = email;
tvEmail.setText(Users.class.getEm);
new Handler().postDelayed(new Runnable()
{
@Override
public void run()
{
Intent intent = new Intent(WelcomeActivity.this, EthicsActivity.class);
startActivity(intent);
finish();
}
}, SPLASH_TIME_OUT);
}
}
Users.java
public class Users
{
private String email;
private String password;
public Users() {}
public Users(String email, String password)
{
this.email = email;
this.password = password;
}
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
@Override
public String toString()
{
return "Users{" +
"email='" + email + '\'' +
", password='" + password + '\'' +
'}';
}
}
你们对此有何看法? 我尝试使用WelcomeActivity或splash活动单击登录后,从SQLiteDbHelper检索我的电子邮件并在另一个活动的TextView中查看它,因为我只希望查看它4秒钟,但是idk当我出现问题时登录它会使我崩溃。
答案 0 :(得分:0)
我相信您有很多问题。
我认为tvEmail.setText(Users.class.getEm);
行不会编译,而是需要tvEmail.setText(user);
或tvEmail.setText(email);
(您不必同时拥有email
和user
具有相同值的字符串。)
假设EthicsActivity是有效的工作活动,并且清单 AndroidManifest.xml 中有适当的 activity 条目,那么问题是您将得到一个错误遵循:-
11-10 19:56:12.863 1170-1170/so53240174.so53240174 E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{so53240174.so53240174/so53240174.so53240174.WelcomeActivity}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:418)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
at so53240174.so53240174.DataBaseHelper.getEmail(DataBaseHelper.java:96)
at so53240174.so53240174.WelcomeActivity.onCreate(WelcomeActivity.java:25)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
这就是说Cursor为空,因此您无法从中获取任何数据,并且它发生在 getEmail 方法中的 String user = cursor.getString(cursor.getColumnIndex(COLUMN_EMAIL));
行如日志中的行所示:-
at so53240174.so53240174.DataBaseHelper.getEmail(DataBaseHelper.java:96)
发生这种情况的原因是:a) users 表中没有行,并且b)未检查Cursor moveToFirst 方法的结果。取而代之的是,假设它将始终移至游标的第一行。
而不是 getEmail 方法:-
public String getEmail(String COLUMN_EMAIL)
{
m_db = this.getReadableDatabase();
Cursor cursor = m_db.query(TABLE_USERS, new String[]{COLUMN_EMAIL}, null, null,
null, null, null);
cursor.moveToFirst(); //<<<<<<<<<< WRONG
String user = cursor.getString(cursor.getColumnIndex(COLUMN_EMAIL));
cursor.close();
return user;
}
可能是:-
public String getEmail(String COLUMN_EMAIL) {
String user = "No Email Found."; //<<<<<<<<<< Default value in case of empty table
m_db = this.getReadableDatabase();
Cursor cursor = m_db.query(TABLE_USERS, new String[]{COLUMN_EMAIL}, null, null,
null, null, null);
if (cursor.moveToFirst()) { //<<<<<<<<<< checks result of the move
user = cursor.getString(cursor.getColumnIndex(COLUMN_EMAIL));
}
cursor.close();
return user;
}
当然可以应用此修复程序,除非将行添加到 users 表中,否则将始终导致TextView显示未找到电子邮件。如果有多行,它将显示其中一行的电子邮件,而不必显示特定行。
在通过调用 getEmail 检索值之前添加行db.insert("fred@fredmail.com","1234567890");
,结果 fred@fredmail.com 显示在TextView中。 / p>