| "无法实例化活性QUOT; | [英] | "Unable to instantiate activity" |

查看:181
本文介绍了| "无法实例化活性QUOT; |的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目的主要目的是为了启动首次应用程序时自动创建的数据库。
我试图通过类扩展SQLiteOpenHelper创建一个简单的数据库。下面是这个类的code:

The main purpose of my project is to create a database automatically when starting up the application for the first time. I am trying to create a simple database through a class extending SQLiteOpenHelper. The following is the code of this class:

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) 
    {
        //--
    }



}

请并不是说我没有在我的布局/ main.xml中文件中声明,因为我相信没有必要为这个项目的目的任何布局。

Please not that I do not have any layout declared in my layout/main.xml file as I believe there is no need to for the purpose of this project.

以下是我的主要活动code:

The following is my main activity code:

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运行应用程序时,会显示以下错误:

When I run the application on my Nexus 7, the following error is displayed:

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

"Unfortunately, Testing Database has Stopped."

和在LogCat中的错误是:

and the error in the LogCat is :

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...

我真的会在这个问题上的AP preciate帮助,即使有人可以链接我有同样的问题的其他职位。试图寻找一些其他的职位,并试图以下提供了无果的答案。

I would really appreciate help on this matter, even if someone could link me to other posts having the same problem. Tried looking for a couple of other posts and tried following the answers that were provided to no avail.

感谢一大堆。

编辑:我是新android开发,所以不要骂我傻任何错误:)

Edited: I am new to android development so do not scold me for any silly errors :)

推荐答案

初始化DatabaseImplementation和SQLiteDatabase内onCreate方法。

Initialize DatabaseImplementation and SQLiteDatabase inside onCreate method.

这篇关于| &QUOT;无法实例化活性QUOT; |的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆