Android应用连接SQLite数据库后无法正常运行求助
解决Android SQLite应用无响应/无法显示界面的问题
嘿,我仔细看了你的代码和遇到的问题,这几个关键问题导致了应用无法正常显示界面甚至无响应:
1. 布局文件不完整(核心原因)
你的activity_main.xml只写了ConstraintLayout的起始标签,没有添加你在MainActivity中用到的EditText、TextView和操作按钮,这会导致应用加载布局时找不到对应的控件,直接引发界面加载失败,出现你看到的无响应/空白界面。
补全后的activity_main.xml示例:
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <EditText android:id="@+id/user_input" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="输入产品名称" android:layout_margin="16dp" app:layout_constraintTop_toTopOf="parent"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginHorizontal="16dp" app:layout_constraintTop_toBottomOf="@id/user_input"> <Button android:id="@+id/add_button" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="添加" android:onClick="addButtonClicked"/> <Button android:id="@+id/delete_button" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="删除" android:onClick="deleteButtonClicked"/> </LinearLayout> <TextView android:id="@+id/user_text" android:layout_width="match_parent" android:layout_height="0dp" android:layout_margin="16dp" app:layout_constraintTop_toBottomOf="@id/add_button" app:layout_constraintBottom_toBottomOf="parent"/> </android.support.constraint.ConstraintLayout>
2. Cursor未关闭导致资源泄漏
在MyDBHandler的databaseToString()方法中,你获取了Cursor对象但没有关闭它,这会造成内存泄漏,长时间运行会导致应用无响应。
修改后的databaseToString()方法:
//printout database as a string public String databaseToString(){ String dbString = ""; SQLiteDatabase db = this.getReadableDatabase(); // 读取操作使用getReadableDatabase更合适 String query="SELECT * FROM " + TABLE_PRODUCTS; Cursor c=db.rawQuery(query, null); try { //move to first row in your results if (c.moveToFirst()) { do { String productName = c.getString(c.getColumnIndex(COLUMN_PRODUCTNAME)); if (productName != null){ dbString += productName; dbString +="\n"; } } while (c.moveToNext()); } } finally { c.close(); // 无论是否有数据,都关闭Cursor释放资源 db.close(); } return dbString; }
3. Delete方法的SQL注入风险与不规范写法
你的deleteproduct()方法使用字符串拼接SQL语句,存在SQL注入风险,建议使用SQLiteDatabase内置的delete()方法,更安全且规范。
修改后的deleteproduct()方法:
//delete a row in database public void deleteproduct(String productName){ SQLiteDatabase db=getWritableDatabase(); // 使用delete方法,避免SQL注入风险 db.delete(TABLE_PRODUCTS, COLUMN_PRODUCTNAME + " = ?", new String[]{productName}); db.close(); }
4. 额外优化点
在MainActivity的onDestroy()方法中,增加空判断避免空指针异常:
@Override protected void onDestroy() { if(myDBHandler != null){ myDBHandler.close(); } super.onDestroy(); }
按照上面的修改后,先补全布局文件,再修复数据库操作的资源泄漏和安全问题,你的应用应该就能正常显示界面并运行了。
内容的提问来源于stack exchange,提问作者Randika Wanninayaka
相关产品推荐
相关产品推荐

