| "无法实例化活动" |

时间:2013-07-16 12:42:35

标签: android

我的项目的主要目的是在第一次启动应用程序时自动创建数据库。 我试图通过扩展SQLiteOpenHelper的类创建一个简单的数据库。以下是该类的代码:

public class DatabaseImplementation extends SQLiteOpenHelper 
{

    private static final String DATABASE_NAME = "TestDatabase.db";
    private static final int DATABASE_VERSION = 1;

    private static final String createDep = "CREATE TABLE tbl_Department("+ 
                                                    "_id INTEGER PRIMARY KEY AUTOINCREMENT," +
                                                    "department_name TEXT NOT NULL);";

    private static final String createEmp = "CREATE TABLE tbl_Employee(" +
                                                    "_id INTEGER PRIMARY KEY," +
                                                    "name TEXT NOT NULL," +
                                                    "age INTEGER," +
                                                    "department_id INTEGER, "+
                                                    "FOREIGN KEY(department_id)"+
                                                    "REFERENCES tbl_Department(_id));";




    public DatabaseImplementation(Context context) 
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) 
    {
        db.execSQL(createDep);
        db.execSQL(createEmp);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
    {
        //--
    }



}

请注意,我的layout / main.xml文件中没有声明任何布局,因为我认为没有必要用于此项目。

以下是我的主要活动代码:

public class TestingDatabaseMain extends Activity {

DatabaseImplementation db = new DatabaseImplementation(this);

SQLiteDatabase dbWrite = db.getWritableDatabase();

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

    insertRecords();


}

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

private void insertRecords(){
    ContentValues values = new ContentValues();
    values.put("_id", 1);
    values.put("department_name", "HR");

    long newrowID = dbWrite.insert("tbl_Department",  null, values);

}

}

当我在Nexus 7上运行应用程序时,会显示以下错误:

“不幸的是,测试数据库已停止。”

并且LogCat中的错误是:

07-16 14:14:14.240: E/AndroidRuntime(9979): FATAL EXCEPTION: main
07-16 14:14:14.240: E/AndroidRuntime(9979): java.lang.RuntimeException: Unable to     instantiate activity     ComponentInfo{com.example.testingdatabase/com.example.testingdatabase.TestingDatabaseMain}:     java.lang.NullPointerException
07-16 14:14:14.240: E/AndroidRuntime(9979):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
07-16 14:14:14.240: E/AndroidRuntime(9979):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-16 14:14:14.240: E/AndroidRuntime(9979):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-16 14:14:14.240: E/AndroidRuntime(9979):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-16 14:14:14.240: E/AndroidRuntime(9979):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-16 14:14:14.240: E/AndroidRuntime(9979):     at android.os.Looper.loop(Looper.java:137)
07-16 14:14:14.240: E/AndroidRuntime(9979):     at android.app.ActivityThread.main(ActivityThread.java:5041)
07-16 14:14:14.240: E/AndroidRuntime(9979):     at java.lang.reflect.Method.invokeNative(Native Method)
07-16 14:14:14.240: E/AndroidRuntime(9979):     at java.lang.reflect.Method.invoke(Method.java:511)
07-16 14:14:14.240: E/AndroidRuntime(9979):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-16 14:14:14.240: E/AndroidRuntime(9979):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-16 14:14:14.240: E/AndroidRuntime(9979):     at dalvik.system.NativeStart.main(Native Method)
07-16 14:14:14.240: E/AndroidRuntime(9979): Caused by: java.lang.NullPointerException
07-16 14:14:14.240: E/AndroidRuntime(9979):     at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:229)
07-16 14:14:14.240: E/AndroidRuntime(9979):     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
07-16 14:14:14.240: E/AndroidRuntime(9979):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
07-16 14:14:14.240: E/AndroidRuntime(9979):     at com.example.testingdatabase.TestingDatabaseMain.<init>(TestingDatabaseMain.java:13)
07-16 14:14:14.240: E/AndroidRuntime(9979):     at java.lang.Class.newInstanceImpl(Native Method)
07-16 14:14:14.240: E/AndroidRuntime(9979):     at java.lang.Class.newInstance(Class.java:1319)
07-16 14:14:14.240: E/AndroidRuntime(9979):     at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
07-16 14:14:14.240: E/AndroidRuntime(9979):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
07-16 14:14:14.240: E/AndroidRuntime(9979):     ... 11 more
07-16 14:16:47.030: W/ActivityThread(10487): Application com.example.testingdatabase is waiting for the debugger on port 8100...

我真的很感激这方面的帮助,即使有人可以将我链接到其他有相同问题的帖子。尝试寻找其他几个帖子并尝试按照提供的答案无效。

非常感谢。

编辑:我是Android开发的新手,所以不要骂我任何愚蠢的错误:)

3 个答案:

答案 0 :(得分:0)

在onCreate方法中初始化DatabaseImplementation和SQLiteDatabase。

答案 1 :(得分:0)

DatabaseImplementation db ;

SQLiteDatabase dbWrite;

@Override
protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);
     db  = new DatabaseImplementation(this);
     dbWrite = db.getWritableDatabase();
     insertRecords();


}

答案 2 :(得分:0)

在onCreate方法中初始化DatabaseImplementation和SQLiteDatabase。

像这样:

DatabaseImplementation db ;
SQLiteDatabase dbWrite;

@Override
protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);
     db  = new DatabaseImplementation(this);
     dbWrite = db.getWritableDatabase();
     insertRecords();


}