-2

ユーザーの検索入力に基づいてデータベース内の場所を検索する地図アプリケーションを Android で開発しています。データベースを検索し、データベース内のすべての場所の緯度を返すフィールドを使用して sqlite データベースにクエリを実行しようとしています。フィールドは主キーではありません。以下のコードです。助けが必要です。データベースを作成してアクセスするためのデータベース クラス。

public class Places {
private Context context;
private DbHelper dataHelper;
private SQLiteDatabase database;

//columns for places table
public static final String ROW_ID="_id";
public static final String P_NAME="placename";
public static final String G_NAME="genericname";
public static final String LONGITUDE="longitude";
public static final String LATITUDE="latitude";
public static final String CONTACT="contactnumbers";



private static final String DB_NAME="MAP.db";
private static final String DB_TABLE_PLACES="places";
private static final String DB_TABLE_EXAMS="exam";
private static final String DB_TABLE_SMS="message";
private static final int DB_VERSION= 1;

//Places.java のコンストラクタ

 public Places(Context c){
     this.context = c;
 }

//データベース ヘルパー クラス

private static class DbHelper extends SQLiteOpenHelper  {


 //constructor for DbHelper inner class

public DbHelper(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
    // TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    //creating table PLACES
    db.execSQL("CREATE TABLE "+DB_TABLE_PLACES+"("+ROW_ID+" PRIMARY KEY, " +
            ""+P_NAME+" TEXT NOT NULL, " +
            ""+G_NAME+" TEXT NOT NULL, " +
            ""+LONGITUDE+" TEXT NOT NULL, " +
            ""+LATITUDE+" TEXT NOT NULL, " +
            ""+CONTACT+" TEXT );");



    @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
    db.execSQL("DROP TABLE IF EXISTS "+DB_TABLE_PLACES);
    db.execSQL("DROP TABLE IF EXISTS "+DB_TABLE_EXAMS);
    db.execSQL("DROP TABLE IF EXISTS "+DB_TABLE_SMS);

    onCreate(db);
}

}

  public Places openDatabase() throws SQLiteException{
   dataHelper = new DbHelper(context);
   database= dataHelper.getWritableDatabase();
return this;

}

  public void closeDatabase(){
dataHelper.close();
database.close();

}

@SuppressWarnings("null")
 public List<String> getPlaceLatitude(String string) {
// TODO Auto-generated method stub

List<String> latitudes = null;
String[] columns= new String[]{ROW_ID,P_NAME,LATITUDE,LONGITUDE,G_NAME,CONTACT};

Cursor c = database.query(DB_TABLE_PLACES, columns,G_NAME+" = "+string, null, null, null, null);

int iLatitude = c.getColumnIndex(LATITUDE);
if (c !=null){
for(c.moveToFirst();!c.isAfterLast();c.moveToNext()){
    latitudes.add(c.getString(iLatitude));
}
}
return latitudes;}

データベースにクエリを実行するアクションをトリガーするメソッド。

public void searchDbAndOpenIntent(String search){
try{
        Places placeDb = new Places(this);
        placeDb.openDatabase();
        List<String> longitude1= placeDb.getPlaceLongitude(search);
                List<String> latitude1=placeDb.getPlaceLatitude(search);
        placeDb.closeDatabase();
}
                  catch (Exception e) {
            // TODO:handle exception                                    Toast.makeText(getBaseContext(),e.toString(),Toast.LENGTH_LONG).show();
        }
}




public void searchDbAndOpenIntent(String search){
try{
        Places placeDb = new Places(this);
        placeDb.openDatabase();
        List<String> longitude1= placeDb.getPlaceLongitude(search);
                List<String> latitude1=placeDb.getPlaceLatitude(search);
        placeDb.closeDatabase();



        }
                  catch (Exception e) {
            // TODO: handle exception


            Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG).show();
        }
}

場所の一般名に基づいて緯度を返したいのですが、generic_name 列を照会しようとすると、java.lang.NullPointerexception が返されます。その下に、データベースを照会するボタンが押されたときに取得するログ猫が表示されます

       05-14 20:52:37.202: D/ddm-heap(243): Got feature list request
       05-14 20:52:41.123: E/ActivityThread(243): Failed to find provider info for       com.google.settings
       05-14 20:52:41.132: E/ActivityThread(243): Failed to find provider info for   com.google.settings
       05-14 20:52:41.172: E/ActivityThread(243): Failed to find provider info for com.google.settings
       05-14 20:52:41.562: D/dalvikvm(243): GC freed 2007 objects / 134424 bytes in 125ms
       05-14 20:52:41.802: D/LocationManager(243): Constructor: service =   android.location.ILocationManager$Stub$Proxy@44e8c078
       05-14 20:52:42.012: I/MapActivity(243): Handling network change  notification:CONNECTED
       05-14 20:52:42.012: E/MapActivity(243): Couldn't get connection factory client
       05-14 20:52:42.453: D/dalvikvm(243): GC freed 4934 objects / 288736 bytes in 136ms
       05-14 20:52:48.852: D/dalvikvm(243): GC freed 10599 objects / 817800 bytes in  137ms
       05-14 20:53:56.712: W/KeyCharacterMap(243): No keyboard for id 0
       05-14 20:53:56.723: W/KeyCharacterMap(243): Using default keymap: /system /usr/keychars/qwerty.kcm.bin
       05-14 20:53:59.712: D/LocationManager(243): removeUpdates: listener =  my.project.success.KnustP2PActivity@44e93c70
       05-14 20:54:11.812: D/dalvikvm(243): GC freed 5940 objects / 725376 bytes in 98ms
       05-14 20:54:11.812: I/dalvikvm(243): Uncaught exception thrown by finalizer (will be discarded):
       05-14 20:54:11.812: I/dalvikvm(243): Ljava/lang/IllegalStateException;:  Finalizing cursor android.database.sqlite.SQLiteCursor@44f31670 on places that has not  been deactivated or closed
       05-14 20:54:11.812: I/dalvikvm(243):     at android.database.sqlite.SQLiteCursor.finalize(SQLiteCursor.java:596)
       05-14 20:54:11.822: I/dalvikvm(243):     at dalvik.system.NativeStart.run(Native Method)
       05-14 20:54:11.872: E/Database(243): Leak found
       05-14 20:54:11.872: E/Database(243): java.lang.IllegalStateException: /data/data/my.project.success/databases/MAP.db SQLiteDatabase created and never closed
       05-14 20:54:11.872: E/Database(243):     at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1694)
       05-14 20:54:11.872: E/Database(243):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:738)
       05-14 20:54:11.872: E/Database(243):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:760)
       05-14 20:54:11.872: E/Database(243):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:753)
       05-14 20:54:11.872: E/Database(243):     at android.app.ApplicationContext.openOrCreateDatabase(ApplicationContext.java:473)
       05-14 20:54:11.872: E/Database(243):     at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:193)
       05-14 20:54:11.872: E/Database(243):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98)
       05-14 20:54:11.872: E/Database(243):     at my.project.success.Places.openDatabase(Places.java:201)
       05-14 20:54:11.872: E/Database(243):     at my.project.success.SearchActivity.searchDbAndOpenIntent(SearchActivity.java:217)
       05-14 20:54:11.872: E/Database(243):     at my.project.success.SearchActivity.onClick(SearchActivity.java:151)
       05-14 20:54:11.872: E/Database(243):     at android.view.View.performClick(View.java:2364)
       05-14 20:54:11.872: E/Database(243):     at android.view.View.onTouchEvent(View.java:4179)
       05-14 20:54:11.872: E/Database(243):     at android.widget.TextView.onTouchEvent(TextView.java:6540)
       05-14 20:54:11.872: E/Database(243):     at android.view.View.dispatchTouchEvent(View.java:3709)
       05-14 20:54:11.872: E/Database(243):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
       05-14 20:54:11.872: E/Database(243):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
       05-14 20:54:11.872: E/Database(243):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
       05-14 20:54:11.872: E/Database(243):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
       05-14 20:54:11.872: E/Database(243):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
       05-14 20:54:11.872: E/Database(243):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
       05-14 20:54:11.872: E/Database(243):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
       05-14 20:54:11.872: E/Database(243):     at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
       05-14 20:54:11.872: E/Database(243):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
       05-14 20:54:11.872: E/Database(243):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
       05-14 20:54:11.872: E/Database(243):     at android.os.Handler.dispatchMessage(Handler.java:99)
       05-14 20:54:11.872: E/Database(243):     at android.os.Looper.loop(Looper.java:123)
       05-14 20:54:11.872: E/Database(243):     at     android.app.ActivityThread.main(ActivityThread.java:4363)
       05-14 20:54:11.872: E/Database(243):     at java.lang.reflect.Method.invokeNative(Native Method)
       05-14 20:54:11.872: E/Database(243):     at java.lang.reflect.Method.invoke(Method.java:521)
       05-14 20:54:11.872: E/Database(243):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
       05-14 20:54:11.872: E/Database(243):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
       05-14 20:54:11.872: E/Database(243):     at dalvik.system.NativeStart.main(Native Method)
       05-14 20:54:12.014: I/dalvikvm(243): Uncaught exception thrown by finalizer (will be discarded):
       05-14 20:54:12.022: I/dalvikvm(243): Ljava/lang/IllegalStateException;: Finalizing cursor  android.database.sqlite.SQLiteCursor@44f2f0b0 on places that has not been deactivated or closed
       05-14 20:54:12.022: I/dalvikvm(243):     at android.database.sqlite.SQLiteCursor.finalize(SQLiteCursor.java:596)
       05-14 20:54:12.032: I/dalvikvm(243):     at dalvik.system.NativeStart.run(Native Method)
       05-14 20:54:12.152: E/Database(243): Leak found
       05-14 20:54:12.152: E/Database(243): java.lang.IllegalStateException: /data/data/my.project.success/databases/MAP.db SQLiteDatabase created and never closed
       05-14 20:54:12.152: E/Database(243):     at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1694)
       05-14 20:54:12.152: E/Database(243):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:738)
       05-14 20:54:12.152: E/Database(243):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:760)
       05-14 20:54:12.152: E/Database(243):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:753)
       05-14 20:54:12.152: E/Database(243):     at android.app.ApplicationContext.openOrCreateDatabase(ApplicationContext.java:473)
       05-14 20:54:12.152: E/Database(243):     at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:193)
       05-14 20:54:12.152: E/Database(243):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98)
       05-14 20:54:12.152: E/Database(243):     at my.project.success.Places.openDatabase(Places.java:201)
       05-14 20:54:12.152: E/Database(243):     at my.project.success.SearchActivity.searchDbAndOpenIntent(SearchActivity.java:217)
       05-14 20:54:12.152: E/Database(243):     at my.project.success.SearchActivity.onClick(SearchActivity.java:79)
       05-14 20:54:12.152: E/Database(243):     at android.view.View.performClick(View.java:2364)
       05-14 20:54:12.152: E/Database(243):     at android.view.View.onTouchEvent(View.java:4179)
       05-14 20:54:12.152: E/Database(243):     at android.widget.TextView.onTouchEvent(TextView.java:6540)
       05-14 20:54:12.152: E/Database(243):     at android.view.View.dispatchTouchEvent(View.java:3709)
       05-14 20:54:12.152: E/Database(243):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
       05-14 20:54:12.152: E/Database(243):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
       05-14 20:54:12.152: E/Database(243):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
       05-14 20:54:12.152: E/Database(243):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
       05-14 20:54:12.152: E/Database(243):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
       05-14 20:54:12.152: E/Database(243):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
       05-14 20:54:12.152: E/Database(243):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
       05-14 20:54:12.152: E/Database(243):     at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
       05-14 20:54:12.152: E/Database(243):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
       05-14 20:54:12.152: E/Database(243):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
       05-14 20:54:12.152: E/Database(243):     at android.os.Handler.dispatchMessage(Handler.java:99)
       05-14 20:54:12.152: E/Database(243):     at android.os.Looper.loop(Looper.java:123)
       05-14 20:54:12.152: E/Database(243):     at android.app.ActivityThread.main(ActivityThread.java:4363)
       05-14 20:54:12.152: E/Database(243):     at java.lang.reflect.Method.invokeNative(Native Method)
       05-14 20:54:12.152: E/Database(243):     at java.lang.reflect.Method.invoke(Method.java:521)
       05-14 20:54:12.152: E/Database(243):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
       05-14 20:54:12.152: E/Database(243):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
       05-14 20:54:12.152: E/Database(243):     at dalvik.system.NativeStart.main(Native Method)
4

1 に答える 1

0

stringクエリでは、変数を一重引用符で囲む必要があります。

次のようになります。

G_NAME+" = '" + string + "'"

編集

最初に最初のレコードに移動せずにカーソルにアクセスしようとしているため、これは正しくありません。

int iLatitude = c.getColumnIndex(LATITUDE);
if (c !=null){
for(c.moveToFirst();!c.isAfterLast();c.moveToNext()){
    latitudes.add(c.getString(iLatitude));
}

これを試して:

c.moveToFirst();
int iLatitude = c.getColumnIndex(LATITUDE);
if (c !=null){
    while(c.isAfterLast == false){
        latitudes.add(c.getString(iLatitude));
        c.moveToNext();
    }
}

これはまた、主に読みやすさのために、ループを変更します。

于 2012-05-13T22:44:59.470 に答える