android studio Sqlite databse java.lang.IllegalStateException:递归调用getDatabase

时间:2017-07-02 15:53:23

标签: java android database sqlite

请帮我解决这个错误我无法找到解决方法。这是我第一次在android studio中使用SQlite数据库。我想从数据库中获取数据,所以我得到了这个错误  流程:com.raisse.guessplayers,PID:14400

 Process: com.raisse.guessplayers, PID: 14400
                                                                     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.raisse.guessplayers/com.raisse.guessplayers.MainActivity}: java.lang.IllegalStateException: getDatabase called recursively
                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3150)
                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3260)
                                                                         at android.app.ActivityThread.access$1000(ActivityThread.java:218)
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1734)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                         at android.os.Looper.loop(Looper.java:145)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:6934)
                                                                         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:1404)
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
                                                                      Caused by: java.lang.IllegalStateException: getDatabase called recursively
                                                                         at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:203)
                                                                         at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
                                                                         at com.raisse.guessplayers.Db_sqlit.insertData(Db_sqlit.java:39)
                                                                         at com.raisse.guessplayers.Db_sqlit.onCreate(Db_sqlit.java:29)
                                                                         at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
                                                                         at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
                                                                         at com.raisse.guessplayers.Db_sqlit.getAllData(Db_sqlit.java:50)
                                                                         at com.raisse.guessplayers.MainActivity.onCreate(MainActivity.java:45)
                                                                         at android.app.Activity.performCreate(Activity.java:6609)
                                                                         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1134)
                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3103)
                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3260) 
                                                                         at android.app.ActivityThread.access$1000(ActivityThread.java:218) 
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1734) 
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                         at android.os.Looper.loop(Looper.java:145) 
                                                                         at android.app.ActivityThread.main(ActivityThread.java:6934) 
public class Db_sqlit extends SQLiteOpenHelper{

    String TABLE_NAME = "tableballs";

    public final static String name = "db_data";

    public Db_sqlit(Context context) {
        super(context, name, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table "+TABLE_NAME+" (id INTEGER PRIMARY KEY AUTOINCREMENT, balls INTEGER)");

        insertData(1);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
       db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
       onCreate(db);
    }

    public boolean insertData(int balls){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("balls",balls);
        long result = db.insert("tableballs",null,contentValues);
        if (result == -1)
            return false;
        else
            return true;
    }

    public Cursor getAllData() {
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor res = db.rawQuery("select * from "+TABLE_NAME,null);
        return res;
    }
}
public class MainActivity extends AppCompatActivity {

    //Text
    TextView mSettings;
    TextView mBalls;

    //Button
    Button mBut_start;

    //Animation
    Animation anim;

    //Db_sqlite


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



        //TEXT
        mSettings = (TextView)findViewById(R.id.mSettings);
        mBalls = (TextView)findViewById(R.id.mText_Balls);

        //Sqlite
        Db_sqlit db_sqlit= new Db_sqlit(this);
        Cursor res = db_sqlit.getAllData();
        if(res.getCount()==0){
            Toast.makeText(this, "error", Toast.LENGTH_SHORT).show();
            return;
        }
        StringBuffer buffer = new StringBuffer();
        String id = buffer.append(res.getString(0)).toString();
        mBalls.setText(id);


        //Animation
        anim = AnimationUtils.loadAnimation(this,R.anim.scale_anim);

        //button
        mBut_start = (Button)findViewById(R.id.mBut_start);
        mBut_start.startAnimation(anim);
        mBut_start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(),LevelActicity.class);
                startActivity(intent);

            }
        });


    }
}

请帮帮我们

1 个答案:

答案 0 :(得分:3)

首先,您可能希望详细了解what recursion means in computer programming

然后,来自serial的{​​{1}}或onCreate(),您无法拨打onUpgrade()SQLiteOpenHelper,因为它们会触发另一个{{1}的来电}或getReadableDatabase()

此外,您不需要调用它们,因为您获得了getWritableDatabase(),因此您无需调用方法来检索它。

所以:

  • onCreate()替换为onUpgrade()

  • SQLiteDatabase替换为insertData(1);

  • insertData(db, 1);

  • 中移除public boolean insertData(int balls)