1

我有一个单例类,我首先在 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();
    }

}

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

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

编辑:更多代码

当我初始化单例时:我运行数组列表holder = RestaurantMenuHolder.getInstance(restaurantMenu);在哪里restaurantMenu。我初始化它来存储数据。这已验证有效,此时单例不为空。

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

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

该问题似乎出现在 ImageAdapter 中。我对这两个活动都使用了带有 ImageAdapter 的slidingMenu。单例适用于第一个活动并且不为空,但当我尝试在第二个活动中再次使用它时变为空。

这是来自 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();结果是NullPointerException. Log.d((menuHolder == null) + "", "MENUHOLDER NULL");此时返回true,当它之前返回时false

4

1 回答 1

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

}
于 2013-08-23T04:55:54.027 回答