0

I try this to add some data to "global variables". Class "Conection" without window in void

for (int in = 7; in < arAll.length; in++) {
        if (arAll[in].toString().endsWith("/")) {
            ((MyApplication) this.getApplication()).setPath(arAll[in]
                    .toString());
        } else {
            ((MyApplication) this.getApplication()).setFile(arAll[in]
                    .toString());
        }

MyApplication getter's and setter's:

private ArrayList<String> file = new ArrayList<String>();
private ArrayList<String> path = new ArrayList<String>();
private ArrayList<String> all = new ArrayList<String>();


public void removeAll() {
    this.file.clear();
    this.path.clear();
    this.all.clear();

}

public int len() {
    return this.all.size();
}

public String getStrbyId (int i) {
    return this.all.get(i).toString();
}

public ArrayList<String> getFile() {
    return this.file;
}

public void setFile(String file) {
    this.file.add(file);
    setAll(file);
    Log.v("",file);
}

public ArrayList<String> getPath() {
    return this.path;
}

public void setPath(String path) {
    this.path.add(path);
    setAll(path);
    Log.v("",path);
}
public ArrayList<String> getAll() {
    Log.v("",String.valueOf(len()));
    return this.all;
}

private void setAll(String all) {
    this.all.add(all);
}

In manifest <application android:name="MyApplication"

When I try to execute first void, I have an error.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • @user625565: can you pls post what's the error, you had got in your Logcat?? – Sankar Ganesh PMP Feb 21 '11 at 13:50
  • don't see where you initialize your 'MyApplication' with onCreate()... or post the entire code and the logcat entrys – Franco Feb 21 '11 at 13:55
  • See this thread http://stackoverflow.com/questions/708012/android-how-to-declare-global-variables/708317#708317 . You can try using `getApplicationContext()` – ccheneson Feb 21 '11 at 14:13
  • @user625565: Use `adb logcat`, DDMS, or the DDMS perspective in Eclipse to look at LogCat and examine the stack trace associated with your error. – CommonsWare Feb 21 '11 at 16:03

1 Answers1

1

The fact that you are using global variables via the Application context very likely that there is something improperly designed in your app. You should not need to use global variables like this at all, and it may be a potential source of your error (passing around the context and getting access to these global variables is sometimes necessary, but perhaps not in this situation). Instead what you should do is pass the necessary variables between components and use them within each component. (Activity, Service, ContentProvider.) So perhaps instead of trying to get this hacky thing working, you could rethink why you need global variables.

Kristopher Micinski
  • 7,572
  • 3
  • 29
  • 34