Android单例变为空

时间:2013-08-23 02:15:36

标签: java android singleton

我有一个单例类,我首先在1个活动中实例化,然后在其他2个活动中使用它。加载第二个活动时,实例变为空,其中之前的值不为空。

以下是单例类的代码:

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();
    }

}

我尝试将synchronized添加到getInstance方法,构造函数和getCount,但是当第二个活动加载时我仍然为null。有谁知道单身人士为什么或如何突然变成空?

如果需要,我可以为活动提供代码。

编辑:更多代码

当我初始化单例时:我运行holder = RestaurantMenuHolder.getInstance(restaurantMenu);,其中restaurantMenu是一个arrayList。我初始化它来存储数据。这被证实有效,此时单例不为空。

在我使用单例的第一个活动中,我在RestaurantMenuHolder menuHolder = RestaurantMenuHolder.getInstance();中为片段运行onCreateView。我使用此实例来检索先前存储的数据。单身人士也经过验证可以在这里工作。

当第二个活动开始时,单例变为空,但不是立即。我在第二个活动中再次运行menuHolder = RestaurantMenuHolder.getInstance();并检索更多数据,所有数据都有效。

问题似乎发生在ImageAdapter中。我将一个带有ImageAdapter的slidingMenu用于这两个活动。单例适用于第一个活动而不是null,但当我尝试在第二个活动中再次使用它时,它变为null。

以下是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

第二项活动开始后,return menuHolder.getCount();会产生NullPointerExceptionLog.d((menuHolder == null) + "", "MENUHOLDER NULL");此时返回true,之前已返回false

1 个答案:

答案 0 :(得分:0)

只需进行一次更改即可使用

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();
    }

}