-1

Okay this error for the life of me I can't figure out, to the point where I don't even know what to google anymore. This is a fragment of my initial class. (You can view the whole thing here)

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i("Avariel", "I am running");

        RealmManager realmManager = new RealmManager();

        Log.i("Avariel", "I have passed RM");

In it I am trying to create a class of Realm Manager (You can view the whole class here). At this point I can even get the "I am running Realm Manager's on Create"to show. When I debug it just freezes.

public class RealmManager extends Activity {

    public static final String TAG = MainFragmentActivity.class.getName();
    private Realm realm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Log.i("Avariel REALM", "I am running Realm Manager's on Create");
        /**
         * https://realm.io/docs/java/latest/#getting-started
         * http://facebook.github.io/stetho/
         * https://github.com/uPhyca/stetho-realm
         * chrome://inspect/#devices
         */
       /* Stetho.initializeWithDefaults(this);
        Realm.init(this);
        Stetho.initialize(
                Stetho.newInitializerBuilder(this)
                        .enableDumpapp(Stetho.defaultDumperPluginsProvider(this))
                        .enableWebKitInspector(RealmInspectorModulesProvider.builder(this).build())
                        .build());

        realm = Realm.getDefaultInstance();
        realm.beginTransaction();
        Player player = realm.createObject(Player.class);
        player = player.create(01, "Gideon", "Barlock", "Orc", "Lawful Evil", "Fighter", "A wandering Warrior", 0, 30, 0, null, null, null, null, null, null, null, null, null, 0);
        realm.commitTransaction();
        Realm.init(this);

        RealmConfiguration config = new RealmConfiguration.Builder().build();
        Realm.setDefaultConfiguration(config);*/
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        realm.close();
    }

Meaning the log I see

05-27 22:38:13.477 32158-32158/com.example.gideonsassoon.avariel I/Avariel: I am running
05-27 22:38:13.559 32158-32158/com.example.gideonsassoon.avariel I/Avariel: I have passed RM

What have I done wrong here. You can view the full project here

Many thanks for your help,

Gideon Sassoon
  • 544
  • 1
  • 9
  • 29

2 Answers2

1

Never create an instance of an Activity yourself via the constructor.

Since RealmManager does not seem to be an actual activity (with a UI and such), remove extends Activity from your declaration of RealmManager. Then, you need to arrange to call methods on your RealmManager yourself, like any other Java object.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

"I am running Realm Manager's on Create"

No you aren't. You called new, which called the default constructor, which never calls onCreate.

Try the following to create a regular Java object that manages some Realm object

public class RealmManager  {

    private final Context context;
    private final Realm realm;

    public RealmManager(Context context) {
        Log.i("Avariel REALM", "I am running Realm Manager"); 
        context = context;
        realm = Realm.getDefaultInstance();
    }

Plus,

RealmManager realmManager = new RealmManager(this);

I believe all that other code belongs in an Application class, not any Activity

For example

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245