我试图创建一个简单的列表应用程序,但是当我尝试删除列中的所有值时,应用程序崩溃了。我的XML如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.berserker.shoppinglist.MainActivity">
<ListView
android:id="@+id/listing"
android:layout_width="match_parent"
android:layout_height="430dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp" />
<Button
android:id="@+id/Add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="447dp"
android:layout_marginBottom="17dp"
android:onClick="addNewList"
android:layout_marginLeft="104dp"
android:layout_marginStart="104dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:id="@+id/clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/Add"
android:layout_toEndOf="@+id/Add"
android:layout_toRightOf="@+id/Add"
android:text="Clear All"
android:onClick="clear"
/>
这是我的代码:
public class MainActivity extends AppCompatActivity {
public ListView lv;
ArrayAdapter<String> adapters;
ArrayList<String> titles = new ArrayList<String>();
String[] split;
File path = Environment.getDataDirectory();
SQLiteDatabase titleDB;
Cursor resultTitles;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
titleDB = openOrCreateDatabase("titles", MODE_APPEND, null);
titleDB.execSQL("CREATE TABLE IF NOT EXISTS Titles(title VARCHAR);");
resultTitles = titleDB.rawQuery("Select * from Titles",null);
resultTitles.moveToFirst();
String title = "";
long count = DatabaseUtils.longForQuery(titleDB, "SELECT COUNT(*) FROM titles", null);
while (resultTitles.moveToNext()) {
title = resultTitles.getString(resultTitles.getColumnIndex("title"));
titles.add(title);
}
titles.add("" + (int) count);
titleDB.close();
//Adapter for the ListView
adapters = new ArrayAdapter<String>(this, R.layout.simplerow, titles);
// Add Adapter to List
lv = (ListView) findViewById(R.id.listing);
lv.setAdapter(adapters);
}
public void clear(View v) {
titleDB.delete("title",null,null);
titleDB.execSQL("CREATE TABLE IF NOT EXISTS Titles(title VARCHAR);");
}
public void addNewList(View v) {
Intent intent2 = new Intent(this, addItem.class);
startActivity(intent2);
}
}
当我按下清除所有按钮,它连接到clear()功能onclick时,它会立即崩溃。我想删除表标题列中的所有当前数据,但仍保留列标题。任何想法为什么它崩溃?
这是我的 LogCat :
06-17 10:39:49.926 32150-32150/? I/art: Not late-enabling -Xcheck:jni (already on)
06-17 10:39:49.926 32150-32150/? W/art: Unexpected CPU variant for X86 using defaults: x86
06-17 10:39:49.963 32150-32157/? E/art: Failed sending reply to debugger: Broken pipe
06-17 10:39:49.963 32150-32157/? I/art: Debugger is no longer active
06-17 10:39:49.963 32150-32157/? I/art: Starting a blocking GC Instrumentation
06-17 10:39:50.126 32150-32150/? W/System: ClassLoader referenced unknown path: /data/app/com.berserker.shoppinglist-1/lib/x86
06-17 10:39:50.137 32150-32150/? I/InstantRun: starting instant run server: is main process
06-17 10:39:50.267 32150-32150/? W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
06-17 10:39:50.470 32150-32166/? I/OpenGLRenderer: Initialized EGL, version 1.4
06-17 10:39:50.470 32150-32166/? D/OpenGLRenderer: Swap behavior 1
06-17 10:39:50.470 32150-32166/? W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
06-17 10:39:50.470 32150-32166/? D/OpenGLRenderer: Swap behavior 0
06-17 10:39:50.492 32150-32166/? D/EGL_emulation: eglCreateContext: 0xa735ebc0: maj 2 min 0 rcv 2
06-17 10:39:50.519 32150-32166/? D/EGL_emulation: eglMakeCurrent: 0xa735ebc0: ver 2 0 (tinfo 0xa907b710)
06-17 10:39:50.569 32150-32166/? D/EGL_emulation: eglMakeCurrent: 0xa735ebc0: ver 2 0 (tinfo 0xa907b710)
06-17 10:39:52.069 32150-32150/com.berserker.shoppinglist D/AndroidRuntime: Shutting down VM
06-17 10:39:52.072 32150-32150/com.berserker.shoppinglist E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.berserker.shoppinglist, PID: 32150
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/user/0/com.berserker.shoppinglist/databases/titles
at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1662)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1608)
at com.berserker.shoppinglist.MainActivity.clear(MainActivity.java:75)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
06-17 10:42:18.481 32150-32157/com.berserker.shoppinglist W/art: Suspending all threads took: 20.975ms
答案 0 :(得分:1)
public class MainActivity extends AppCompatActivity {
public ListView lv;
ArrayAdapter<String> adapters;
ArrayList<String> titles = new ArrayList<String>();
String[] split;
File path = Environment.getDataDirectory();
SQLiteDatabase titleDB;
Cursor resultTitles;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
titleDB = openOrCreateDatabase("titles", MODE_APPEND, null);
titleDB.execSQL("CREATE TABLE IF NOT EXISTS Titles(title VARCHAR);");
resultTitles = titleDB.rawQuery("Select * from Titles",null);
resultTitles.moveToFirst();
String title = "";
long count = DatabaseUtils.longForQuery(titleDB, "SELECT COUNT(*) FROM titles", null);
while (resultTitles.moveToNext()) {
title = resultTitles.getString(resultTitles.getColumnIndex("title"));
titles.add(title);
}
titles.add("" + (int) count);
titleDB.close();
//Adapter for the ListView
adapters = new ArrayAdapter<String>(this, R.layout.simplerow, titles);
// Add Adapter to List
lv = (ListView) findViewById(R.id.listing);
lv.setAdapter(adapters);
}
public void clear(View v) {
titleDB = openOrCreateDatabase("titles", MODE_APPEND, null);
titleDB.delete("title",null,null);
titleDB.execSQL("CREATE TABLE IF NOT EXISTS Titles(title VARCHAR);");
}
public void addNewList(View v) {
Intent intent2 = new Intent(this, addItem.class);
startActivity(intent2);
}
}
试试这个
答案 1 :(得分:0)
检查android:onClick in xml和java code这样,然后再试一次。
public void onClickClear(View v) {
titleDB.delete("title",null,null);
titleDB.execSQL("CREATE TABLE IF NOT EXISTS Titles(title VARCHAR);");
}
无法执行android的方法:onClick
答案 2 :(得分:0)
尝试使用此代码来解决您的问题。这里的mOpenHelper是DatabaseHelper,您可以在onCreate()方法中初始化,如下所示:
的onCreate()
DatabaseHelper mOpenHelper = new DatabaseHelper(this);
private void clear(View v) {
try {
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
db.delete(TABLE_NAME, " title", null);
} catch (SQLException e) {
}
获取更多想法,您可以通过java2s.com