In general, I created the database through the sqlitebrowoser program, added it to the assets folder, and when compiling this error occurs, if the database is removed from the assets folder, this error disappears.
Error log:
Information:Gradle tasks [clean, :app:generateDebugSources, :app:generateDebugAndroidTestSources, :app:mockableAndroidJar, :app:prepareDebugUnitTestDependencies, :app:compileDebugSources, :app:compileDebugAndroidTestSources, :app:compileDebugUnitTestSources] :clean UP-TO-DATE :app:clean :app:preBuild UP-TO-DATE :app:preDebugBuild UP-TO-DATE :app:checkDebugManifest :app:preReleaseBuild UP-TO-DATE :app:prepareComAndroidSupportAppcompatV72220Library :app:prepareComAndroidSupportMediarouterV72220Library :app:prepareComAndroidSupportSupportV42220Library :app:prepareComGoogleAndroidGmsPlayServices810Library :app:prepareComGoogleAndroidGmsPlayServicesAds810Library :app:prepareComGoogleAndroidGmsPlayServicesAnalytics810Library :app:prepareComGoogleAndroidGmsPlayServicesAppindexing810Library :app:prepareComGoogleAndroidGmsPlayServicesAppinvite810Library :app:prepareComGoogleAndroidGmsPlayServicesAppstate810Library :app:prepareComGoogleAndroidGmsPlayServicesBase810Library :app:prepareComGoogleAndroidGmsPlayServicesBasement810Library :app:prepareComGoogleAndroidGmsPlayServicesCast810Library :app:prepareComGoogleAndroidGmsPlayServicesDrive810Library :app:prepareComGoogleAndroidGmsPlayServicesFitness810Library :app:prepareComGoogleAndroidGmsPlayServicesGames810Library :app:prepareComGoogleAndroidGmsPlayServicesGcm810Library :app:prepareComGoogleAndroidGmsPlayServicesIdentity810Library :app:prepareComGoogleAndroidGmsPlayServicesLocation810Library :app:prepareComGoogleAndroidGmsPlayServicesMaps810Library :app:prepareComGoogleAndroidGmsPlayServicesMeasurement810Library :app:prepareComGoogleAndroidGmsPlayServicesNearby810Library :app:prepareComGoogleAndroidGmsPlayServicesPanorama810Library :app:prepareComGoogleAndroidGmsPlayServicesPlus810Library :app:prepareComGoogleAndroidGmsPlayServicesSafetynet810Library :app:prepareComGoogleAndroidGmsPlayServicesVision810Library :app:prepareComGoogleAndroidGmsPlayServicesWallet810Library :app:prepareComGoogleAndroidGmsPlayServicesWearable810Library :app:prepareDebugDependencies :app:compileDebugAidl :app:compileDebugRenderscript :app:generateDebugBuildConfig :app:mergeDebugShaders :app:compileDebugShaders :app:generateDebugAssets :app:mergeDebugAssets :app:generateDebugResValues UP-TO-DATE :app:generateDebugResources :app:mergeDebugResources D:\Android Project\Virtual\POKEMON\POKEMON\Pokemon\app\src\main\res\assets\coordinate.db Error:(1, 1) Error: Content is not allowed in prolog. :app:mergeDebugResources FAILED Error:Execution failed for task ':app:mergeDebugResources'. > D:\Android Project\Virtual\POKEMON\POKEMON\Pokemon\app\src\main\res\assets\coordinate.db:1:1: Error: Content is not allowed in prolog. Information:BUILD FAILED Information:Total time: 19.908 secs Information:2 errors Information:0 warnings Information:See complete output in console DB class:
public class DatabaseHelper extends SQLiteOpenHelper {
private static String DB_PATH = "/data/data/com.pokemongo.pokemon.database/databases/"; public static final String DB_NAME = "coordinate"; public static final String LATITUDE = "latitude"; public static final String LONGITUDE = "longitude"; private static final int SCHEMA = 1; // версия базы данных static final String TABLE = "users"; public SQLiteDatabase database; private Context myContext; public DatabaseHelper(Context context) { super(context, DB_NAME, null, SCHEMA); this.myContext=context; } @Override public void onCreate(SQLiteDatabase db) { } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } public void create_db() { { InputStream myInput = null; OutputStream myOutput = null; try { File file = new File(DB_PATH + DB_NAME); if (!file.exists()) { this.getReadableDatabase(); //получаем локальную бд как поток myInput = myContext.getAssets().open(DB_NAME); // Путь к новой бд String outFileName = DB_PATH + DB_NAME; // Открываем пустую бд myOutput = new FileOutputStream(outFileName); // побайтово копируем данные byte[] buffer = new byte[1024]; int length; while ((length = myInput.read(buffer)) > 0) { myOutput.write(buffer, 0, length); } myOutput.flush(); myOutput.close(); myInput.close(); } } catch (IOException ex) { } } } public void open() throws SQLException { String path = DB_PATH + DB_NAME; database = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READWRITE); } @Override public synchronized void close() { if (database != null) { database.close(); } super.close(); } }
UPD: changed in class activity method, vyleyate on the cursor:
private void randomCoordinate() { ArrayList<Double> latitudesAr = new ArrayList<Double>(); ArrayList<Double> longitudesAr = new ArrayList<Double>(); String queryLatitude = "SELECT * FROM latitude ORDER BY column DESC LIMIT 1"; String queryLongitude = "SELECT * FROM longitude ORDER BY column DESC LIMIT 1"; Cursor cLatitude = sqlHelper.database.rawQuery(queryLatitude, null); Cursor cLongitude = sqlHelper.database.rawQuery(queryLongitude, null); if(cLatitude != null) { while(cLatitude.moveToNext()){ latitudesAr.add(cLatitude.getDouble(cLatitude.getColumnIndex(DatabaseHelper.LATITUDE))); } } if(cLongitude != null) { while(cLongitude.moveToNext()){ longitudesAr.add(cLatitude.getDouble(cLatitude.getColumnIndex(DatabaseHelper.LONGITUDE))); } } //получаем рандомно выбранные индексы Random r = new Random(); int indexLatitude = r.nextInt(latitudesAr.size()); int indexLongitude = r.nextInt(longitudesAr.size()); //присваиваем переменным mMyLatitude = latitudesAr.get(indexLatitude); mMyLongitude = longitudesAr.get(indexLongitude); } I call in oncreate
can mistakenly create a cursor? Bza lies at: assets / database Updated class BD:
public class DatabaseHelper extends SQLiteOpenHelper { public static String DB_PATH = "/data/data/com.pokemongo.pokemon.database/databases/"; public static final String DB_NAME = "coordinate.db"; public static final String LATITUDE = "latitude"; public static final String LONGITUDE = "longitude"; private static final int SCHEMA = 1; // версия базы данных static final String TABLE = "users"; public SQLiteDatabase database; public final Context myContext; public DatabaseHelper(Context context) { super(context, DB_NAME, null, SCHEMA); this.myContext=context; } @Override public void onCreate(SQLiteDatabase db) { } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } public void create_db() throws IOException{ InputStream myInput = myContext.getAssets().open(DB_NAME); String outFileName = DB_PATH + DB_NAME; OutputStream myOutput = new FileOutputStream(outFileName); byte[] buffer = new byte[1024]; int length; while ((length = myInput.read(buffer))>0){ myOutput.write(buffer, 0, length); } myOutput.flush(); myOutput.close(); myInput.close(); } public void open() throws SQLException { String dbPath = DB_PATH + DB_NAME; database = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READWRITE); } @Override public synchronized void close() { if (database != null) { database.close(); } super.close(); } } т Mistake:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor android.database.sqlite.SQLiteDatabase.rawQuery(java.lang.String, java.lang.String[])' on a null object reference 