从另一个类调用数据库对象时无法插入记录

时间:2014-01-27 15:22:44

标签: android android-sqlite android-context

首先,我是android和Java的新手。

我正在开发一个Android应用程序,我在database内为tablesdatabase编写单独的类,现在我想从不同的类中插入到表中创建数据库类和表类的对象。

我在日志文件中遇到错误,不明白它们到底意味着什么。

01-27 15:15:42.283: D/libEGL(17506): loaded /system/lib/egl/libEGL_genymotion.so
01-27 15:15:42.287: D/(17506): HostConnection::get() New Host Connection established 0xb891b390, tid 17506
01-27 15:15:42.363: D/libEGL(17506): loaded /system/lib/egl/libGLESv1_CM_genymotion.so
01-27 15:15:42.367: D/libEGL(17506): loaded /system/lib/egl/libGLESv2_genymotion.so
01-27 15:15:42.743: W/EGL_genymotion(17506): eglSurfaceAttrib not implemented
01-27 15:15:42.767: E/OpenGLRenderer(17506): Getting MAX_TEXTURE_SIZE from GradienCache
01-27 15:15:42.823: E/OpenGLRenderer(17506): Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
01-27 15:15:42.827: D/OpenGLRenderer(17506): Enabling debug mode 0
01-27 15:15:45.567: I/Choreographer(17506): Skipped 39 frames!  The application may be doing too much work on its main thread.
01-27 15:15:45.831: D/AndroidRuntime(17506): Shutting down VM
01-27 15:15:45.855: W/dalvikvm(17506): threadid=1: thread exiting with uncaught exception (group=0xa4b8c648)
01-27 15:15:45.943: E/AndroidRuntime(17506): FATAL EXCEPTION: main
01-27 15:15:45.943: E/AndroidRuntime(17506): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.portfoliomanager/com.example.portfoliomanager.stockmanager}: java.lang.NullPointerException
01-27 15:15:45.943: E/AndroidRuntime(17506):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
01-27 15:15:45.943: E/AndroidRuntime(17506):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
01-27 15:15:45.943: E/AndroidRuntime(17506):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-27 15:15:45.943: E/AndroidRuntime(17506):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
01-27 15:15:45.943: E/AndroidRuntime(17506):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-27 15:15:45.943: E/AndroidRuntime(17506):    at android.os.Looper.loop(Looper.java:137)
01-27 15:15:45.943: E/AndroidRuntime(17506):    at android.app.ActivityThread.main(ActivityThread.java:5103)
01-27 15:15:45.943: E/AndroidRuntime(17506):    at java.lang.reflect.Method.invokeNative(Native Method)
01-27 15:15:45.943: E/AndroidRuntime(17506):    at java.lang.reflect.Method.invoke(Method.java:525)
01-27 15:15:45.943: E/AndroidRuntime(17506):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-27 15:15:45.943: E/AndroidRuntime(17506):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-27 15:15:45.943: E/AndroidRuntime(17506):    at dalvik.system.NativeStart.main(Native Method)
01-27 15:15:45.943: E/AndroidRuntime(17506): Caused by: java.lang.NullPointerException
01-27 15:15:45.943: E/AndroidRuntime(17506):    at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:235)
01-27 15:15:45.943: E/AndroidRuntime(17506):    at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
01-27 15:15:45.943: E/AndroidRuntime(17506):    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
01-27 15:15:45.943: E/AndroidRuntime(17506):    at com.example.portfoliomanager.Database.<init>(Database.java:17)
01-27 15:15:45.943: E/AndroidRuntime(17506):    at com.example.portfoliomanager.stockmanager.<init>(stockmanager.java:14)
01-27 15:15:45.943: E/AndroidRuntime(17506):    at java.lang.Class.newInstanceImpl(Native Method)
01-27 15:15:45.943: E/AndroidRuntime(17506):    at java.lang.Class.newInstance(Class.java:1130)
01-27 15:15:45.943: E/AndroidRuntime(17506):    at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
01-27 15:15:45.943: E/AndroidRuntime(17506):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
01-27 15:15:45.943: E/AndroidRuntime(17506):    ... 11 more
0

以下是我的代码。

数据库类

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 {

final String tablename="StockTable";
private String column1="Stock_ID";
private String column2="StockName";

stockmanager sm=new stockmanager();

final String stocktable = "CREATE TABLE " + tablename + 
            " (" + column1+ " INTEGER PRIMARY KEY , " + column2 + " TEXT) ";

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(){


}

}       

请帮我解决这个问题,因为我被困了几个小时。

1 个答案:

答案 0 :(得分:0)

您可能还有其他问题,但现在就是这样:

Database d=new Database(this);

在“this”构建之前初始化“d”。将初始化程序移动到onCreate,如下所示:

d=new Database(this);

在您的StockTable类中,您在初始化之前也会引用您的活动(“stockmanager”不存在且没有引用);

您应该查看有关数据库创建和使用过程的几个教程。这是一个:http://hmkcode.com/android-simple-sqlite-database-tutorial/

和另一个:http://examples.javacodegeeks.com/android/core/database/sqlite/sqlitedatabase/android-sqlite-example/