应用程序可以使用数据库在模拟器上插入项目。但是在我的设备上,它不能使用数据库。尝试运行时会出现类似
的错误2019-04-06 21:33:17.963 28735-28955/com.timucin.locationholder I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
这是我的数据库如何工作
try {
MapsActivity.database = openOrCreateDatabase("Places",MODE_PRIVATE,null);
Cursor cursor = MapsActivity.database.rawQuery("SELECT * FROM places",null);
int nameIx = cursor.getColumnIndex("name");
int latitudeIx = cursor.getColumnIndex("latitude");
int longitudeIx = cursor.getColumnIndex("longitude");
while (cursor.moveToNext()) {
nameFromDatabase = cursor.getString(nameIx);
String latitudeFromDatabase = cursor.getString(latitudeIx);
String longitudeFromDatabase = cursor.getString(longitudeIx);
image = cursor.getBlob(3);
// names.add(nameFromDatabase);
Double l1 = Double.parseDouble(latitudeFromDatabase);
Double l2 = Double.parseDouble(longitudeFromDatabase);
// System.out.println("coordinates:"+l1+","+l2);
locationFromDatabase = new LatLng(l1, l2);
names.add(nameFromDatabase);
locations.add(locationFromDatabase);
list.add(new Location(nameFromDatabase, image));
System.out.println(names);
}
System.out.println(names);
cursor.close();
} catch (Exception e) {
e.printStackTrace();
}
}
答案 0 :(得分:0)
好吧,我下载了代码并在AndroidStudio中运行。 因此,避免在主线程中执行数据库操作的更现代的方法是与应用程序执行程序一起执行。
在您的项目中创建一个新类,该类将调用AppExecutors,如下所示:
package com.timucin.locationholder;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.NonNull;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
public class AppExecutors {
// For Singleton instantiation
private static final Object LOCK = new Object();
private static AppExecutors sInstance;
private final Executor diskIO;
private final Executor mainThread;
private final Executor networkIO;
private AppExecutors(Executor diskIO, Executor networkIO, Executor mainThread) {
this.diskIO = diskIO;
this.networkIO = networkIO;
this.mainThread = mainThread;
}
public static AppExecutors getInstance() {
if (sInstance == null) {
synchronized (LOCK) {
sInstance = new AppExecutors(Executors.newSingleThreadExecutor(),
Executors.newFixedThreadPool(3),
new MainThreadExecutor());
}
}
return sInstance;
}
public Executor diskIO() {
return diskIO;
}
public Executor mainThread() {
return mainThread;
}
public Executor networkIO() {
return networkIO;
}
private static class MainThreadExecutor implements Executor {
private Handler mainThreadHandler = new Handler(Looper.getMainLooper());
@Override
public void execute(@NonNull Runnable command) {
mainThreadHandler.post(command);
}
}
}
现在您可以使用它来运行数据库操作。例如在MainActivitys onCreate函数中。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final GridView gridView= (GridView) findViewById(R.id.gridView);
list = new ArrayList<>();
//Database Operations
AppExecutors.getInstance().diskIO().execute(new Runnable() {
@Override
public void run() {
try {
MapsActivity.database = openOrCreateDatabase("Places",MODE_PRIVATE,null);
Cursor cursor = MapsActivity.database.rawQuery("SELECT * FROM places",null);
int nameIx = cursor.getColumnIndex("name");
int latitudeIx = cursor.getColumnIndex("latitude");
int longitudeIx = cursor.getColumnIndex("longitude");
while (cursor.moveToNext()) {
nameFromDatabase = cursor.getString(nameIx);
String latitudeFromDatabase = cursor.getString(latitudeIx);
String longitudeFromDatabase = cursor.getString(longitudeIx);
image = cursor.getBlob(3);
// names.add(nameFromDatabase);
Double l1 = Double.parseDouble(latitudeFromDatabase);
Double l2 = Double.parseDouble(longitudeFromDatabase);
// System.out.println("coordinates:"+l1+","+l2);
locationFromDatabase = new LatLng(l1, l2);
names.add(nameFromDatabase);
locations.add(locationFromDatabase);
list.add(new Location(nameFromDatabase, image));
adapter = new LocationListAdapter(getApplicationContext(), R.layout.location_items, list);
gridView.setAdapter(adapter);
System.out.println(names);
}
System.out.println(names);
cursor.close();
} catch (Exception e) {
e.printStackTrace();
}
}
});
完成。您应该对所有数据库操作执行此操作。 实际上,我无法重现您收到的错误,因此我不知道它是否可以解决问题。但是无论如何,在主线程中运行数据库操作是不好的做法,并且可能会产生其他错误。尝试一下,希望对您有所帮助。