gpt4 book ai didi

java - MapActivity 中的数据库处理

转载 作者:行者123 更新时间:2023-11-29 02:35:09 25 4
gpt4 key购买 nike

我已经使用 Google Map API 创建了一个 apk。在它的 MainActivity 中它有两个按钮。首先在谷歌地图上显示我的位置,并将经纬度保存到 SQLite 数据库中。第二个按钮打开谷歌地图,其中包含从数据库加载的所有地点(这些是我之前检查位置的地方)。

我创建应用程序的方式是两个 MainActivity 按钮启动相同的 MapActivity,但具有不同的标志。在 onMapReady 方法中,我使用了 If 语句,我正在检查标志以了解正在调用哪个按钮。

public void onMapReady(GoogleMap googleMap) {
if ((ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED ||
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED)) {

mMap = googleMap;

if (getIntent().getFlags() == 1){
MarkerOptions options = new MarkerOptions();
LatLng llList;
ArrayList<LatLng> resultList = new ArrayList<LatLng>();

SQLiteDatabase mDatabase = openOrCreateDatabase("geoLoc", SQLiteDatabase.OPEN_READWRITE, null);
mDatabase.setLocale(Locale.getDefault());
String columns[] = {"lat", "lon"};
Cursor c = mDatabase.query("latlon", columns, null, null, null, null, "result DESC");
while (c.moveToNext())
{
Double lat = c.getDouble(c.getColumnIndex("lat"));
Double lng = c.getDouble(c.getColumnIndex("lon"));
try
{
llList = new LatLng(lat,lng);
resultList.add(llList);
}
catch (Exception e) {
}

}
for (LatLng point : resultList) {
options.position(point);
options.title(point.latitude + " " + point.longitude);
mMap.addMarker(options);
}
LatLng center= new LatLng(11,11);
mMap.moveCamera(CameraUpdateFactory.newLatLng(center));

}
else{
mDatabase = openOrCreateDatabase("geoLoc", SQLiteDatabase.CREATE_IF_NECESSARY, null);
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false));
double lat = location.getLatitude();
double lng = location.getLongitude();

// Saving to DB
ContentValues values = new ContentValues();
values.put("lat", lat);
values.put("lon", lng);
long newAuthorID = mDatabase.insert("latlon", null, values);

mMap.setMyLocationEnabled(true);
// Add a marker in Sydney and move the camera
LatLng place = new LatLng(lat,lng);
mMap.addMarker(new MarkerOptions().position(place).title("Your location"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(place));
}
}
}

在我的 MainActivity 的 onCreate() 方法中,我创建了这样的数据库

locale = Locale.getDefault();
SQLiteDatabase mDatabase = openOrCreateDatabase("geoLoc", SQLiteDatabase.CREATE_IF_NECESSARY, null);
mDatabase.setLocale(locale);
String CREATE_LL_TABLE = "CREATE TABLE IF NOT EXISTS latlon (lat DOUBLE, lon DOUBLE);";
mDatabase.execSQL(CREATE_LL_TABLE);

显示我的位置的部分工作正常,但是当我想检查我所有的位置时应用程序崩溃,并显示错误消息。

#################################################################
Error Code : 1 (SQLITE_ERROR)
Caused By : SQL(query) error or missing database.
(no such column: result (code 1): , while compiling: SELECT lat, lon FROM latlon ORDER BY result DESC)
#################################################################
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:996)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:561)
11at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1471)
at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1318)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1189)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1357)
at com.example.lukasz.aaaaa.MapsActivity.onMapReady(MapsActivity.java:75)
at com.google.android.gms.maps.SupportMapFragment$zza$1.zza(Unknown Source)
at com.google.android.gms.maps.internal.zzt$zza.onTransact(Unknown Source)
at android.os.Binder.transact(Binder.java:387)
at gl.b(:com.google.android.gms.DynamiteModulesB@11518436:20)
at com.google.android.gms.maps.internal.bf.a(:com.google.android.gms.DynamiteModulesB@11518436:5)
at com.google.maps.api.android.lib6.impl.bc.run(:com.google.android.gms.DynamiteModulesB@11518436:5)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
W ActivityManager: Force finishing activity com.example.lukasz.aaaaa/.MapsActivity

是不是数据库被锁了?

最佳答案

解决问题。我将选择查询更改为

Cursor c = mDatabase.rawQuery("select * from latlon",null);

在这两种情况下我都关闭了数据库连接。

关于java - MapActivity 中的数据库处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47512441/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com