我为数据库创建了一个类,为表和多个活动创建了多个类classess当我在activity类中使用数据库类实例时,一切正常,但同时我在表类中使用数据库类实例并运行一旦控件转到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
答案 0 :(得分:1)
你应该替换这个
String select="Select StockID from Stocktable";
使用
String select="Select Stock_ID from Stocktable";
并初始化了您的Context C1
。创建将当前Context
传递到StockTable
的构造函数,如:
public StockTable(Context cxnt){
this.c1=cxnt;
d1=new Database(c1);
}
并添加此
StockTable st=new StockTable(this);
进入stockmanager
活动。
答案 1 :(得分:1)
错误是因为您尝试使用空上下文访问数据库。所以你需要在传递给Database构造函数之前初始化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
}
}
答案 2 :(得分:0)
StockID
&amp; Stock_ID
是两个不同的列名。
在创建表时,您使用Stock_ID
作为列,而在触发选择查询时,您使用StockID
作为列,这是您的错误。您的查询应该是
String select="Select Stock_ID from Stocktable";
并初始化了您的Context C1
。创建将当前Context
传递到StockTable
的构造函数,如:
public StockTable(Context cxnt){
this.c1=cxnt;
}
答案 3 :(得分:0)
当您实例化StockTable时,这两行是个问题:
Context c1;
Database d1=new Database(c1);
c1未初始化..没有创建数据库的上下文。将有效上下文传递给构造函数,或使用“set”方法,然后实例化数据库以供使用。像这样:
public StockTable(Context context) {
c1 = context;
d1=new Database(c1);
}
我会替换:
String select="Select StockID from Stocktable";
使用:
String select="Select "+ column1 + " from " + tablename;
答案 4 :(得分:0)
每当您使用数据库和帮助程序时,
Database database = new Database(this);
在onCreate()之前,不能将类实例用作Context。将数据库和数据库初始化推迟到onCreate()或更高版本。
请参考这些,以便更好地理解它:
SQlite Database: Unable to find context
nullPointerException for SQLiteOpenHelper.getWritableDatabase(), Android