我开发了一个Android任务提醒应用程序。它没有错误,但有一些警告,这只是由于已弃用的方法。当我运行我的应用程序时,它说不方便的应用程序已经停止。任何人都可以告诉我背后的原因是什么。 这是我得到的logcat错误:
07-14 00:44:05.180: E/AndroidRuntime(1038): FATAL EXCEPTION: main
07-14 00:44:05.180: E/AndroidRuntime(1038): Process: com.android.taskreminder, PID: 1038
07-14 00:44:05.180: E/AndroidRuntime(1038): java.lang.RuntimeException: Unable to instantiate receiver com.android.taskreminder.OnBootReceiver: java.lang.ClassNotFoundException: Didn't find class "com.android.taskreminder.OnBootReceiver" on path: DexPathList[[],nativeLibraryDirectories=[/system/lib]]
07-14 00:44:05.180: E/AndroidRuntime(1038): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2400)
07-14 00:44:05.180: E/AndroidRuntime(1038): at android.app.ActivityThread.access$1700(ActivityThread.java:135)
07-14 00:44:05.180: E/AndroidRuntime(1038): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1272)
07-14 00:44:05.180: E/AndroidRuntime(1038): at android.os.Handler.dispatchMessage(Handler.java:102)
07-14 00:44:05.180: E/AndroidRuntime(1038): at android.os.Looper.loop(Looper.java:136)
07-14 00:44:05.180: E/AndroidRuntime(1038): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-14 00:44:05.180: E/AndroidRuntime(1038): at java.lang.reflect.Method.invokeNative(Native Method)
07-14 00:44:05.180: E/AndroidRuntime(1038): at java.lang.reflect.Method.invoke(Method.java:515)
07-14 00:44:05.180: E/AndroidRuntime(1038): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-14 00:44:05.180: E/AndroidRuntime(1038): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-14 00:44:05.180: E/AndroidRuntime(1038): at dalvik.system.NativeStart.main(Native Method)
07-14 00:44:05.180: E/AndroidRuntime(1038): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.android.taskreminder.OnBootReceiver" on path: DexPathList[[],nativeLibraryDirectories=[/system/lib]]
07-14 00:44:05.180: E/AndroidRuntime(1038): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
07-14 00:44:05.180: E/AndroidRuntime(1038): at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
07-14 00:44:05.180: E/AndroidRuntime(1038): at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
07-14 00:44:05.180: E/AndroidRuntime(1038): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2395)
07-14 00:44:05.180: E/AndroidRuntime(1038): ... 10 more
07-14 00:45:52.640: E/SQLiteLog(1163): (1) near "tablereminders": syntax error
07-14 00:45:52.700: E/AndroidRuntime(1163): FATAL EXCEPTION: main
07-14 00:45:52.700: E/AndroidRuntime(1163): Process: com.android.taskreminder, PID: 1163
07-14 00:45:52.700: E/AndroidRuntime(1163): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.taskreminder/com.android.taskreminder.ReminderListActivity}: android.database.sqlite.SQLiteException: near "tablereminders": syntax error (code 1): , while compiling: create tablereminders (_id integer primary key autoincrement, title text not null, body text not null,reminder_date_time text not null);
07-14 00:45:52.700: E/AndroidRuntime(1163): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
07-14 00:45:52.700: E/AndroidRuntime(1163): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-14 00:45:52.700: E/AndroidRuntime(1163): at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-14 00:45:52.700: E/AndroidRuntime(1163): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-14 00:45:52.700: E/AndroidRuntime(1163): at android.os.Handler.dispatchMessage(Handler.java:102)
07-14 00:45:52.700: E/AndroidRuntime(1163): at android.os.Looper.loop(Looper.java:136)
07-14 00:45:52.700: E/AndroidRuntime(1163): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-14 00:45:52.700: E/AndroidRuntime(1163): at java.lang.reflect.Method.invokeNative(Native Method)
07-14 00:45:52.700: E/AndroidRuntime(1163): at java.lang.reflect.Method.invoke(Method.java:515)
07-14 00:45:52.700: E/AndroidRuntime(1163): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-14 00:45:52.700: E/AndroidRuntime(1163): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-14 00:45:52.700: E/AndroidRuntime(1163): at dalvik.system.NativeStart.main(Native Method)
07-14 00:45:52.700: E/AndroidRuntime(1163): Caused by: android.database.sqlite.SQLiteException: near "tablereminders": syntax error (code 1): , while compiling: create tablereminders (_id integer primary key autoincrement, title text not null, body text not null,reminder_date_time text not null);
07-14 00:45:52.700: E/AndroidRuntime(1163): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
07-14 00:45:52.700: E/AndroidRuntime(1163): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
07-14 00:45:52.700: E/AndroidRuntime(1163): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
07-14 00:45:52.700: E/AndroidRuntime(1163): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
07-14 00:45:52.700: E/AndroidRuntime(1163): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
07-14 00:45:52.700: E/AndroidRuntime(1163): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
07-14 00:45:52.700: E/AndroidRuntime(1163): at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1672)
07-14 00:45:52.700: E/AndroidRuntime(1163): at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1603)
07-14 00:45:52.700: E/AndroidRuntime(1163): at com.android.taskreminder.RemindersDbAdapter$DatabaseHelper.onCreate(RemindersDbAdapter.java:36)
07-14 00:45:52.700: E/AndroidRuntime(1163): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
07-14 00:45:52.700: E/AndroidRuntime(1163): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
07-14 00:45:52.700: E/AndroidRuntime(1163): at com.android.taskreminder.RemindersDbAdapter.open(RemindersDbAdapter.java:48)
07-14 00:45:52.700: E/AndroidRuntime(1163): at com.android.taskreminder.ReminderListActivity.onCreate(ReminderListActivity.java:29)
07-14 00:45:52.700: E/AndroidRuntime(1163): at android.app.Activity.performCreate(Activity.java:5231)
07-14 00:45:52.700: E/AndroidRuntime(1163): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-14 00:45:52.700: E/AndroidRuntime(1163): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
07-14 00:45:52.700: E/AndroidRuntime(1163): ... 11 more
这是sql代码
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class RemindersDbAdapter {
private static final String DATABASE_NAME = "data";
private static final String DATABASE_TABLE = "reminders";
private static final int DATABASE_VERSION = 1;
public static final String KEY_TITLE = "title";
public static final String KEY_BODY = "body";
public static final String KEY_DATE_TIME = "reminder_date_time";
public static final String KEY_ROWID = "_id";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private static final String DATABASE_CREATE =
"CREATE TABLE" + DATABASE_TABLE + " ("+ KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, "+ KEY_TITLE + " TEXT NOT NULL, "
+ KEY_BODY + " TEXT NOT NULL," + KEY_DATE_TIME + " TEXT NOT NULL);";
private final Context mCtx;
public RemindersDbAdapter(Context ctx) {
this.mCtx = ctx;
}
//nested class
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
//main thing happens now
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion) {
// Not used, but you could upgrade the database with ALTER
// Scripts
}
}
public RemindersDbAdapter open() throws android.database.SQLException {
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}
public void close() {
mDbHelper.close();
}
// now we will create a method to create a new reminder
public long createReminder(String title, String body, String reminderDateTime) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_TITLE, title);
initialValues.put(KEY_BODY, body);
initialValues.put(KEY_DATE_TIME, reminderDateTime);
return mDb.insert(DATABASE_TABLE, null, initialValues);
}
//delete reminder
public boolean deleteReminder(long rowId) {
return
mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}
//to fetch all reminders
public Cursor fetchAllReminders() {
return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE,
KEY_BODY, KEY_DATE_TIME}, null, null, null, null, null);
}
//to fetch a particular reminder
public Cursor fetchReminder(long rowId) throws SQLException {
Cursor mCursor = mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,KEY_TITLE, KEY_BODY, KEY_DATE_TIME},
KEY_ROWID + "=" + rowId, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
//to update existing reminder
public boolean updateReminder(long rowId, String title, String body, String reminderDateTime) {
ContentValues args = new ContentValues();
args.put(KEY_TITLE, title);
args.put(KEY_BODY, body);
args.put(KEY_DATE_TIME, reminderDateTime);
return
mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
}
}
答案 0 :(得分:2)
根据logcat,它很清楚:
Caused by: android.database.sqlite.SQLiteException: near "tablereminders": syntax error (code 1): , while compiling: create tablereminders (_id integer primary key autoincrement, title text not null, body text not null,reminder_date_time text not null);
您的sql
无效。
小提示:当您的应用崩溃时,请查看logcat,找到最后的Caused by
,这是出错的代码/原因
答案 1 :(得分:0)
near "tablereminders": syntax error (code 1): , while compiling: create tablereminders ...
您需要在关键字table
和标识符reminders
之间使用空格。