我有一个按下按钮时触发的功能。这是我的ScanDetailActivity
课程:
Scan scan;
ScanDataSource datasource;
public void doDelete(View v) {
Log.i(LOGTAG, "delete clicked");
datasource.open();
Log.i(LOGTAG, "going to delete");
if (datasource.removeFromMyScans(scan)) {
Log.i(LOGTAG, "Item Deleted");
setResult(-1);
finish();
} else {
Log.i(LOGTAG, "item not deleted");
}
}
这适用于if语句。我在函数本身有一些日志信息,这是在我的
ScanDataSource.java
档案:
public boolean removeFromMyScans(Scan scan) {
Log.i(LOGTAG, "removeFromMyScans started");
String where = ScanDBOpenHelper.COLUMN_BID + "=" + scan.getId();
int result = database.delete(ScanDBOpenHelper.TABLE_BUYS, where, null);
return (result == 1);
}
我从未得到“removeFromMyScans started”信息。我查看了有关删除/删除行的其他帖子,我甚至没有达到这一点。我的所有错误消息都表明它在if (datasource.removeFromMyScans(scan)
行失败了。我是Android开发的新手,无法弄清楚我哪里出错了。我一直在学习Linda.com视频。
编辑
如果有帮助,以下是错误消息:
I/DATABASE(1985): Table has been created
I/DATABASE(1985): Table buys has column isbn
I/DATABASE(1985): Database opened scans
I/DATABASE(1985): Returned 0 rows in findAll
I/DATABASE(1985): Returned 32160 rows in findAll
I/DATABASE(1985): Database opened scans
I/DATABASE(1985): Returned 1 rows
I/DATABASE(1985): Scan added
I/DATABASE(1985): Returned 1 rows
I/DATABASE(1985): writeToParcel
I/DATABASE(1985): Database closed
I/DATABASE(1985): createFromParcel
I/DATABASE(1985): Parcel constructor
I/DATABASE(1985): delete clicked
D/AndroidRuntime(1985): Shutting down VM
W/dalvikvm(1985): threadid=1: thread exiting with uncaught exception (group=0xb0d3fb20)
E/AndroidRuntime(1985): FATAL EXCEPTION: main
E/AndroidRuntime(1985): Process: com.bookcellaronline.bookscanning, PID: 1985
E/AndroidRuntime(1985): java.lang.IllegalStateException: Could not execute method of the activity
E/AndroidRuntime(1985): at android.view.View$1.onClick(View.java:3823)
E/AndroidRuntime(1985): at android.view.View.performClick(View.java:4438)
E/AndroidRuntime(1985): at android.view.View$PerformClick.run(View.java:18422)
E/AndroidRuntime(1985): at android.os.Handler.handleCallback(Handler.java:733)
E/AndroidRuntime(1985): at android.os.Handler.dispatchMessage(Handler.java:95)
E/AndroidRuntime(1985): at android.os.Looper.loop(Looper.java:136)
E/AndroidRuntime(1985): at android.app.ActivityThread.main(ActivityThread.java:5017)
E/AndroidRuntime(1985): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(1985): at java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime(1985): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
E/AndroidRuntime(1985): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
E/AndroidRuntime(1985): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(1985): Caused by: java.lang.reflect.InvocationTargetException
E/AndroidRuntime(1985): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(1985): at java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime(1985): at android.view.View$1.onClick(View.java:3818)
E/AndroidRuntime(1985): ... 11 more
E/AndroidRuntime(1985): Caused by: java.lang.NullPointerException
E/AndroidRuntime(1985): at com.bookcellaronline.bookscanning.ScanDetailActivity.doDelete(ScanDetailActivity.java:62)
E/AndroidRuntime(1985): ... 14 more
I/DATABASE(2027): Database opened scans
I/DATABASE(2027): Returned 32160 rows in findAll
I/DATABASE(2027): Database opened scans
ScanDataSource类:
public class ScanDataSource {
public static final String LOGTAG = "DATABASE";
SQLiteOpenHelper dbhelper;
SQLiteDatabase database;
private static final String[] allColumns = {
ScanDBOpenHelper.COLUMN_ID, ScanDBOpenHelper.COLUMN_ISBN, ScanDBOpenHelper.COLUMN_TITLE,
ScanDBOpenHelper.COLUMN_AUTHOR, ScanDBOpenHelper.COLUMN_PRICE, ScanDBOpenHelper.COLUMN_IE,
ScanDBOpenHelper.COLUMN_LIMIT, ScanDBOpenHelper.COLUMN_NOTE, ScanDBOpenHelper.COLUMN_RANK,
ScanDBOpenHelper.COLUMN_SUPPLY, ScanDBOpenHelper.COLUMN_BILLING
};
public ScanDataSource(Context context) {
dbhelper = new ScanDBOpenHelper(context);
}
public void open() {
database = dbhelper.getWritableDatabase();
Log.i(LOGTAG, "Database opened scans");
}
public void close() {
Log.i(LOGTAG, "Database closed");
dbhelper.close();
}
private List<Scan> cursorToScanned(Cursor cursor) {
List<Scan> scans = new ArrayList<Scan>();
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
Scan scan = new Scan();
scan.setId(cursor.getLong(cursor.getColumnIndex(ScanDBOpenHelper.COLUMN_BID)));
scan.setIsbn(cursor.getString(cursor.getColumnIndex(ScanDBOpenHelper.COLUMN_BISBN)));
scan.setQuantity(cursor.getString(cursor.getColumnIndex(ScanDBOpenHelper.COLUMN_QUANTITY)));
scan.setPrice(cursor.getDouble(cursor.getColumnIndex(ScanDBOpenHelper.COLUMN_BPRICE)));
scan.setTitle(cursor.getString(cursor.getColumnIndex(ScanDBOpenHelper.COLUMN_TITLE)));
scan.setAuthor(cursor.getString(cursor.getColumnIndex(ScanDBOpenHelper.COLUMN_AUTHOR)));
scan.setIePrice(cursor.getDouble(cursor.getColumnIndex(ScanDBOpenHelper.COLUMN_IE)));
scan.setMaxLimit(cursor.getString(cursor.getColumnIndex(ScanDBOpenHelper.COLUMN_LIMIT)));
scan.setSupply(cursor.getString(cursor.getColumnIndex(ScanDBOpenHelper.COLUMN_SUPPLY)));
scans.add(scan);
}
}
return scans;
}
public boolean addToMyScans(Scan scan) {
ContentValues values = new ContentValues();
values.put(ScanDBOpenHelper.COLUMN_BISBN, scan.getIsbn());
values.put(ScanDBOpenHelper.COLUMN_QUANTITY, "1");
values.put(ScanDBOpenHelper.COLUMN_BPRICE, scan.getPrice());
long result = database.insert(ScanDBOpenHelper.TABLE_BUYS, null, values);
scan.setId(result);
return (result != -1);
}
public boolean removeFromMyScans(Scan scan) {
Log.i(LOGTAG, "removeFromMyScans started");
String where = ScanDBOpenHelper.COLUMN_BID + "=" + scan.getId();
int result = database.delete(ScanDBOpenHelper.TABLE_BUYS, where, null);
return (result == 1);
}
public boolean updateMyScans(Scan scan) {
Log.i(LOGTAG, "update started");
ContentValues values = new ContentValues();
values.put(ScanDBOpenHelper.COLUMN_BPRICE, scan.getPrice());
values.put(ScanDBOpenHelper.COLUMN_QUANTITY, scan.getQuantity());
String where = ScanDBOpenHelper.COLUMN_BID + "=" + scan.getId();
int result = database.update(ScanDBOpenHelper.TABLE_BUYS, values, where, null);
return (result == 1);
}
public List<Scan> findMyScans() {
String query = "SELECT buys_id,quantity, buyer_temp.price, scan_final.* "
+ " FROM buyer_temp inner join scan_final on scan_final.isbn = buyer_temp.isbn";
Cursor cursor = database.rawQuery(query, null);
Log.i(LOGTAG, "Returned " + cursor.getCount() + " rows");
List<Scan> scans = cursorToScanned(cursor);
return scans;
}
}
答案 0 :(得分:1)
根据日志和您的评论,例外的原因是datasource
方法null
为doDelete
。在代码中搜索您初始化datasource
字段的位置以及将其设置为null
的位置。您可能会在onCreate
方法中对其进行初始化,但在null
或onPause
中设置为onStop
。 (这不起作用,因为在活动停止后没有再次调用onCreate
但是没有销毁。请查看Activity lifecycle以获取详细信息。)