我是编程新手,每次尝试从存储在SQL中的附加内容中获取数据时,我的应用程序都会崩溃。
public class MainActivity extends ListActivity {
public final static String ID_EXTRA="com.pixelcrunch._ID";
Cursor model=null;
CountdownHelper helper=null;
CountdownAdapter adapter = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
helper=new CountdownHelper(this);
model=helper.getAll();
startManagingCursor(model);
adapter=new CountdownAdapter(model);
setListAdapter(adapter);
@Override
public void onListItemClick(ListView list, View view,
int position, long id) {
Intent i=new Intent(MainActivity.this, Edit.class);
i.putExtra(ID_EXTRA, String.valueOf(id));
startActivity(i);
}
在我接收的活动中我有
public class Edit extends Activity {
CountdownHelper helper=null;
String countdownId=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit);
helper=new CountdownHelper(this);
countdownId=getIntent().getStringExtra(MainActivity.ID_EXTRA);
if (countdownId!=null) {
load();
}
}
private void load() {
Cursor c=helper.getById(countdownId);
c.moveToFirst();
mDescription.setText(helper.getDescription(c));
mDateDisplay.setText(helper.getDate(c));
mTimeDisplay.setText(helper.getTime(c));
c.close();
}
最后我的SQL
class CountdownHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME="countdown.db";
private static final int SCHEMA_VERSION=1;
public CountdownHelper(Context context) {
super(context, DATABASE_NAME, null, SCHEMA_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE countdowns (_id INTEGER PRIMARY KEY AUTOINCREMENT, description TEXT, date TEXT, time TEXT);");
}
public Cursor getAll() {
return(getReadableDatabase()
.rawQuery("SELECT _id, description, date, time FROM countdowns ORDER BY description",
null));
}
public Cursor getById(String id) {
String[] args={id};
return(getReadableDatabase()
.rawQuery("SELECT _id, description, date, time, FROM countdowns WHERE _ID=?",
args));
}
public void insert(String description, String date, String time) {
ContentValues cv=new ContentValues();
cv.put("description", description);
cv.put("date", date);
cv.put("time", time);
getWritableDatabase().insert("countdowns", "description", cv);
}
public void update(String id, String description, String date, String time) {
ContentValues cv=new ContentValues();
String[] args={id};
cv.put("description", description);
cv.put("date", date);
cv.put("time", time);
getWritableDatabase().update("countdowns", cv, "_ID=?",
args);
}
public String getDescription(Cursor c) {
return(c.getString(1));
}
public String getDate(Cursor c) {
return(c.getString(2));
}
public String getTime(Cursor c) {
return(c.getString(3));
}
对于长代码很抱歉,我真的无法确定我的问题所在。我知道如果我在load()下删除代码并用toast替换它就可以了。由于我缺乏经验和自学,任何批评都会受到高度赞赏。谢谢 logcat的:
02-14 19:33:19.798: V/InputMethodManager(23673): focusIn: android.widget.ListView@40eab1e8
02-14 19:33:19.798: V/InputMethodManager(23673): onWindowFocus: android.widget.ListView@40eab1e8 softInputMode=272 first=true flags=#8010100
02-14 19:33:19.798: V/InputMethodManager(23673): Has been inactive! Starting fresh
02-14 19:33:19.798: V/InputMethodManager(23673): focusIn: android.widget.ListView@40eab1e8
02-14 19:33:19.798: V/InputMethodManager(23673): checkFocus: view=android.widget.ListView@40eab1e8 next=android.widget.ListView@40eab1e8 restart=true
02-14 19:33:19.798: V/InputMethodManager(23673): Starting input: view=android.widget.ListView@40eab1e8
02-14 19:33:19.798: V/InputMethodManager(23673): Starting input: tba=android.view.inputmethod.EditorInfo@423b3230 ic=null
02-14 19:33:19.798: V/InputMethodManager(23673): START INPUT: android.widget.ListView@40eab1e8 ic=null tba=android.view.inputmethod.EditorInfo@423b3230 initial=true
02-14 19:33:19.803: V/InputMethodManager(23673): Starting input: Bind result=InputBindResult{com.android.internal.view.IInputMethodSession$Stub$Proxy@423b3dd0 com.android.inputmethod.latin/.LatinIME #1180}
02-14 19:33:25.483: W/ResourceType(23673): No package identifier when getting name for resource number 0x00000000
02-14 19:33:25.488: W/ResourceType(23673): No package identifier when getting name for resource number 0x00000000
02-14 19:33:25.503: W/ResourceType(23673): No package identifier when getting name for resource number 0x00000000
02-14 19:33:25.503: W/ResourceType(23673): No package identifier when getting name for resource number 0x00000000
02-14 19:33:25.533: I/SqliteDatabaseCpp(23673): sqlite returned: error code = 1, msg = near "FROM": syntax error, db=/data/data/com.pixelcrunch.crunchtime/databases/countdown.db
02-14 19:33:25.533: D/AndroidRuntime(23673): Shutting down VM
02-14 19:33:25.533: W/dalvikvm(23673): threadid=1: thread exiting with uncaught exception (group=0x40c381f8)
02-14 19:33:25.538: E/AndroidRuntime(23673): FATAL EXCEPTION: main
02-14 19:33:25.538: E/AndroidRuntime(23673): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pixelcrunch.crunchtime/com.pixelcrunch.crunchtime.Edit}: android.database.sqlite.SQLiteException: near "FROM": syntax error: , while compiling: SELECT _id, description, date, time, FROM countdowns WHERE _ID=?
02-14 19:33:25.538: E/AndroidRuntime(23673): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1968)
02-14 19:33:25.538: E/AndroidRuntime(23673): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
02-14 19:33:25.538: E/AndroidRuntime(23673): at android.app.ActivityThread.access$600(ActivityThread.java:127)
02-14 19:33:25.538: E/AndroidRuntime(23673): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
02-14 19:33:25.538: E/AndroidRuntime(23673): at android.os.Handler.dispatchMessage(Handler.java:99)
02-14 19:33:25.538: E/AndroidRuntime(23673): at android.os.Looper.loop(Looper.java:137)
02-14 19:33:25.538: E/AndroidRuntime(23673): at android.app.ActivityThread.main(ActivityThread.java:4507)
02-14 19:33:25.538: E/AndroidRuntime(23673): at java.lang.reflect.Method.invokeNative(Native Method)
02-14 19:33:25.538: E/AndroidRuntime(23673): at java.lang.reflect.Method.invoke(Method.java:511)
02-14 19:33:25.538: E/AndroidRuntime(23673): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
02-14 19:33:25.538: E/AndroidRuntime(23673): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
02-14 19:33:25.538: E/AndroidRuntime(23673): at dalvik.system.NativeStart.main(Native Method)
02-14 19:33:25.538: E/AndroidRuntime(23673): Caused by: android.database.sqlite.SQLiteException: near "FROM": syntax error: , while compiling: SELECT _id, description, date, time, FROM countdowns WHERE _ID=?
02-14 19:33:25.538: E/AndroidRuntime(23673): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
02-14 19:33:25.538: E/AndroidRuntime(23673): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:68)
02-14 19:33:25.538: E/AndroidRuntime(23673): at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:143)
02-14 19:33:25.538: E/AndroidRuntime(23673): at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
02-14 19:33:25.538: E/AndroidRuntime(23673): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:127)
02-14 19:33:25.538: E/AndroidRuntime(23673): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:94)
02-14 19:33:25.538: E/AndroidRuntime(23673): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:53)
02-14 19:33:25.538: E/AndroidRuntime(23673): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
02-14 19:33:25.538: E/AndroidRuntime(23673): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1673)
02-14 19:33:25.538: E/AndroidRuntime(23673): at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1647)
02-14 19:33:25.538: E/AndroidRuntime(23673): at com.pixelcrunch.crunchtime.CountdownHelper.getById(CountdownHelper.java:33)
02-14 19:33:25.538: E/AndroidRuntime(23673): at com.pixelcrunch.crunchtime.Edit.load(Edit.java:78)
02-14 19:33:25.538: E/AndroidRuntime(23673): at com.pixelcrunch.crunchtime.Edit.onCreate(Edit.java:68)
02-14 19:33:25.538: E/AndroidRuntime(23673): at android.app.Activity.performCreate(Activity.java:4465)
02-14 19:33:25.538: E/AndroidRuntime(23673): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
02-14 19:33:25.538: E/AndroidRuntime(23673): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
02-14 19:33:25.538: E/AndroidRuntime(23673): ... 11 more
如果第一次失败:
02-14 19:33:25.483: W/ResourceType(23673): No package identifier when getting name for resource number 0x00000000
答案 0 :(得分:1)
从异常看起来
02-14 19:33:25.538: E/AndroidRuntime(23673): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pixelcrunch.crunchtime/com.pixelcrunch.crunchtime.Edit}: android.database.sqlite.SQLiteException: near "FROM": syntax error: , while compiling: SELECT _id, description, date, time, FROM countdowns WHERE _ID=?
行
.rawQuery("SELECT _id, description, date, time, FROM countdowns WHERE _ID=?",
应该是
.rawQuery("SELECT _id, description, date, time FROM countdowns WHERE _ID=?",
// ^removed comma