Java 原因:android.database.sqlite.SQLiteException:没有这样的表:food(代码1 sqlite_错误):,编译时:按id asc 2从食品订单中选择*

Java 原因:android.database.sqlite.SQLiteException:没有这样的表:food(代码1 sqlite_错误):,编译时:按id asc 2从食品订单中选择*,java,android,android-sqlite,Java,Android,Android Sqlite,当我编译程序时,我在Logcat中得到了这个错误 根据Logcat,这是导致错误的两个地方 一, 二, 以下是日志: 04-26 14:48:55.336 21103-21103/com.example.prudentmubasha.ainnaresto E/DATABASE OPERATIONS: Database created /Opened... 04-26 14:48:55.593 21103-21103/com.example.prudentmubasha.ainnaresto E/

当我编译程序时,我在Logcat中得到了这个错误

根据Logcat,这是导致错误的两个地方

一,

二,

以下是日志:

04-26 14:48:55.336 21103-21103/com.example.prudentmubasha.ainnaresto E/DATABASE OPERATIONS: Database created /Opened...
04-26 14:48:55.593 21103-21103/com.example.prudentmubasha.ainnaresto E/DATABASE OPERATIONS: Database created /Opened...
04-26 14:48:55.753 21103-21121/com.example.prudentmubasha.ainnaresto E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
    glUtilsParamSize: unknow param 0x000082da
04-26 14:48:58.743 21103-21103/com.example.prudentmubasha.ainnaresto E/SQLiteLog: (1) no such table: food
04-26 14:48:58.746 21103-21103/com.example.prudentmubasha.ainnaresto E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.prudentmubasha.ainnaresto, PID: 21103
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.prudentmubasha.ainnaresto/com.example.prudentmubasha.ainnaresto.MenuDrinksActivity}: android.database.sqlite.SQLiteException: no such table: food (code 1 SQLITE_ERROR): , while compiling: SELECT * FROM food order by id asc
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2892)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3027)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:101)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:73)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1786)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
     Caused by: android.database.sqlite.SQLiteException: no such table: food (code 1 SQLITE_ERROR): , while compiling: SELECT * FROM food order by id asc
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:901)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:512)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
        at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
        at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:46)
        at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1401)
        at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1340)
        at com.example.prudentmubasha.ainnaresto.MenuDrinksActivity.loadLocationData(MenuDrinksActivity.java:79)
        at com.example.prudentmubasha.ainnaresto.MenuDrinksActivity.onCreate(MenuDrinksActivity.java:32)
        at android.app.Activity.performCreate(Activity.java:7117)
        at android.app.Activity.performCreate(Activity.java:7108)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1262)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2867)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3027) 
04-28 21:19:42.719 1676-2777/系统\过程E/EGLCODECOMON:glUtilsParamSize:未知参数0x000082da glUtilsParamSize:未知参数0x000082da

这是发生错误的位置

enter c while (st.hasMoreElements()) {
        //Toast.makeText(getApplicationContext(), st.nextElement().toString().substring(0,1), Toast.LENGTH_LONG).show();
        test = st.nextElement().toString().substring(3,1);
        if(count>0)
            total+= Float.parseFloat(test);
        count++;
    }
下面是java类文件的全部活动

 pizza mApp;
EditText et_summary;
TextView tv_total;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_total);

    mApp=((pizza) getApplicationContext());
    et_summary = (EditText) findViewById(R.id.et_summary);
    tv_total = (TextView) findViewById(R.id.tv_total);
    //Toast.makeText(getApplicationContext(), "You have chosen the" + " " +  mApp.getGlobalVarValue(), Toast.LENGTH_LONG).show();
    et_summary.setText(mApp.getGlobalVarValue());
    String str = mApp.getGlobalVarValue();
    StringTokenizer st = new StringTokenizer(str,"$");
    String test="";
    float total=0;
    int count=0;
    while (st.hasMoreElements()) {
        //Toast.makeText(getApplicationContext(), st.nextElement().toString().substring(0,1), Toast.LENGTH_LONG).show();
        test = st.nextElement().toString().substring(3,1);
        if(count>0)
            total+= Float.parseFloat(test);
        count++;
    }
    et_summary.setText(mApp.getGlobalVarValue());
    tv_total.setText("Total :" + total+"");
    mApp.setGlobalClear();


}
public boolean isFloat( String input )
{
    try
    {
        Float.parseFloat( input );
        return true;
    }
    catch( Exception e)
    {
        return false;
    }
}

}

使用
locations=loadLocationData()时,很明显,食品表不存在位于
菜单中的oodactivity

您可以通过在调用
loadLocationData
方法之前创建表来避免此错误。e、 g:-

db=openOrCreateDatabase("Restaurants_DB", Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS food(id int, name text, price int);");
locations = loadLocationData();
addListenerOnButton();
initializeUI();

如果上面的问题解决了,您说它解决了,那么您应该将问题标记为已回答。如果你有后续问题,那么你应该问另一个问题。注意:错误是由以下原因引起的:
:java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:-2
您不使用SQLiteOpenHepler或Room persistence library有什么原因吗?建议使用SQLite的两种方法是什么?
04-26 14:48:55.336 21103-21103/com.example.prudentmubasha.ainnaresto E/DATABASE OPERATIONS: Database created /Opened...
04-26 14:48:55.593 21103-21103/com.example.prudentmubasha.ainnaresto E/DATABASE OPERATIONS: Database created /Opened...
04-26 14:48:55.753 21103-21121/com.example.prudentmubasha.ainnaresto E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
    glUtilsParamSize: unknow param 0x000082da
04-26 14:48:58.743 21103-21103/com.example.prudentmubasha.ainnaresto E/SQLiteLog: (1) no such table: food
04-26 14:48:58.746 21103-21103/com.example.prudentmubasha.ainnaresto E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.prudentmubasha.ainnaresto, PID: 21103
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.prudentmubasha.ainnaresto/com.example.prudentmubasha.ainnaresto.MenuDrinksActivity}: android.database.sqlite.SQLiteException: no such table: food (code 1 SQLITE_ERROR): , while compiling: SELECT * FROM food order by id asc
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2892)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3027)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:101)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:73)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1786)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
     Caused by: android.database.sqlite.SQLiteException: no such table: food (code 1 SQLITE_ERROR): , while compiling: SELECT * FROM food order by id asc
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:901)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:512)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
        at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
        at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:46)
        at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1401)
        at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1340)
        at com.example.prudentmubasha.ainnaresto.MenuDrinksActivity.loadLocationData(MenuDrinksActivity.java:79)
        at com.example.prudentmubasha.ainnaresto.MenuDrinksActivity.onCreate(MenuDrinksActivity.java:32)
        at android.app.Activity.performCreate(Activity.java:7117)
        at android.app.Activity.performCreate(Activity.java:7108)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1262)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2867)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3027) 
 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.prudentmubasha.ainnaresto/com.example.prudentmubasha.ainnaresto.TotalActivity}: java.lang.StringIndexOutOfBoundsException: String index out of range: -2
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2892)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3027)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:101)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:73)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1786)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6656)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
 Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: -2
    at java.lang.String.substring(String.java:2036)
    at com.example.prudentmubasha.ainnaresto.TotalActivity.onCreate(TotalActivity.java:33)
    at android.app.Activity.performCreate(Activity.java:7117)
    at android.app.Activity.performCreate(Activity.java:7108)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1262)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2867)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3027) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:101) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:73) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1786) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:164) 
    at android.app.ActivityThread.main(ActivityThread.java:6656) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823) 
enter c while (st.hasMoreElements()) {
        //Toast.makeText(getApplicationContext(), st.nextElement().toString().substring(0,1), Toast.LENGTH_LONG).show();
        test = st.nextElement().toString().substring(3,1);
        if(count>0)
            total+= Float.parseFloat(test);
        count++;
    }
 pizza mApp;
EditText et_summary;
TextView tv_total;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_total);

    mApp=((pizza) getApplicationContext());
    et_summary = (EditText) findViewById(R.id.et_summary);
    tv_total = (TextView) findViewById(R.id.tv_total);
    //Toast.makeText(getApplicationContext(), "You have chosen the" + " " +  mApp.getGlobalVarValue(), Toast.LENGTH_LONG).show();
    et_summary.setText(mApp.getGlobalVarValue());
    String str = mApp.getGlobalVarValue();
    StringTokenizer st = new StringTokenizer(str,"$");
    String test="";
    float total=0;
    int count=0;
    while (st.hasMoreElements()) {
        //Toast.makeText(getApplicationContext(), st.nextElement().toString().substring(0,1), Toast.LENGTH_LONG).show();
        test = st.nextElement().toString().substring(3,1);
        if(count>0)
            total+= Float.parseFloat(test);
        count++;
    }
    et_summary.setText(mApp.getGlobalVarValue());
    tv_total.setText("Total :" + total+"");
    mApp.setGlobalClear();


}
public boolean isFloat( String input )
{
    try
    {
        Float.parseFloat( input );
        return true;
    }
    catch( Exception e)
    {
        return false;
    }
}
db=openOrCreateDatabase("Restaurants_DB", Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS food(id int, name text, price int);");
locations = loadLocationData();
addListenerOnButton();
initializeUI();