我试图在android中创建一个sqlite数据库来备份文本。这是我第一次使用sqlite数据库,所以我很喜欢它。当打开数据库并在日志中抛出这些错误时,它会崩溃
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3591)
at android.view.View.performClick(View.java:4084)
at android.view.View$PerformClick.run(View.java:16966)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
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: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View$1.onClick(View.java:3586)
at android.view.View.performClick(View.java:4084)
at android.view.View$PerformClick.run(View.java:16966)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
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: java.lang.IllegalArgumentException: Version must be >= 1, was -1669636
at android.database.sqlite.SQLiteOpenHelper.<init>(SQLiteOpenHelper.java:100)
at android.database.sqlite.SQLiteOpenHelper.<init>(SQLiteOpenHelper.java:78)
at com.swavey.testing.smsDatabase$DatabaseHelper.<init>(smsDatabase.java:115)
at com.swavey.testing.smsDatabase.<init>(smsDatabase.java:82)
at com.swavey.testing.MainActivity.Backup(MainActivity.java:44)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View$1.onClick(View.java:3586)
at android.view.View.performClick(View.java:4084)
at android.view.View$PerformClick.run(View.java:16966)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
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)
以下是我的主要活动中的代码
SimpleCursorAdapter adapter;
private smsDatabase db1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void Backup(View view) {
Uri convo = Uri.parse("content://sms");
String[] reqCol = new String[] {"_id", "thread_id", "address", "person", "date", "body", "type"};
ContentResolver contentResolver = getContentResolver();
Cursor c = contentResolver.query(convo, null, null, null, "date asc");
SMSList<SMS> smsList = new SMSList<SMS>();
String count = Integer.toString(c.getCount());
Log.d("Count",count);
Context ctx = getApplicationContext();
db1 = new smsDatabase(ctx);
db1.open();
while (c.moveToNext()) {
SMS txt = new SMS();
long dateLong = c.getLong(c.getColumnIndex("date"));
Integer id1 = c.getInt(c.getColumnIndex("_id"));
String body = c.getString(c.getColumnIndex("body"));
Integer type1 = c.getInt(c.getColumnIndex("type"));
String address1 = c.getString(c.getColumnIndex("address"));
String read1 = c.getString(c.getColumnIndex("read"));
String seen1 = c.getString(c.getColumnIndex("seen"));
String subject1 = c.getString(c.getColumnIndex("subject"));
smsList.add(txt);
db1.insertSMS(txt);
}
}
和我的sqlite数据库中的代码
package com.swavey.testing;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
import android.security.KeyChain;
import java.util.Date;
/**
* Created by Adrian on 11/5/2014.
*/
public class smsDatabase {
private static final String KEY_ID = "_id";
private static final int COLUMN_ID =0;
// database info
public static final String DATABASE_NAME = "texts";
public static final String DATABASE_TABLE = "mainTable";
public static final int DATABASE_VERSION = (int) new Date().getTime()/1000;
// list of fields
private static final String KEY_ADDRESS = "address";
private static final String KEY_BODY = "body";
private static final String KEY_DATE = "date";
private static final String KEY_DATESENT = "dateSent";
private static final String KEY_READ = "read";
private static final String KEY_SUBJECT = "subject";
private static final String KEY_THREADID = "thread_id";
private static final String KEY_PERSON = "person";
private static final String KEY_PROTOCOL = "protocol";
private static final String KEY_TYPE = "type";
private static final String KEY_SERVICECENTER = "serviceCenter";
//list of field numbers
private static final int COL_ADDRESS = 1;
private static final int COL_BODY = 2;
private static final int COL_DATE = 3;
private static final int COL_DATESENT = 4;
private static final int COL_READ = 5;
private static final int COL_SUBJECT = 6;
private static final int COL_THREADID = 7;
private static final int COL_PERSON = 8;
private static final int COL_PROTOCOL = 9;
private static final int COL_TYPE = 10;
private static final int COL_SERVICECENTER = 11;
//create string array of all fields;
public static final String[] ALL_KEYS = new String[] {KEY_ID, KEY_ADDRESS, KEY_BODY, KEY_DATE,
KEY_DATESENT, KEY_PERSON, KEY_PROTOCOL,KEY_READ, KEY_SERVICECENTER, KEY_SUBJECT,
KEY_THREADID, KEY_TYPE};
private static final String DATABASE_CREATE_SQL = "create table " + DATABASE_TABLE
+ " (" + KEY_ID + " integer primary key autoincrement, "
+KEY_ADDRESS + " text not null, "
+KEY_BODY + " text not null, "
+KEY_DATE + " text not null, "
+KEY_DATE + " text not null, "
+KEY_PERSON+ " text not null, "
+KEY_PROTOCOL+ " text not null, "
+KEY_READ+ " text not null, "
+KEY_PERSON + " text not null, "
+KEY_PROTOCOL+ " text not null, "
+KEY_READ+ " text not null, "
+KEY_SERVICECENTER+ " text not null, "
+KEY_SUBJECT+ " text not null, "
+KEY_THREADID+ " text not null, "
+KEY_TYPE+ " text not null"
+ ");";
private final Context context;
private DatabaseHelper dbHelper;
private SQLiteDatabase db;
public smsDatabase (Context cxt) {
this.context = cxt;
dbHelper = new DatabaseHelper(context);
}
//open database
public smsDatabase open() {
db = dbHelper.getWritableDatabase();
return this;
}
//close database
public void close() {
dbHelper.close();
}
// insert sms into table
public long insertSMS (SMS sms) {
ContentValues iv = new ContentValues();
iv.put(KEY_ADDRESS, sms.getAddress());
iv.put(KEY_BODY, sms.getBody());
iv.put(KEY_DATE, sms.getDate());
iv.put(KEY_DATESENT, sms.getDateSent());
iv.put(KEY_PERSON, sms.getPerson());
iv.put(KEY_PROTOCOL, sms.getProtocol());
iv.put(KEY_READ, sms.getRead());
iv.put(KEY_SERVICECENTER, sms.getServiceCenter());
iv.put(KEY_SUBJECT, sms.getSubject());
iv.put(KEY_THREADID, sms.getThread_id());
iv.put(KEY_TYPE, sms.getType());
return db.insert(DATABASE_TABLE, null,iv);
}
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase _db) {
_db.execSQL(DATABASE_CREATE_SQL);
}
public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) {
_db.execSQL("DROP TABLE IF EXISTS "+ DATABASE_TABLE);
onCreate(_db);
}
}
}
答案 0 :(得分:0)
我首先看到你没有初始化你的db1,可能还有其他错误修复,然后我们可以看到更多。
public void Backup(View view) {
Uri convo = Uri.parse("content://sms");
String[] reqCol = new String[] {"_id", "thread_id", "address", "person", "date", "body", "type"};
ContentResolver contentResolver = getContentResolver();
Cursor c = contentResolver.query(convo, null, null, null, "date asc");
SMSList<SMS> smsList = new SMSList<SMS>();
String count = Integer.toString(c.getCount());
Log.d("Count",count);
db1.open();**//this is null**
答案 1 :(得分:0)
这可能会或可能不会完全解决您的问题,但似乎您有一个额外的分号
private static final String DATABASE_CREATE_SQL = "create table " + DATABASE_TABLE
+ " (" + KEY_ID + " integer primary key autoincrement, "
+KEY_ADDRESS + " text not null, "
+KEY_BODY + " text not null, "
+KEY_DATE + " text not null, "
+KEY_DATE + " text not null, "
+KEY_PERSON+ " text not null, "
+KEY_PROTOCOL+ " text not null, "
+KEY_READ+ " text not null, "
+KEY_PERSON + " text not null, "
+KEY_PROTOCOL+ " text not null, "
+KEY_READ+ " text not null, "
+KEY_SERVICECENTER+ " text not null, "
+KEY_SUBJECT+ " text not null, "
+KEY_THREADID+ " text not null, "
+KEY_TYPE+ " text not null"
+ ");";
^take this out