gpt4 book ai didi

android - SQLite没有打开的构造函数错误

转载 作者:行者123 更新时间:2023-12-02 11:05:54 25 4
gpt4 key购买 nike

尝试将数据插入我的sqlite数据库时,我收到logcat错误“无空构造函数”。错误消息听起来很直接,但我可以理解empy构造函数将如何帮助我将数据插入数据库。

错误:

 E/AndroidRuntime(531): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.LoginScr.Example/com.LoginScr.Example.LoginDB}: java.lang.InstantiationException: can't instantiate class com.LoginScr.Example.LoginDB; no empty constructor

数据库:
  public class LoginDB extends SQLiteOpenHelper {

//Table attributes
public static final String DATABASE_NAME = "logindata.db";
public static final int DATABASE_VERSION = 1;
public static final String TABLE_NAME_INFOTABLE = "credentials";

// Data attributes
public static final String COLUMN_NAME_USERNAME = "username";
public static final String COLUMN_NAME_PASSWORD = "password";

private SQLiteOpenHelper DBHelper;
private SQLiteDatabase db;



public LoginDB(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub

String sqlDataStore = "create table if not exists " +
DATABASE_NAME + " ("+ BaseColumns._ID + " integer primary key autoincrement,"

+ COLUMN_NAME_USERNAME + " text not null,"
+ COLUMN_NAME_PASSWORD + " text not null);";

db.execSQL(sqlDataStore);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
if(oldVersion == 1 && newVersion == 2){
//Upgrade the database
}

}

public boolean Login(String username, String password) throws SQLException
{
Cursor mCursor = db.rawQuery("SELECT * FROM " + DATABASE_NAME + " WHERE username=? AND password=?",
new String[]{username,password});
if(mCursor !=null) {
if(mCursor.getCount()>0)
{
return true;
}
}
return false;
}

public void open() {
// TODO Auto-generated method stub
db = DBHelper.getWritableDatabase();
}
public void close() {
DBHelper.close();
}
}

Activity :
      public class CredentialsActivity extends Activity implements OnClickListener{

private Button lReg;
private EditText lUname;
private EditText lPword;

private LoginDB loginDb = new LoginDB(CredentialsActivity.this);

@Override
protected void onCreate (Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.register);

lReg = (Button)findViewById(R.id.reg_button);
lReg.setOnClickListener(this);
}

public void onClick(View v) {

switch(v.getId()) {

case R.id.reg_button:
lUname = (EditText)findViewById(R.id.reg_uname);
lPword = (EditText)findViewById(R.id.reg_pswd);

String uname = lUname.getText().toString();
String pass = lPword.getText().toString();
boolean invalid = false;

if(uname.equals("")){
invalid = true;
Toast.makeText(getApplicationContext(), "Username Missing", Toast.LENGTH_SHORT).show();
}else if(pass.equals("")){
invalid = true;
Toast.makeText(getApplicationContext(), "Password Missing", Toast.LENGTH_SHORT).show();
}
if(invalid == false){
addEntry(uname, pass);
Intent i_register = new Intent(CredentialsActivity.this, LoginDB.class);
startActivity(i_register);
finish();
}}
}
public void onDestroy() {
super.onDestroy();
loginDb.close();
}

public void addEntry(String uname, String pass){

SQLiteDatabase db = loginDb.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("username", uname);
values.put("password", pass);

try{
db.insert(LoginDB.DATABASE_NAME, null, values);
Toast.makeText(getApplicationContext(), "Saved! Please login now", Toast.LENGTH_SHORT).show();
}catch(Exception err){
err.printStackTrace();
}
}
}

最佳答案

去掉

private LoginDB loginDb = new LoginDB(CredentialsActivity.this);

而是从 ActivityonCreate调用相同的代码:
private LoginDB loginDb;
void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
loginDb = new LoginDB(CredentialsActivity.this);
...
}

关于android - SQLite没有打开的构造函数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12015435/

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