gpt4 book ai didi

android - 重新启动 Activity 时 SQLite 数据库丢失

转载 作者:行者123 更新时间:2023-11-29 00:10:51 25 4
gpt4 key购买 nike

我正在创建一个应用程序,用于在数据库中插入数据并从数据库中检索数据。当第一次 Activity 开始时,数据插入数据库并从数据库中检索数据,但是当我重新启动 Activity 并单击按钮从数据库中检索数据时,我的应用程序因错误而关闭。请帮助我??

数据库助手类

public class DataBaseHelper extends SQLiteOpenHelper
{
public DataBaseHelper(Context context, String name,CursorFactory factory, int version)
{
super(context, name, factory, version);
}
// Called when no database exists in disk and the helper class needs
// to create a new one.
@Override
public void onCreate(SQLiteDatabase _db)
{
_db.execSQL(DetailDataBaseAdapter.DATABASE_CREATE);


}
// Called when there is a database version mismatch meaning that the version
// of the database on disk needs to be upgraded to the current version.
@Override
public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion)
{
// Log the version upgrade.
Log.w("TaskDBAdapter", "Upgrading from version " +_oldVersion + " to " +_newVersion + ", which will destroy all old data");

// Upgrade the existing database to conform to the new version. Multiple
// previous versions can be handled by comparing _oldVersion and _newVersion
// values.
// The simplest case is to drop the old table and create a new one.
_db.execSQL("DROP TABLE IF EXISTS " + "TEMPLATE");
// Create a new one.
onCreate(_db);
}

}

数据库适配器类

public class DetailDataBaseAdapter{

static final String DATABASE_NAME = "phone.db";
static final int DATABASE_VERSION = 1;
public static final int NAME_COLUMN = 1;
// TODO: Create public field for each column in your table.
// SQL Statement to create a new database.
static final String DATABASE_CREATE = "create table "+"PHONE"+
"( " +"ID"+" integer primary key autoincrement,"+ "NUMBER text,SIM text,IMEI text); ";
// Variable to hold the database instance
public SQLiteDatabase db;
// Context of the application using the database.
private final Context context;
// Database open/upgrade helper
private DataBaseHelper dbHelper;
public DetailDataBaseAdapter(Context _context)
{
context = _context;
dbHelper = new DataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public DetailDataBaseAdapter open() throws SQLException
{
db = dbHelper.getWritableDatabase();
return this;
}
public void close()
{
db.close();
}
public SQLiteDatabase getDatabaseInstance()
{
return db;
}

public void insertEntry(String number,String sim,String imei)
{
ContentValues newValues = new ContentValues();
// Assign values for each row.
newValues.put("NUMBER", number);
newValues.put("SIM",sim);
newValues.put("IMEI",imei);

// Insert the row into your table
db.insert("PHONE", null, newValues);
///Toast.makeText(context, "Reminder Is Successfully Saved", Toast.LENGTH_LONG).show();
}

public String getData(String number)
{
Cursor cursor=db.query("PHONE", null, " NUMBER=?", new String[]{number}, null, null, null);
if(cursor.getCount()<1) // UserName Not Exist
{
cursor.close();
return "NOT EXIST";
}
cursor.moveToFirst();
String sim_id= cursor.getString(cursor.getColumnIndex("NUMBER"));
cursor.close();
return sim_id;
}

}

主 Activity 类

public class MainActivity extends Activity{
TextView tv;
String telNumber,simID,IMEI;
Button btn,bt;
TelephonyManager tm;
DetailDataBaseAdapter detailDataBaseAdapter;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
btn = (Button) findViewById(R.id.ok);
detailDataBaseAdapter=new DetailDataBaseAdapter(this);
detailDataBaseAdapter=detailDataBaseAdapter.open();
btn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
simID = tm.getSimSerialNumber();
if (simID != null) {
tv=(TextView)findViewById(R.id.tv_sim_no);
tv.setText(simID);
}
// ---get the phone number---
telNumber = tm.getLine1Number();
if (telNumber != null) {
tv=(TextView)findViewById(R.id.tv_phone_no);
tv.setText(telNumber);
}
// ---get the IMEI number---
IMEI = tm.getDeviceId();
if (IMEI != null) {
tv=(TextView)findViewById(R.id.tv_imei_no);
tv.setText(IMEI);
}

detailDataBaseAdapter.insertEntry(telNumber,simID,IMEI);
}
});
bt=(Button)findViewById(R.id.getDetail);
bt.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
String storedPassword=detailDataBaseAdapter.getData(telNumber);


Toast.makeText(MainActivity.this, storedPassword, Toast.LENGTH_LONG).show();

}
});

}
}

日志

FATAL EXCEPTION: main
Process: com.example.phone_detail, PID: 10548
E/AndroidRuntime(10548): java.lang.IllegalArgumentException: the bind value at index 1 is null
at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:164)
at android.database.sqlite.SQLiteProgram.bindAllArgsAsStrings(SQLiteProgram.java:200)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)
at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1163)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1034)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1202)
at com.example.phone_detail.DetailDataBaseAdapter.getData(DetailDataBaseAdapter.java:59)
at com.example.phone_detail.MainActivity$2.onClick(MainActivity.java:59)

最佳答案

logCat 表示您正在绑定(bind) null 以在您的 getData 方法中进行查询 - 因此您可能正在使用 null 参数调用此方法。在调用 db.query

之前尝试检查参数是否为 null

关于android - 重新启动 Activity 时 SQLite 数据库丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30211986/

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