当前位置: 首页 > 知识库问答 >
问题:

Android Singleton

东门清夷
2023-03-14
Class MyApplication is created :


    public class MyApplication extends Application {  // code - freeCodeCamp.org 
    
        private static MyApplication singleton;
        private List<Location> myLocations;
    
    
        public List<Location> getMyLocations() {
            return myLocations;
        }
    
        public void setMyLocations(List<Location> myLocations) {
            this.myLocations = myLocations;
        }
    
        public MyApplication getInstance(){
            return singleton;
        }
    
        public void OnCreate() {
            super.onCreate();
            singleton = this;
            myLocations = new ArrayList<>();
        }
    }

    MyApplication myApplication = (MyApplication)getApplicationContext();
    savedLocations = myApplication.getMyLocations();
    savedLocations.add(currentLocation);

currentLocation具有有效的位置值。有什么提示吗?谢谢

共有1个答案

龚安民
2023-03-14

MyLocations为null,因为您拼错了onCreate方法签名,而且实际上从未调用过它。

您应该更改以下内容:

public void OnCreate() {

对此:

@Override
public void onCreate() {
 类似资料:

相关问答

相关文章

相关阅读