2014-04-09 38 views
0

我跟着這個教程: http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/ 創建數據庫使用SQLite數據庫瀏覽器,把數據庫文件中的「資產」文件夾等。查詢數據從現有的SQLite數據庫

然後,添加此方法來結束DataBaseHelper類的;

public ArrayList<market> getMarkets(String table) { 

    ArrayList<market> markets = new ArrayList<market>(); 
    market mrkt = new market(); 

    SQLiteDatabase db = this.getReadableDatabase(); 
    Cursor cursor = db.rawQuery("SELECT * FROM " + table, null); 

    if(cursor.moveToFirst()) { 
     do { 
      mrkt.market_id = cursor.getInt(cursor.getColumnIndex("marketid")); 
      mrkt.market_name = cursor.getString(cursor.getColumnIndex("name")); 
      mrkt.market_telno = cursor.getInt(cursor.getColumnIndex("telno")); 
      mrkt.market_location = cursor.getString(cursor.getColumnIndex("location")); 
      mrkt.market_hours = cursor.getString(cursor.getColumnIndex("hours")); 
      markets.add(mrkt); 
     }while(cursor.moveToNext()); 
    } 

    cursor.close(); 
    db.close(); 
    return markets; 
} 

,並試圖顯示在列表視圖市場,在主要活動課這些行:

dbh = new DataBaseHelper(getApplicationContext()); 
markets = new ArrayList<market>(); 
marketListView = (ListView) findViewById(R.id.listViewMarkets); 

markets = dbh.getMarkets("markets"); 



// Create The Adapter with passing ArrayList as 3rd parameter 
ArrayAdapter<market> arrayAdapter =  
new ArrayAdapter<market>(this,android.R.layout.simple_list_item_1, markets); 

// Set The Adapter 
marketListView.setAdapter(arrayAdapter); 

我的佈局xml文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<ListView 
    android:id="@+id/listViewMarkets" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:dividerHeight="0.1dp" 
    android:divider="#0000CC" 
    > 
</ListView> 

</LinearLayout> 

但是,我得到了錯誤,「unfotunately應用已停止「.. 我是新的android,有人可以幫助我嗎? 謝謝。

這裏的logcat:

04-09 16:32:00.125: W/dalvikvm(17103): threadid=1: thread exiting with uncaught exception (group=0x41685c80) 
04-09 16:32:00.130: E/AndroidRuntime(17103): FATAL EXCEPTION: main 
04-09 16:32:00.130: E/AndroidRuntime(17103): Process: com.example.deneme, PID: 17103 
04-09 16:32:00.130: E/AndroidRuntime(17103): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.deneme/com.example.deneme.MainActivity}: android.database.sqlite.SQLiteException: no such table: markets (code 1): , while compiling: SELECT * FROM markets 
04-09 16:32:00.130: E/AndroidRuntime(17103): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215) 
04-09 16:32:00.130: E/AndroidRuntime(17103): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2265) 
04-09 16:32:00.130: E/AndroidRuntime(17103): at android.app.ActivityThread.access$800(ActivityThread.java:145) 
04-09 16:32:00.130: E/AndroidRuntime(17103): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206) 
04-09 16:32:00.130: E/AndroidRuntime(17103): at android.os.Handler.dispatchMessage(Handler.java:102) 
04-09 16:32:00.130: E/AndroidRuntime(17103): at android.os.Looper.loop(Looper.java:136) 
04-09 16:32:00.130: E/AndroidRuntime(17103): at android.app.ActivityThread.main(ActivityThread.java:5081) 
04-09 16:32:00.130: E/AndroidRuntime(17103): at java.lang.reflect.Method.invokeNative(Native Method) 
04-09 16:32:00.130: E/AndroidRuntime(17103): at java.lang.reflect.Method.invoke(Method.java:515) 
04-09 16:32:00.130: E/AndroidRuntime(17103): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781) 
04-09 16:32:00.130: E/AndroidRuntime(17103): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
04-09 16:32:00.130: E/AndroidRuntime(17103): at dalvik.system.NativeStart.main(Native Method) 
04-09 16:32:00.130: E/AndroidRuntime(17103): Caused by: android.database.sqlite.SQLiteException: no such table: markets (code 1): , while compiling: SELECT * FROM markets 
04-09 16:32:00.130: E/AndroidRuntime(17103): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) 
04-09 16:32:00.130: E/AndroidRuntime(17103): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889) 
04-09 16:32:00.130: E/AndroidRuntime(17103): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500) 
04-09 16:32:00.130: E/AndroidRuntime(17103): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) 
04-09 16:32:00.130: E/AndroidRuntime(17103): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58) 
04-09 16:32:00.130: E/AndroidRuntime(17103): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37) 
04-09 16:32:00.130: E/AndroidRuntime(17103): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44) 
04-09 16:32:00.130: E/AndroidRuntime(17103): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314) 
04-09 16:32:00.130: E/AndroidRuntime(17103): at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253) 
04-09 16:32:00.130: E/AndroidRuntime(17103): at com.example.deneme.DataBaseHelper.getMarkets(DataBaseHelper.java:165) 
04-09 16:32:00.130: E/AndroidRuntime(17103): at com.example.deneme.MainActivity.onCreate(MainActivity.java:25) 
04-09 16:32:00.130: E/AndroidRuntime(17103): at android.app.Activity.performCreate(Activity.java:5231) 
04-09 16:32:00.130: E/AndroidRuntime(17103): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
04-09 16:32:00.130: E/AndroidRuntime(17103): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169) 
04-09 16:32:00.130: E/AndroidRuntime(17103): ... 11 more 
+1

發佈您的logcat –

+0

後logcat和是您的數據庫是?你有沒有在數據/數據文件夾中找到它? – Android

+0

logcat發佈.. – cmnl

回答

0
dbh = new DataBaseHelper(null); 

null不是有效的Context引用,SQLiteOpenHelper需要。替換爲this以將該活動用作上下文。這是NPE在getDatabaseLocked()中的原因。

+0

Yepp,我編輯:新的DataBaseHelper(getApplicationContext());並更新logcat – cmnl

+0

您沒有在創建表的sqlite open helper中發佈該部分,但是嘗試先卸載應用程序以防數據庫文件的舊版本出現。 – laalto

+0

我創建了與教程鏈接相同的方式。把創建的文件放在「資產」文件夾中...... Logcat說「沒有這樣的表」。我無法理解錯誤在哪裏:/ – cmnl