我是Android的初學者。我正在開發一個沒有任何驗證的簡單登錄頁面的應用程序。點擊登錄按鈕後,下一頁必須在微調器中顯示錶格。當我運行我的應用程序時,我得到一個StackOverflowError異常。請幫我解決。Android SQLite - > StackOverflowError
display.java
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);
}
}
MyDBHandler
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);
}
}
錯誤
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.StackOverflowError
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:0)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.java:20)
at com.example.arjun.intentlogin.MyDBHandler.<init>(MyDBHandler.jav
d/dalvikvm:GC_CONCURRENT釋放604K,27%的遊離3233K/4423K,暫停12ms + 1ms,共計80次MS 從目標VM斷開,地址: '本地主機:8600',運輸: '插座'
嘿,,,, StackOverFlowError的原因是過深或無限遞歸。 用適當的日誌檢查你的代碼。 – GvSharma
哪一行做堆棧轉儲指向? –
因爲'private MyDBHandler dbhelper = new MyDBHandler(null,null,null,1);'當您使用public MyDBHandler(Context context,String name,SQLiteDatabase.CursorFactory factory,int version)創建一個對象時調用{super ,DATABASE_NAME,工廠,DATABASE_VERSION); }'它調用變量init調用構造函數調用變量init調用構造函數 – EpicPandaForce