我目前正在进行A Level计算机科学控制评估,使用Java和XML在Android Studio中制作Scout Manager应用程序 - 以前我都没有使用过。我现在正在研究一个n事件创建者。
我正在尝试创建一个记录,它将显示哪个传输已被选择用于哪个事件,但是我遇到了一个问题 - LogCat给了我以下错误:
04-02 15:25:42.663 18482-18482/com.example.atomi.scoutmanagerprototype E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.atomi.scoutmanagerprototype, PID: 18482
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
at android.view.View.performClick(View.java:4761)
at android.view.View$PerformClick.run(View.java:19767)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5310)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:4761)
at android.view.View$PerformClick.run(View.java:19767)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5310)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
Caused by: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/data/com.example.atomi.scoutmanagerprototype/databases/ScoutManager.db
at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1158)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1034)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1202)
at com.example.samogonovsky.scoutmanager.databaseHandler.getRandomTransport(databaseHandler.java:649)
at com.example.samogonovsky.scoutmanager.frmCreateEvent2.saveAndExit(frmCreateEvent2.java:528)
但是,我还没有关闭这两个地方的光标。
这是“databaseHandler”类中的代码:
//Select a random mode of transport
public Cursor getRandomTransport()
{
Cursor cursor=db.query(TABLE_TRANSPORT, null, null, null, null, null, "RANDOM()limit 1");
if (cursor!=null&&cursor.moveToFirst())
{
cursor.moveToFirst();
}
else
{
cursor=null;
}
return cursor;
}
这是“frmAddEvent2”类中的代码:
double TotalTransportCost;
if (providingtransport==true)
{
int peopletotransport=noOfPeople;
TotalTransportCost = 0;
do
{
int eventtransportid=createEventTransportId();
Cursor ModeOfTransport = DatabaseHandler.getRandomTransport();
if (ModeOfTransport.moveToFirst()) {
int transportid=ModeOfTransport.getInt(ModeOfTransport.getColumnIndex(
DatabaseHandler.COLUMN_TRID));
double PriceForHiring = ModeOfTransport.getDouble(ModeOfTransport.getColumnIndex(
DatabaseHandler.COLUMN_HCOST));
double PricePerMile = ModeOfTransport.getDouble(ModeOfTransport.getColumnIndex(
DatabaseHandler.COLUMN_MCOST));
int NoOfSeats = ModeOfTransport.getInt(ModeOfTransport.getColumnIndex(
DatabaseHandler.COLUMN_SEATNO));
double ModeCost = ((PricePerMile*distancefrombase)+PriceForHiring);
eventTransportDetails eventtransport = new eventTransportDetails(eventtransportid,
id, transportid);
DatabaseHandler.createEventTransport(eventtransport);
BigDecimal bigdecimalmodecost=new BigDecimal(ModeCost);
BigDecimal bigdecimalpropermodecost=((bigdecimalmodecost.setScale(2,bigdecimalmodecost.ROUND_DOWN)));
double propermodecost=bigdecimalpropermodecost.doubleValue();
TotalTransportCost = TotalTransportCost + propermodecost;
peopletotransport = (peopletotransport-NoOfSeats);
ModeOfTransport.close();
}
else
{
Toast.makeText(
context,
"Error - you have not added any lunches, therefore food " +
"cannot be added.",
Toast.LENGTH_SHORT
).show();
return;
}
}
while (peopletotransport>0);
}
else
{
TotalTransportCost=0;
}
我想知道是否有人能指出我正确的方向?
另外,请随意提出我的问题的任何改进 - 如上所述,这对我来说很新,所以我可能遗漏了一些非常重要的材料。
好的,事实证明,我把“db.close();”在databaseHandler中的一些函数之后 - 通过删除所有这些函数,我修复了问题。如果您认为这个问题最好不在网站上,请告诉我,我会删除它。谢谢所有评论的人。