不幸的是,app_name(数据库)已经停止

时间:2014-02-13 18:00:47

标签: android

我使用简单的应用程序开发了sqlite数据库连接。但是,当我尝试运行我的应用程序时,它显示“不幸的是app已停止”。我的数据库包含所有插入,更新,删除操作。我也发布了所有代码和logcat错误。

databasehandler.java

package com.example.database;

import java.util.ArrayList;
import java.util.List;

import com.example.androidsqlite.Contact;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class DatabaseHandler extends Activity 
{
    // All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 1;

    // Database Name
    private static final String DATABASE_NAME = "contact";

    // Contacts table name
    private static final String TABLE_CONTACTS = "contact";

 // Contacts Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_NAME = "name";
    private static final String KEY_PH_NO = "phone_number";

    public DatabaseHandler(Context context)
    {
        super();
    }

 // Creating Tables
    public void onCreate(SQLiteDatabase db)
    {
        String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
                + KEY_PH_NO + " TEXT" + ")";
        db.execSQL(CREATE_CONTACTS_TABLE);
    }
 // Upgrading database
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
    {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);

        // Create tables again
        onCreate(db);
    }

 // Adding new contact
    public void addContact(Contact contact) 
    {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_NAME, contact.getName()); // Contact Name
        values.put(KEY_PH_NO, contact.getPhoneNumber()); // Contact Phone Number

        // Inserting Row
        db.insert(TABLE_CONTACTS, null, values);
        db.close(); // Closing database connection

    }

    private SQLiteDatabase getWritableDatabase() {
        // TODO Auto-generated method stub
        return null;
    }

    // Getting single contact
    public Contact getContact(int id) 
    {

            SQLiteDatabase db = this.getWritableDatabase();

            Cursor cursor = db.query(TABLE_CONTACTS, new String[] 
            { KEY_ID,
              KEY_NAME, 
              KEY_PH_NO 
            }, KEY_ID + "=?",
            new String[] { String.valueOf(id) }, null, null, null, null);


            if (cursor != null)
                cursor.moveToFirst();

            Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),
                    cursor.getString(1), cursor.getString(2));
            // return contact
            return contact;
    }



    // Getting All Contacts
    public List<Contact> getAllContacts()
    {

            List<Contact> contactList = new ArrayList<Contact>();
            // Select All Query
            String selectQuery = "SELECT  * FROM " + TABLE_CONTACTS;

            SQLiteDatabase db = this.getWritableDatabase();
            Cursor cursor = db.rawQuery(selectQuery, null);

            // looping through all rows and adding to list
            if (cursor.moveToFirst()) {
                do {
                    Contact contact = new Contact();
                    contact.setID(Integer.parseInt(cursor.getString(0)));
                    contact.setName(cursor.getString(1));
                    contact.setPhoneNumber(cursor.getString(2));
                    // Adding contact to list
                    contactList.add(contact);
                } while (cursor.moveToNext());
            }

            // return contact list
            return contactList;
        }


    // Getting contacts Count
    public int getContactsCount() 
    {

            String countQuery = "SELECT  * FROM " + TABLE_CONTACTS;
            SQLiteDatabase db = this.getWritableDatabase();
            Cursor cursor = db.rawQuery(countQuery, null);
            cursor.close();

            // return count
            return cursor.getCount();
      }

    // Updating single contact
    public int updateContact(Contact contact)
    {

            SQLiteDatabase db = this.getWritableDatabase();

            ContentValues values = new ContentValues();
            values.put(KEY_NAME, contact.getName());
            values.put(KEY_PH_NO, contact.getPhoneNumber());

            // updating row
            return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?",
                    new String[] { String.valueOf(contact.getID()) });
        }


    // Deleting single contact
    public void deleteContact(Contact contact) 
    {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_CONTACTS, KEY_ID + " = ?",
                new String[] { String.valueOf(contact.getID()) });
        db.close();
    }




}

logcat错误

02-13 12:51:59.280: D/Insert:(793): Inserting ..
02-13 12:51:59.300: D/AndroidRuntime(793): Shutting down VM
02-13 12:51:59.300: W/dalvikvm(793): threadid=1: thread exiting with uncaught exception (group=0xb4ac0b90)
02-13 12:51:59.320: E/AndroidRuntime(793): FATAL EXCEPTION: main
02-13 12:51:59.320: E/AndroidRuntime(793): Process: com.example.database, PID: 793
02-13 12:51:59.320: E/AndroidRuntime(793): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.database/com.example.database.MainActivity}: java.lang.NullPointerException
02-13 12:51:59.320: E/AndroidRuntime(793):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
02-13 12:51:59.320: E/AndroidRuntime(793):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
02-13 12:51:59.320: E/AndroidRuntime(793):  at android.app.ActivityThread.access$700(ActivityThread.java:135)
02-13 12:51:59.320: E/AndroidRuntime(793):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
02-13 12:51:59.320: E/AndroidRuntime(793):  at android.os.Handler.dispatchMessage(Handler.java:102)
02-13 12:51:59.320: E/AndroidRuntime(793):  at android.os.Looper.loop(Looper.java:137)
02-13 12:51:59.320: E/AndroidRuntime(793):  at android.app.ActivityThread.main(ActivityThread.java:4998)
02-13 12:51:59.320: E/AndroidRuntime(793):  at java.lang.reflect.Method.invokeNative(Native Method)
02-13 12:51:59.320: E/AndroidRuntime(793):  at java.lang.reflect.Method.invoke(Method.java:515)
02-13 12:51:59.320: E/AndroidRuntime(793):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
02-13 12:51:59.320: E/AndroidRuntime(793):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
02-13 12:51:59.320: E/AndroidRuntime(793):  at dalvik.system.NativeStart.main(Native Method)
02-13 12:51:59.320: E/AndroidRuntime(793): Caused by: java.lang.NullPointerException
02-13 12:51:59.320: E/AndroidRuntime(793):  at com.example.database.DatabaseHandler.addContact(DatabaseHandler.java:64)
02-13 12:51:59.320: E/AndroidRuntime(793):  at com.example.database.MainActivity.onCreate(MainActivity.java:26)
02-13 12:51:59.320: E/AndroidRuntime(793):  at android.app.Activity.performCreate(Activity.java:5243)
02-13 12:51:59.320: E/AndroidRuntime(793):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-13 12:51:59.320: E/AndroidRuntime(793):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
02-13 12:51:59.320: E/AndroidRuntime(793):  ... 11 more
02-13 12:53:25.212: I/Process(793): Sending signal. PID: 793 SIG: 9
02-13 12:53:34.512: D/Insert:(1053): Inserting ..
02-13 12:53:34.512: D/AndroidRuntime(1053): Shutting down VM
02-13 12:53:34.522: W/dalvikvm(1053): threadid=1: thread exiting with uncaught exception (group=0xb4ac0b90)
02-13 12:53:34.522: E/AndroidRuntime(1053): FATAL EXCEPTION: main
02-13 12:53:34.522: E/AndroidRuntime(1053): Process: com.example.database, PID: 1053
02-13 12:53:34.522: E/AndroidRuntime(1053): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.database/com.example.database.MainActivity}: java.lang.NullPointerException
02-13 12:53:34.522: E/AndroidRuntime(1053):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
02-13 12:53:34.522: E/AndroidRuntime(1053):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
02-13 12:53:34.522: E/AndroidRuntime(1053):     at android.app.ActivityThread.access$700(ActivityThread.java:135)
02-13 12:53:34.522: E/AndroidRuntime(1053):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
02-13 12:53:34.522: E/AndroidRuntime(1053):     at android.os.Handler.dispatchMessage(Handler.java:102)
02-13 12:53:34.522: E/AndroidRuntime(1053):     at android.os.Looper.loop(Looper.java:137)
02-13 12:53:34.522: E/AndroidRuntime(1053):     at android.app.ActivityThread.main(ActivityThread.java:4998)
02-13 12:53:34.522: E/AndroidRuntime(1053):     at java.lang.reflect.Method.invokeNative(Native Method)
02-13 12:53:34.522: E/AndroidRuntime(1053):     at java.lang.reflect.Method.invoke(Method.java:515)
02-13 12:53:34.522: E/AndroidRuntime(1053):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
02-13 12:53:34.522: E/AndroidRuntime(1053):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
02-13 12:53:34.522: E/AndroidRuntime(1053):     at dalvik.system.NativeStart.main(Native Method)
02-13 12:53:34.522: E/AndroidRuntime(1053): Caused by: java.lang.NullPointerException
02-13 12:53:34.522: E/AndroidRuntime(1053):     at com.example.database.DatabaseHandler.addContact(DatabaseHandler.java:64)
02-13 12:53:34.522: E/AndroidRuntime(1053):     at com.example.database.MainActivity.onCreate(MainActivity.java:26)
02-13 12:53:34.522: E/AndroidRuntime(1053):     at android.app.Activity.performCreate(Activity.java:5243)
02-13 12:53:34.522: E/AndroidRuntime(1053):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-13 12:53:34.522: E/AndroidRuntime(1053):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
02-13 12:53:34.522: E/AndroidRuntime(1053):     ... 11 more
02-13 12:53:36.882: I/Process(1053): Sending signal. PID: 1053 SIG: 9

2 个答案:

答案 0 :(得分:1)

看一下这个字符串:

SQLiteDatabase db = this.getWritableDatabase();

您尚未实施getWritableDatabase()方法,这就是您的dbnull

的原因

编辑好的,您的DatabaseHandler写错了。它应该扩展SQLiteOpenHelper。在这种情况下,添加了getWritableDatabase函数的处理程序的基本示例如下:

public class DBWorker extends SQLiteOpenHelper {

    private static String DB_PATH = "/data/data/<your app>/databases/";
    private static String DB_NAME = "db.sqlite";
    private SQLiteDatabase myDataBase;
    private final Context myContext;

    public DBWorker(Context context) {
        super(context, DB_NAME, null, 1);
        this.myContext = context;
    }

    public void getWritableDatabase() throws SQLException {

        //Open the database
        String myPath = DB_PATH + DB_NAME;
        myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);

    }

    @Override
    public synchronized void close() {

        if(myDataBase != null)
            myDataBase.close();
        super.close();

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
       //do the stuff
    }

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

答案 1 :(得分:0)

您尚未初始化某些内容,而该内容正在导致崩溃。看到这个错误:

Caused by: java.lang.NullPointerException

浏览代码,看看哪个变量未初始化