2015-04-12 75 views
0

我總是有我的SQLite數據庫的問題,任何人都可以幫助解決?Android的Eclipse無法讀取我的SQLite數據庫

這是我DatabaseHelper.java

private static final String  DB_NAME  = "msekolah"; 
private static final int  DB_VER  = 1; 

private static final String SD_DATA = "sd_data"; 
public static final String COL_ID = "_rowid"; 
public static final String COL_NAMA = "namasek"; 
public static final String COL_ALAMAT = "address"; 
public static final String COL_KELURAHAN = "kelu"; 
public static final String COL_LATI = "lat"; 
public static final String COL_LONG = "lon"; 

private static DatabaseHelper dbInstance; 
private static SQLiteDatabase db; 

private DatabaseHelper(Context context) 
{ 
    super(context, DB_NAME, null, DB_VER); 
} 

public static DatabaseHelper getInstance(Context context) 
{ 
    if (dbInstance == null) 
    { 
     dbInstance = new DatabaseHelper(context); 

    } 
    return dbInstance; 
} 

@Override 
public synchronized void close() 
{ 
    super.close(); 
    if (dbInstance != null) 
    { 
     dbInstance.close(); 
    } 
} 

public List<Kamus> getAllKamus() 
{ 
    List<Kamus> lisKamus = new ArrayList<Kamus>(); 

    Cursor cursor = db.query(SD_DATA, new String[] { 
      COL_ID, COL_NAMA, COL_ALAMAT, COL_KELURAHAN, COL_LATI, COL_LONG }, 
      COL_ID + "=" + "_rowid", 
      null, null, null, null); 
      if (cursor.getCount() >= 1) 
    { 
     cursor.moveToFirst(); 

     do 
     { 
      Kamus kamus = new Kamus(); 
      kamus.setLongitude(cursor.getString(cursor.getColumnIndexOrThrow(COL_LONG))); 
      kamus.setLatitude(cursor.getString(cursor.getColumnIndexOrThrow(COL_LATI))); 
      kamus.setKelurahan(cursor.getString(cursor.getColumnIndexOrThrow(COL_KELURAHAN))); 
      kamus.setAlamat(cursor.getString(cursor.getColumnIndexOrThrow(COL_ALAMAT))); 
      kamus.setNamaSekolah(cursor.getString(cursor.getColumnIndexOrThrow(COL_NAMA))); 
      lisKamus.add(kamus); 

     } while (cursor.moveToNext()); 
    } 
    return lisKamus; 

} 

public Cursor getBukuByJudul(String query) 
{ 
    Cursor cursor = null; 

    if (TextUtils.isEmpty(query)) 
    { 
     cursor = db.query(SD_DATA, new String[] { COL_ID, COL_ID, 
       COL_NAMA, COL_ALAMAT, COL_KELURAHAN, COL_LATI, COL_LONG }, null, null, null, null, COL_LONG); 
    } else 
    { 
     cursor = db.query(SD_DATA, new String[] { COL_ID, COL_NAMA, COL_ALAMAT, COL_KELURAHAN, COL_LATI, COL_LONG }, COL_LONG + " like '" + query + "%'", 
       null, null, null, null); 
    } 
    return cursor; 
} 

,這是我的MainActivity.java

@Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     lv = (ListView) findViewById(R.id.lv_data); 
     lv.setEmptyView(findViewById(R.id.empty)); 
     search = (EditText) findViewById(R.id.search); 

     dbHelper = DatabaseHelper.getInstance(this); 

     setData(); 

     search.addTextChangedListener(this); 
     lv.setOnItemClickListener(this); 
    } 

    private void setData() 
    { 
     listKamus = dbHelper.getAllKamus(); 

     adapter = new ArrayAdapter<Kamus>(this, 
       android.R.layout.simple_expandable_list_item_1, listKamus); 
     lv.setAdapter(adapter); 

    } 

    @Override 
    public void afterTextChanged(Editable arg0) 
    { 
    } 

    @Override 
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, 
      int arg3) 
    { 
    } 

    @Override 
    public void onTextChanged(CharSequence s, int arg1, int arg2, int arg3) 
    { 
     adapter.getFilter().filter(s.toString()); 
    } 

    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, 
      long id) 
    { 
     // TODO Auto-generated method stub 
     Bundle b = new Bundle(); 
     b.putString("lon", adapter.getItem(position).getLongitude()); 
     b.putString("lat", adapter.getItem(position).getLatitude()); 
     b.putString("kelu", adapter.getItem(position).getKelurahan()); 
     b.putString("address", adapter.getItem(position).getAlamat()); 
     b.putString("namasek", adapter.getItem(position).getNamaSekolah()); 

     Intent i = new Intent(this, ArtiActivity.class); 
     i.putExtras(b); 
     startActivity(i); 

    } 

} 

我已經厭倦了找上因爲模擬器的logcat的 問題無法讀取我的數據庫中的SQLite資產 請幫忙..

04-12 18:54:45.216:E/AndroidRuntime(275):致命例外:main 04-12 18:54:45.216:E/AndroidRuntime(275):java.lang.RuntimeException: 無法啓動活動 ComponentInfo {id.nuhun.lbssekolah/id.nuhun.lbssekolah.MainActivity}: java.lang.NullPointerException 04-12 18:54:45.216: E/AndroidRuntime(275):at android.app.ActivityThread.performLaunchActivity( ActivityThread.java:2663) 04-12 18:54:45.216:E/AndroidRuntime(275):at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 04-12 18:54:45.216:E/AndroidRuntime(275):at android.app.ActivityThread.access $ 2300(ActivityThread.java:125)04-12 18:54:45.216:E/Android運行時(275):在 android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2033) 04-12 18:54:45.216:E/AndroidRuntime(275):at android.os.Handler.dispatchMessage( Handler.java:99)04-12 18:54:45.216:E/AndroidRuntime(275):at android.os.Looper.loop(Looper.java:123)04-12 18:54:45.216: E/AndroidRuntime(275):at android.app.ActivityThread.main(ActivityThread.java:4627)04-12 18:54:45.216:E/AndroidRuntime(275):at java.lang.reflect.Method.invokeNative (Native Method)04-12 18:54:45.216:E/AndroidRuntime(275):at java.lang.reflect.Method.invoke(Method.java:521)04-12 18:54:45.216: E/AndroidRuntime(275):在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868) 04-12 18:54:45.216:E/AndroidRuntime(275):at com.android.internal.os.ZygoteInit。 main(ZygoteInit.java:626)04-12 18:54:45.216:E/AndroidRuntime(275):at dalvik.system.NativeStart.main(Native Method)04-12 18:54:45.216: E/AndroidRuntime(275):導致:java.lang.NullPointerException 04-12 18:54:45.216:E/AndroidRuntime(275):at id.nuhun.lbssekolah.DatabaseHelper.getAllKamus(DatabaseHelper.java:58) 04 -12 18:54:45.216:E/AndroidRuntime(275):at id.nuhun.lbssekolah.MainActivity.setData(MainActivity.java:47)04-12 18:54:45.216:E/AndroidRuntime(275):在 id.nuhun.lbssekolah.MainActivity.onCreate(MainActivity.java:39)04-12 18:54:45.216:E/AndroidRuntime(275):at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 04-12 18:54:45.216:E/AndroidRuntime(275):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 04-12 18:54:45.216:E/AndroidRuntime(275) ...11多

+1

和錯誤是什麼? – Zielu

+0

@Zielu我如何上傳我的logcat評論? – Nuhun

+0

編輯您的帖子並在其中添加logcat。 – Iqbal

回答

0

在你getAllKamus(),你需要通話,例如query()它的方法之前初始化你db變量:

db = dbInstance.getReadableDatabase();