我为数据库创建了一个类,为表和多个活动类创建了多个类,当我在活动类中使用数据库类实例时,一切正常,但是同时在表类中使用数据库类实例并尽快运行应用程序当控件转到getwritabledatabase
时,我遇到异常,退出了应用程序,以下是所有3个类的代码。
数据库类。
public class Database extends SQLiteOpenHelper{
private static String dbname="Manager";
private static int dbversion=2;
SQLiteDatabase db;
public Database(Context context) {
super(context, dbname, null, dbversion);
// TODO Auto-generated constructor stub
db=this.getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
StockTable st=new StockTable();
db.execSQL(st.stocktable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
活动课
public class stockmanager extends Activity{
String getentry=null;
Database d=new Database(this);
StockTable st=new StockTable();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.stockmanager);
final Button AddStock=(Button) findViewById(R.id.button1);
final EditText entry=(EditText) findViewById(R.id.editText1);
final Button BroDetail=(Button) findViewById(R.id.button2);
AddStock.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
getentry=entry.getText().toString();
d.db.insert(st.tablename, null,st.insert());
}
});
}
}
表类
public class StockTable {
Context c1;
Database d1=new Database(c1);
final String tablename="StockTable";
private String column1="Stock_ID";
private String column2="StockName";
final String stocktable = "CREATE TABLE " + tablename +
" (" + column1+ " INTEGER PRIMARY KEY , " + column2 + " TEXT) ";
public ContentValues insert(){
String select="Select StockID from Stocktable";
d.db.execsql(select);
ContentValues cvi=new ContentValues();
for(int i=0;i<=sm.getentry.length();i++)
{
cvi.put(column1, 1);
cvi.put(column2,sm.getentry);
}
return cvi;
}
public void delete(){
}
首先,当控制权从活动类转到数据库类的
getwritabledatabase
然后执行时,但是当股票表类的控制权转到数据库类并执行getwritabledatabase
时,会发生异常,但是不幸的是,我无法在此处粘贴异常。谁能帮我解决这个问题?
Logcat
02-07 10:06:09.624: D/libEGL(10756): loaded /system/lib/egl/libEGL_genymotion.so
02-07 10:06:09.628: D/(10756): HostConnection::get() New Host Connection established 0xb87f5e00, tid 10756
02-07 10:06:09.720: D/libEGL(10756): loaded /system/lib/egl/libGLESv1_CM_genymotion.so
02-07 10:06:09.748: D/libEGL(10756): loaded /system/lib/egl/libGLESv2_genymotion.so
02-07 10:06:10.024: W/EGL_genymotion(10756): eglSurfaceAttrib not implemented
02-07 10:06:10.044: E/OpenGLRenderer(10756): Getting MAX_TEXTURE_SIZE from GradienCache
02-07 10:06:10.196: E/OpenGLRenderer(10756): Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
02-07 10:06:10.212: D/OpenGLRenderer(10756): Enabling debug mode 0
02-07 10:07:17.328: W/EGL_genymotion(10756): eglSurfaceAttrib not implemented
02-07 10:07:19.820: I/Choreographer(10756): Skipped 36 frames! The application may be doing too much work on its main thread.
02-07 10:07:22.556: W/EGL_genymotion(10756): eglSurfaceAttrib not implemented
02-07 10:07:22.788: I/Choreographer(10756): Skipped 44 frames! The application may be doing too much work on its main thread.
02-07 10:07:28.064: D/dalvikvm(10756): GC_FOR_ALLOC freed 105K, 1% free 16944K/17072K, paused 37ms, total 40ms
02-07 10:07:30.524: I/System.out(10756): asdfdw
02-07 10:07:30.576: D/AndroidRuntime(10756): Shutting down VM
02-07 10:07:30.612: W/dalvikvm(10756): threadid=1: thread exiting with uncaught exception (group=0xa4b6e648)
02-07 10:07:30.652: E/AndroidRuntime(10756): FATAL EXCEPTION: main
02-07 10:07:30.652: E/AndroidRuntime(10756): android.database.sqlite.SQLiteException: unknown error (code 0): Queries can be performed using SQLiteDatabase query or rawQuery methods only.
02-07 10:07:30.652: E/AndroidRuntime(10756): at android.database.sqlite.SQLiteConnection.nativeExecuteForChangedRowCount(Native Method)
02-07 10:07:30.652: E/AndroidRuntime(10756): at android.database.sqlite.SQLiteConnection.executeForChangedRowCount(SQLiteConnection.java:734)
02-07 10:07:30.652: E/AndroidRuntime(10756): at android.database.sqlite.SQLiteSession.executeForChangedRowCount(SQLiteSession.java:754)
02-07 10:07:30.652: E/AndroidRuntime(10756): at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:64)
02-07 10:07:30.652: E/AndroidRuntime(10756): at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1674)
02-07 10:07:30.652: E/AndroidRuntime(10756): at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1603)
02-07 10:07:30.652: E/AndroidRuntime(10756): at com.example.portfoliomanager.StockTable.insert(StockTable.java:43)
02-07 10:07:30.652: E/AndroidRuntime(10756): at com.example.portfoliomanager.stockmanager$1.onClick(stockmanager.java:36)
02-07 10:07:30.652: E/AndroidRuntime(10756): at android.view.View.performClick(View.java:4240)
02-07 10:07:30.652: E/AndroidRuntime(10756): at android.view.View$PerformClick.run(View.java:17721)
02-07 10:07:30.652: E/AndroidRuntime(10756): at android.os.Handler.handleCallback(Handler.java:730)
02-07 10:07:30.652: E/AndroidRuntime(10756): at android.os.Handler.dispatchMessage(Handler.java:92)
02-07 10:07:30.652: E/AndroidRuntime(10756): at android.os.Looper.loop(Looper.java:137)
02-07 10:07:30.652: E/AndroidRuntime(10756): at android.app.ActivityThread.main(ActivityThread.java:5103)
02-07 10:07:30.652: E/AndroidRuntime(10756): at java.lang.reflect.Method.invokeNative(Native Method)
02-07 10:07:30.652: E/AndroidRuntime(10756): at java.lang.reflect.Method.invoke(Method.java:525)
02-07 10:07:30.652: E/AndroidRuntime(10756): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
02-07 10:07:30.652: E/AndroidRuntime(10756): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-07 10:07:30.652: E/AndroidRuntime(10756): at dalvik.system.NativeStart.main(Native Method)
02-07 10:07:34.288: I/Process(10756): Sending signal. PID: 10756 SIG: 9
02-07 10:07:36.624: D/libEGL(10871): loaded /system/lib/egl/libEGL_genymotion.so
02-07 10:07:36.644: D/(10871): HostConnection::get() New Host Connection established 0xb87f5e70, tid 10871
02-07 10:07:36.784: D/libEGL(10871): loaded /system/lib/egl/libGLESv1_CM_genymotion.so
02-07 10:07:36.784: D/libEGL(10871): loaded /system/lib/egl/libGLESv2_genymotion.so
02-07 10:07:37.104: W/EGL_genymotion(10871): eglSurfaceAttrib not implemented
02-07 10:07:37.108: E/OpenGLRenderer(10871): Getting MAX_TEXTURE_SIZE from GradienCache
02-07 10:07:37.132: E/OpenGLRenderer(10871): Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
02-07 10:07:37.132: D/OpenGLRenderer(10871): Enabling debug mode 0
最佳答案
错误是因为您尝试使用空上下文访问数据库。因此,您需要在传递给数据库构造函数之前初始化c1。
我只是编辑您的StockTable.java试试这个
import android.content.ContentValues;
import android.content.Context;
public class StockTable {
Context c1;
Database d1;
final String tablename = "StockTable";
private String column1 = "Stock_ID";
private String column2 = "StockName";
final String stocktable = "CREATE TABLE " + tablename + " (" + column1 + " INTEGER PRIMARY KEY , " + column2 + " TEXT) ";
public StockTable(Context mContext){
c1= mContext;
d1= new Database(c1);
}
public ContentValues insert() {
ContentValues cvi = new ContentValues();
for (int i = 0; i <= sm.getentry.length(); i++) {
cvi.put(column1, 1);
cvi.put(column2, sm.getentry);
}
return cvi;
}
public void delete() {
}
}
如何使用?
public class stockmanager extends Activity {
String getentry = null;
Database d;
StockTable st;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.stockmanager);
d = new Database(this.getApplicationContext());
st = new StockTable(this.getApplicationContext());
final Button AddStock = (Button) findViewById(R.id.button1);
final EditText entry = (EditText) findViewById(R.id.editText1);
final Button BroDetail = (Button) findViewById(R.id.button2);
AddStock.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
getentry = entry.getText().toString();
d.db.insert(st.tablename, null, st.insert());
}
});
}
}
更新您的数据库类
public class Database extends SQLiteOpenHelper {
private static String dbname = "Manager";
private static int dbversion = 2;
SQLiteDatabase db;
private Context mContext;
public Database(Context context) {
super(context, dbname, null, dbversion);
// TODO Auto-generated constructor stub
mContext = context;
db = this.getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
StockTable st = new StockTable(mContext);
db.execSQL(st.stocktable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
关于android - 在Android中执行getwritabledatabase()时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21595300/