-1

App can use database to insert items on emulator.But on my device,it can't use database.When it tries gives me an error like

2019-04-06 21:33:17.963 28735-28955/com.timucin.locationholder I/System.out: (HTTPLog)-Static: isSBSettingEnabled false

Here how my database is working

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();
                }
        }
  • Possible duplicate of [I/System.out: (HTTPLog)-Static: isSBSettingEnabled false?](https://stackoverflow.com/questions/37834292/i-system-out-httplog-static-issbsettingenabled-false) – Daniel Spiess Apr 06 '19 at 19:52
  • But I didn't see any solution there @DanielSpiess – user10230951 Apr 06 '19 at 19:54
  • There is a lot of possible solutions. I can't tell you more then there. It is a memory problem. If u want to have a better answer you may post a link to your github repository with the full code of your app. – Daniel Spiess Apr 06 '19 at 20:11
  • @DanielSpiess yes that makes sense I will share github link here – user10230951 Apr 06 '19 at 20:26
  • @DanielSpiess https://github.com/timucincicek/LocationHolder here it's.Thanks for your interest – user10230951 Apr 06 '19 at 20:46
  • In your the onCreate function of your MainActivity you make database operations in the MainThread. This could cause the problem. Try to use AppExecutors or AsynchTask instead. https://github.com/googlesamples/android-architecture-components/blob/master/PersistenceMigrationsSample/app/src/main/java/com/example/android/persistence/migrations/AppExecutors.java https://developer.android.com/reference/android/os/AsyncTask Tell me then if it works. – Daniel Spiess Apr 06 '19 at 21:02
  • @DanielSpiess I know AsynchTask.Used it before for API operations.But didn't get how can I use for database operations.Can you write it on my code? – user10230951 Apr 06 '19 at 22:42

1 Answers1

0

Ok i downloaded you code and run it in AndroidStudio. So a more modern way to avoid doing database operations in the main thread is do it with app executors.

Create a new class in your project that calls AppExecutors and looks like this:

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);
        }
    }
}

Now you can use this to run the database operations. For example in the MainActivitys onCreate function.

@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();
                }
            }
        });

And its done. You should do so with all your database operations. Actually i could not reproduce the error you get, so i dont know if it will resolve the problem. But anyway, running database operations in the main thread is bad practice and can produce other errors to. Try it ou, i hope it will help.

Daniel Spiess
  • 222
  • 1
  • 13