简体   繁体   English

Android单例变为空

[英]Android singleton becomes null

I have a singleton class which I first instantiate in 1 activity, then use it in 2 other activities. 我有一个单例类,我首先在1个活动中实例化,然后在2个其他活动中使用它。 When the 2nd activity loads, the instance becomes null, where is was not null before. 加载第二个活动时,该实例将变为null,而其中的not之前不是null。

Here is the code for the singleton class: 这是单例类的代码:

public class RestaurantMenuHolder{

    private static RestaurantMenuHolder mInstance = null;
    public static ArrayList<RestaurantMenuObject> restaurantMenu;

    public static RestaurantMenuHolder getInstance(ArrayList<RestaurantMenuObject> restaurantMenu){
        if(mInstance == null){
            mInstance = new RestaurantMenuHolder(restaurantMenu);
        }
        return mInstance;
    }

    public static RestaurantMenuHolder getInstance(){
        return mInstance;
    }

    private RestaurantMenuHolder(ArrayList<RestaurantMenuObject> restaurantMenu){
        this.restaurantMenu = restaurantMenu;
    }

    public static int getCount(){
        return restaurantMenu.size();
    }

}

I have tried adding synchronized to the getInstance methods, the constructor and getCount , but I still get null when the second activity loads. 我尝试将synchronized添加到getInstance方法,构造函数和getCount ,但是在第二个活动加载时仍然会为null。 Does anyone know why or how the singleton would suddenly become null? 有谁知道为什么单身人士会突然变得空虚?

I can provide code for the activities if required. 如果需要,我可以提供活动代码。

EDIT: More Code 编辑:更多代码

When I initialize the singleton: I run holder = RestaurantMenuHolder.getInstance(restaurantMenu); 初始化单例时:运行holder = RestaurantMenuHolder.getInstance(restaurantMenu); where restaurantMenu is an arrayList. 其中restaurantMenu是一个arrayList。 I initialize this to store data. 我将其初始化以存储数据。 This is verified to work, the singleton is not null at this point. 这已被验证可以正常工作,此时单例不为null。

In the first activity that I use the singleton, I run RestaurantMenuHolder menuHolder = RestaurantMenuHolder.getInstance(); 在使用单例的第一个活动中,我运行RestaurantMenuHolder menuHolder = RestaurantMenuHolder.getInstance(); in onCreateView for the fragment. 在片段的onCreateView中。 I use this instance to retrieve the data previously stored. 我使用此实例来检索以前存储的数据。 The singleton is also verified to work here. 单例也经过验证可以在这里工作。

When the second activity is started, the singleton becomes null, but not immediately. 当第二个活动开始时,单例变为空,但不是立即变为空。 I run menuHolder = RestaurantMenuHolder.getInstance(); 我运行menuHolder = RestaurantMenuHolder.getInstance(); again in the second activity and retrieve some more data, all of which is valid. 再次在第二个活动中,并检索更多的数据,所有这些数据都是有效的。

The problem appears to occur in the ImageAdapter. 该问题似乎出现在ImageAdapter中。 I use a slidingMenu with an ImageAdapter for both activities. 我对这两个活动都使用了带有ImageAdapter的slideingMenu。 The singleton works for the first activity and is not null, but becomes null when I try to use it again in the second activity. 单例适用于第一个活动,但不为null,但当我尝试在第二个活动中再次使用它时,其为null。

Here is code from the ImageAdapter: 这是来自ImageAdapter的代码:

public class ImageAdapter extends BaseAdapter{

    private static LayoutInflater inflater = null;
    private ViewHolder holder;
    private String id;
    private RestaurantMenuHolder menuHolder;
    private RestaurantLocationHolder locationHolder;
    private Context context;

    private String[] sliding_list = {"Home", "Skip to Menu", "Location & Hours", "About Us"};
    Typeface tf;

    public ImageAdapter(Context context, String id){
        this.context = context;
        inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.id = id;
        tf = Typeface.createFromAsset(context.getAssets(),
                "Roboto-Light.ttf");
        menuHolder = RestaurantMenuHolder.getInstance();
        locationHolder = RestaurantLocationHolder.getInstance(context);
        MenuUtilities.setImageHash();
    }

    @Override
    public int getCount() {
        if(id == "menu"){
            Log.d((menuHolder == null) + "", "MENUHOLDER NULL");
            return menuHolder.getCount();
        } else if (id == "location") {
            return locationHolder.getCount();
        } else {
            return sliding_list.length;
        }
    }
... more code

When the second activity is started, return menuHolder.getCount(); 当第二个活动开始时, return menuHolder.getCount(); results in a NullPointerException . 导致NullPointerException Log.d((menuHolder == null) + "", "MENUHOLDER NULL"); returns true at this point, when it had previously returned false . 返回true在这一点上,当它以前返回false

just Do single change and will work 只需做一次零钱就可以了

public class RestaurantMenuHolder{

    private static RestaurantMenuHolder mInstance = null;
    public static ArrayList<RestaurantMenuObject> restaurantMenu;

    public static RestaurantMenuHolder getInstance(ArrayList<RestaurantMenuObject> restaurantMenu){
        if(mInstance == null){
            mInstance = new RestaurantMenuHolder(restaurantMenu);
        }
        return mInstance;
    }

    public static RestaurantMenuHolder getInstance(){
        if(mInstance == null){
            mInstance = new RestaurantMenuHolder(restaurantMenu);
        }
        return mInstance;
    }

    private RestaurantMenuHolder(ArrayList<RestaurantMenuObject> restaurantMenu){
        this.restaurantMenu = restaurantMenu;
    }

    public static int getCount(){
        return restaurantMenu.size();
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM