如何修复java.lang.IllegalStateException

时间:2019-04-02 16:12:53

标签: java android sqlite android-sqlite

我目前正在进行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;
        }

我想知道是否有人可以指出我正确的方向?

此外,请随时提出对我的问题的任何改进-如上所述,其中很多对我来说都是新的,所以我可能遗漏了一些非常重要的材料。

1 个答案:

答案 0 :(得分:0)

好吧,事实证明,我把“ db.close();”在databaseHandler中的一些功能之后-通过删除所有这些功能,我解决了问题。如果您认为最好不在此问题上提出此问题,请告诉我,我将其删除。尽管感谢所有发表评论的人。