Because many instances of that object (static variable) is created. If you
want to share the variable across all the activities, use shared preferences
for primitive types like integer, float, string, boolean etc. You can also
use a Singleton class, which creates one instance, with getter and setter
methods.
By using Singleton class, you can share all the data including string arrays
and arraylist throughout your application.
Here is an example of Singleton class :
public class Singleton {
private static Singleton instance;
public String userid = "";
public String[] cateogoryName;
private ArrayList<Bitmap> CatImageBit = new ArrayList<Bitmap>();
private Bitmap freebitmap;
public String[] getcategoryName() {
return cateogoryName;
}
public void setcategoryName(String[] cat_name) {
this.cateogoryName = cat_name;
}
public ArrayList<Bitmap> getCatImageBit() {
return CatImageBit;
}
public void setCatImageBit(ArrayList<Bitmap> ImageBit) {
this.CatImageBit = ImageBit;
}
public String getUserid() {
return userid;
}
public void setUserid(String userId) {
this.userid = userId;
}
public Bitmap getfreeImage() {
return freebitmap;
}
public void setfreeImage(Bitmap freeimage) {
this.freebitmap = freeimage;
}
// constructor is made inaccessible by declaring
// it private
private Singleton() {
}
// Access to the single instance of the class is
// provided by a static accessor method
public static Singleton getInstance() {
// returns a reference of the private instance
return instance;
}
public static Singleton createMySingleton() {
if (instance == null)
instance = new Singleton();
// instance.setUname(username);
return instance;
}
}
// End of code
I hope this helps you.
Regards,
Shruthi.
On Tue, Apr 12, 2011 at 12:41 PM, Christophe <
[email protected]> wrote:
> hello everybody,
>
> I use a static variable to share data across all the activities of my
> apps.
> The problems is that sometimes this variable is set to null, which
> cause the others activities to crash (nullPointersException) ...
>
> does somebody have an idea of what could set the static variables to
> null ?
> regards,
> Christophe
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en