应用程序。在DB中插入行时崩溃

时间:2012-02-26 19:34:19

标签: android sqlite

我有一个带有一个SQLite表的类只是为了测试一些东西,但我不知道应用程序崩溃的原因。

这是SQLite类:

    public class DBDemoActivity extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 2;
    private static final String DATABASE_NAME = "DBDemo.db";
    private static final String MP_TABLE_NAME = "Demo";

    DBDemoActivity (Context context) { 
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }// end of 

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL(" CREATE TABLE " + MP_TABLE_NAME + " ( " +
                BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                " name TEXT, " +
                " lat REAL, " +
                " lng REAL " +
                ");" );
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
     }

    public String getName(long id) {
        SQLiteDatabase db = this.getReadableDatabase();
        SQLiteCursor c = (SQLiteCursor) db.rawQuery("SELECT name FROM Demo WHERE "+
                                                    BaseColumns._ID+" = "+
                                                    Long.toString(id), null);
        countRows(c);
        c.moveToFirst();
        String r = c.getString(0);
        c.close();
        db.close();
        return r;       
    }

    public int countRows(SQLiteCursor c) {
        return c.getCount();
    }
}

这是活动:

    public class DemoData extends Activity{

    DBDemoActivity dbd;
    SQLiteDatabase db;
    ContentValues cv;

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

        dbd = new DBDemoActivity(this);
        db = dbd.getWritableDatabase();
        cv = new ContentValues();

        cv.put("name", "Amr");
        cv.put("lat", 10);
        cv.put("lng", 20);

        long newID = db.insert("Demo", null, cv);

        if (newID != -1){
            Toast.makeText(getBaseContext(),
                          "data saved ", 
                           Toast.LENGTH_SHORT).show();          
        }// end if*/        
    }   
}

这是LogCat:

02-26 20:28:55.530: E/AndroidRuntime(17124): FATAL EXCEPTION: main
02-26 20:28:55.530: E/AndroidRuntime(17124): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.androidbook.DBDemo/com.androidbook.DBDemo.DBDemoActivity}: java.lang.InstantiationException: com.androidbook.DBDemo.DBDemoActivity
02-26 20:28:55.530: E/AndroidRuntime(17124):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1573)
02-26 20:28:55.530: E/AndroidRuntime(17124):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
02-26 20:28:55.530: E/AndroidRuntime(17124):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-26 20:28:55.530: E/AndroidRuntime(17124):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
02-26 20:28:55.530: E/AndroidRuntime(17124):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-26 20:28:55.530: E/AndroidRuntime(17124):    at android.os.Looper.loop(Looper.java:123)
02-26 20:28:55.530: E/AndroidRuntime(17124):    at android.app.ActivityThread.main(ActivityThread.java:3691)
02-26 20:28:55.530: E/AndroidRuntime(17124):    at java.lang.reflect.Method.invokeNative(Native Method)
02-26 20:28:55.530: E/AndroidRuntime(17124):    at java.lang.reflect.Method.invoke(Method.java:507)
02-26 20:28:55.530: E/AndroidRuntime(17124):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
02-26 20:28:55.530: E/AndroidRuntime(17124):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
02-26 20:28:55.530: E/AndroidRuntime(17124):    at dalvik.system.NativeStart.main(Native Method)
02-26 20:28:55.530: E/AndroidRuntime(17124): Caused by: java.lang.InstantiationException: com.androidbook.DBDemo.DBDemoActivity
02-26 20:28:55.530: E/AndroidRuntime(17124):    at java.lang.Class.newInstanceImpl(Native Method)
02-26 20:28:55.530: E/AndroidRuntime(17124):    at java.lang.Class.newInstance(Class.java:1409)
02-26 20:28:55.530: E/AndroidRuntime(17124):    at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
02-26 20:28:55.530: E/AndroidRuntime(17124):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1565)
02-26 20:28:55.530: E/AndroidRuntime(17124):    ... 11 more

1 个答案:

答案 0 :(得分:0)

清单文件应包含应用程序中每个 活动的定义。
因此,您的清单应包含

    <activity
        android:name=".DemoData"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

不应包含

    <activity
        android:name=".DBDemoActivity">
    </activity>