-1

I am trying to initialize a class that calls another class that uses AsyncTask. I am using GetDataFromDB gDataFromDB = new GetDataFromDB() but that does not initialize the class, it just gives me access to any static methods in the class. So what do I do to get the onCreate method to run? I have tried using intent but keep getting an error because this is a static class

public class FacadeDataFromDB extends Activity {

static ArrayList<HashMap<String, String>> visitorsList;
private static FacadeDataFromDB dataFromDB;
static boolean accessDB = false;

private FacadeDataFromDB() {

}

public static void initInstance() {

}

public static FacadeDataFromDB getInstance() {
    if (dataFromDB == null) {
        // Create the instance
        dataFromDB = new FacadeDataFromDB();
    }
    return dataFromDB;
}

public static void setData() {
    if (!accessDB) {
        GetDataFromDB gDataFromDB = new GetDataFromDB();
        accessDB = true;
    }
    // visitorsList = gDataFromDB.returnInfoFromDB();
}

public static ArrayList<HashMap<String, String>> getVisitorForDay() {
    // TODO Auto-generated method stub
    setData();
    return visitorsList;
}

}

GetDataFromDB is the other class that I am calling. The current class is a static class and uses a singleton because I only want one initialization of the class the gets data from the db. If you have more questions or want me to post code let me know. Thanks

Aaron
  • 4,380
  • 19
  • 85
  • 141
  • Are the methods on `GetDataFromDB.java` set to `private`? – Emmanuel Feb 19 '14 at 15:35
  • No, the onCreate in GetDataFromDB is public. I can call it, but cannot figure out what to put in the parameters. – Aaron Feb 19 '14 at 15:37
  • 1
    You should never call an `Activity`'s life cycle method. The system calls these methods for you when it is the proper time to do so. – Emmanuel Feb 19 '14 at 15:38
  • 1
    `new FacadeDataFromDB` you can't do that. that's an activity. the system will instanciate it, not you. – njzk2 Feb 19 '14 at 15:41
  • How and when does the system choose to instantiate the activity? – Aaron Feb 19 '14 at 15:42
  • 2
    @Aaron: your oldest android related question is from mid 2011 and you still haven't collected the knowledge how an Activity is handled by Android? This makes me really speechless. Sorry to say that, but that is the basic of basic android knowledge! – WarrenFaith Feb 19 '14 at 15:45
  • is `GetDataFromDB` an Activity ? if not, it should not have an `onCreate` method. And if it is an Activity, you should not cretae it with `new`. – Pierre Rust Feb 19 '14 at 15:48
  • @PierreRust, yes GetDataFromDB is an activity. It is a static class and I keep getting errors when I use intent – Aaron Feb 19 '14 at 15:50

2 Answers2

2

It seems to me that your two classes FacadeDataFromDB GetDataFromDB should not inherit Activity

Activities are made for GUI and user-interaction (I don't see any in your example) and their life-cycle is managed by the framework : you never create them manually with new.

See the android tutorial : https://developer.android.com/guide/components/activities.html and Activity javadoc : https://developer.android.com/reference/android/app/Activity.html.

Pierre Rust
  • 2,474
  • 18
  • 15
0

I'm not sure that you completely understand the Android runtime. You should start Activities using Intent objects, not by creating them with the new keyword as you are. To ensure that your onCreate() method is called within your Activity, you could launch an explicit Intent from some other Activity/Context: Intent intent = new Intent(currentContext, FacadeDataFromDB.class);.

Also, when it comes to Activities, you shouldn't use private constructors. See this post for reasons why.

Community
  • 1
  • 1
Submersed
  • 8,810
  • 2
  • 30
  • 38
  • Android runtime is kind of a mystery to me. I am in a static class and it won't let me use Intent. I keep on getting cannot use this in a static context error – Aaron Feb 19 '14 at 15:48
  • Static inner classes don't have an implicit reference to their containing class - this is likely the issue you're running into. You should pass the inner class a Context when constructing it, store it as a WeakReference, and use that when you need a context in the inner class. It sounds like your might want to rethink your program's organization, though. – Submersed Feb 19 '14 at 15:53