0
我嘗試用這種方式將一些數據添加到「全局變量」中。類 「連接如」 無窗空隙Android全局變量,getter,setter,錯誤,
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的吸氣的和setter的:
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);
}
在清單<application android:name="MyApplication"
當我嘗試執行第一孔洞,我有一個錯誤。
@ user625565:你可以請你發佈什麼錯誤,你有你的Logcat? –
看不到你在哪裏初始化你的'MyApplication'onCreate()...或發佈整個代碼和logcat entrust – Franco
看到這個線程http://stackoverflow.com/questions/708012/android-how-to- declare-global-variables/708317#708317。您可以嘗試使用'getApplicationContext()' – ccheneson