2012-02-26 62 views
1

我只有一個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

重組你的logcat,並檢查您在manifiest文件中添加你的活動 – 2012-02-26 19:40:37

+0

我已經加入這兩項活動清單 – LetsamrIt 2012-02-26 19:51:21

+0

@ user1121443:如果您的意思是您的清單中有'DBDemoActivity'條目,那麼您不應該有。它是一個'SQLiteOpenHelper'而不是'Activity',所以不應該被命名爲一個,並且在清單文件中不應該有任何地方的任何條目。 – Squonk 2012-02-26 19:56:08

回答

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> 
相關問題