当Android私有属性突然变为null时,可能是由于多线程并发访问或者内存不足导致的。以下是一种可能的解决方法:
private Object myObject;
private final Object lock = new Object();
public void setMyObject(Object object) {
synchronized (lock) {
this.myObject = object;
}
}
public Object getMyObject() {
synchronized (lock) {
return myObject;
}
}
private ConcurrentHashMap myMap = new ConcurrentHashMap<>();
public void setMyObject(String key, Object object) {
myMap.put(key, object);
}
public Object getMyObject(String key) {
return myMap.get(key);
}
private Object myObject;
public void setMyObject(Object object) {
Object tempObject = object;
this.myObject = tempObject;
}
以上是一些可能的解决方法,具体解决方法可能因情况而异。