Android SQLite - > StackOverflowError

时间:2016-11-14 08:17:15

标签: android android-studio android-sqlite android-spinner multiple-tables

我是Android的初学者。我正在开发一个具有简单登录页面但没有任何验证的应用程序。单击登录按钮后,下一页必须在微调器中显示表。当我运行我的应用程序时,我收到了一个StackOverflowError异常。请帮我解决。

display.java

var groupData = (data) => {
    let result = []

    for (var d in data) {
        // get date from data
        var date = d.game_date.split(' ');

        // add into new array and use key as the date
        if (!result[date]) {
            result[date] = [d]
        } else {

            // if key already existed, extend into group
            result[date].push(d)
        }
    }

    return result;
}

MyDBHandler

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

import java.util.ArrayList;

public class display extends AppCompatActivity {

    Spinner spinner;
    private MyDBHandler helper = new MyDBHandler(null, null, null, 1);

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

        spinner = (Spinner) findViewById(R.id.mySpinner);
        ArrayList<String> arrTblNames;
        arrTblNames = helper.gettablename();

        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line, arrTblNames);

        arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(arrayAdapter);

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getBaseContext(), parent.getItemIdAtPosition(position) + "", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is
        present.
                getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if(id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
  

错误

import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.Cursor;
import android.content.Context;

import java.util.ArrayList;

public class MyDBHandler extends SQLiteOpenHelper {
    ArrayList<String> arrTblName = new ArrayList<>();
    private MyDBHandler dbhelper = new MyDBHandler(null, null, null, 1);
    private SQLiteDatabase dbt = dbhelper.getWritableDatabase();

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "productDB.db";
    public static final String TABLE_PRODUCTS = "products";
    public static final String TABLE_PRODUCTSTWO = "productstwo";
    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_PRODUCTNAME = "productname";
    public static final String COLUMN_IDTWO = "_id";
    public static final String COLUMN_PRODUCTNAMETWO = "productname";

    //We need to pass database information along to superclass
    public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, DATABASE_NAME, factory, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String query = "CREATE TABLE " + TABLE_PRODUCTS + "(" +
                COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COLUMN_PRODUCTNAME + " TEXT " +
                ");";
        String querytwo = "CREATE TABLE " + TABLE_PRODUCTSTWO + "(" +
                COLUMN_IDTWO + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COLUMN_PRODUCTNAMETWO + " TEXT " +
                ");";
        db.execSQL(query);
        db.execSQL(querytwo);
    }

    public ArrayList<String> gettablename() {
        Cursor c = dbt.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null);

        if(c.moveToFirst()) {
            while(!c.isAfterLast()) {
                arrTblName.add(c.getString(c.getColumnIndex("name")));
                c.moveToNext();
            }
        }
        c.close();

        return arrTblName;
    }

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

D / dalvikvm:GC_CONCURRENT释放604K,27%免费3233K / 4423K,暂停12ms + 1ms,总计80ms 断开与目标VM的连接,地址:'localhost:8600',transport:'socket'

3 个答案:

答案 0 :(得分:2)

public class MyDBHandler extends SQLiteOpenHelper {
    private MyDBHandler dbhelper = new MyDBHandler(null, null, null, 1);

MyDBHandler课程中删除此字段。该类的每个实例都创建一个同一个类的新实例,这个递归将一直持续到堆栈耗尽为止。

答案 1 :(得分:0)

像这样改变display类:

public class display extends AppCompatActivity {

    Spinner spinner;
    private MyDBHandler helper = null; //HERE

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

        helper = new MyDBHandler(this, "dbName", null, 1); //HERE

...

答案 2 :(得分:0)

将MyDBHandler上的构造函数更改为:

private final Context myContext;

public MyDBHandler(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); this.myContext = context; }

显示类上的DataBaseHelper实例:

DataBaseHelper myDbHelpers = new DataBaseHelper(context);