Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
弱引用的Java单例_Java_Android_Singleton_Weak References - Fatal编程技术网

弱引用的Java单例

弱引用的Java单例,java,android,singleton,weak-references,Java,Android,Singleton,Weak References,这是我的弱引用的单例类 public class HandheldMapViewProvider { private static WeakReference<HandheldMapViewProvider> mInstance = null; private HandheldMapViewProvider(){ } public static synchronized WeakReference<HandheldMapViewProv

这是我的弱引用的单例类

public class HandheldMapViewProvider  {

    private static WeakReference<HandheldMapViewProvider> mInstance = null;

    private HandheldMapViewProvider(){

    }

    public static synchronized WeakReference<HandheldMapViewProvider> getInstance(){
       if(mInstance == null){
         mInstance = new WeakReference<HandheldMapViewProvider>(new HandheldMapViewProvider());
       }
       return mInstance;
    }

    public void onprint(String data){
        Log.D("TAG",data)
    }


}
公共类HandheldMapViewProvider{
私有静态WeakReference mInstance=null;
私有HandheldMapViewProvider(){
}
公共静态同步WeakReference getInstance(){
if(minInstance==null){
MinInstance=新的WeakReference(新的HandheldMapViewProvider());
}
回报率;
}
公共void onprint(字符串数据){
Log.D(“标签”,数据)
}
}
以上类别的用法如下

 private WeakReference<HandheldMapViewProvider> hereMapViewProvider;

 public void onprint(){
     hereMapViewProvider = HandheldMapViewProvider.getInstance();
     hereMapViewProvider.get().onprint("somevalue");
 }
private WeakReference hereMapViewProvider;
公开作废onprint(){
hereMapViewProvider=HandheldMapViewProvider.getInstance();
hereMapViewProvider.get().onprint(“somevalue”);
}
第一次调用onprint方法时,由于get()导致的应用程序get崩溃有时为null

知道我哪里做错了吗。这并不是一直都在发生

解决方案如下

public static synchronized HandheldMapViewProvider getInstance(){
  HandheldMapViewProvider mapProvider = mInstance == null ? null :mInstance.get();

  if(mapProvider == null){

       mInstance = new WeakReference<HandheldMapViewProvider>(mapProvider =new HandheldMapViewProvider());
   }

   return mapProvider;

  }
public static synchronized HandheldMapViewProvider getInstance(){
HandheldMapViewProvider mapProvider = mInstance == null ? null :mInstance.get();

if(mapProvider == null){

   mInstance = new WeakReference<HandheldMapViewProvider>(mapProvider =new 
   HandheldMapViewProvider());
}

return mapProvider;

}
公共静态同步HandheldMapViewProvider getInstance()的{ HandheldMapViewProvider mapProvider=MinInstance==null?null:MinInstance.get(); if(mapProvider==null){ MinInstance=new WeakReference(mapProvider=new HandheldMapViewProvider()); } 返回地图提供者; }
问题在于,您将
HandheldMapViewProvider
存储为
WeakReference
。 垃圾收集器清除该值,因此
get()
返回null


要避免这种行为,只需删除
weakreference
实现,并将
HandheldMapViewProvider
存储为“常规”静态字段

问题是您将
手持MapViewProvider
存储为
WeakReference
。 垃圾收集器清除该值,因此
get()
返回null


要避免这种行为,只需删除
weakreference
实现,并将
HandheldMapViewProvider
存储为“常规”静态字段

弱引用对象,它不会阻止其引用对象被终结、终结,然后被回收

垃圾收集器在某个时间点确定哪个对象是弱可访问的。届时,它将自动清除对该对象的所有弱引用,以及对任何其他弱可及对象的所有弱引用,通过强引用和软引用链可以从这些弱可及对象中获得该对象。同时,它将声明所有以前弱可达的对象都是可终结的

当我们使用弱引用时,在同一时间或稍后的某个时间,它将使那些新清除的弱引用进入引用队列

这是从垃圾收集器中发生的…所以我们不知道什么时候会发生

您可以使用try-catch块保存代码,以避免崩溃

public void onprint(){
    hereMapViewProvider = HandheldMapViewProvider.getInstance();
    try{
        hereMapViewProvider.get().onprint("somevalue");
    }catch(Exception e){
      Log.e("Err", e.toString()+"");
    }
}

弱引用对象,不阻止其引用对象可终结、终结和回收

垃圾收集器在某个时间点确定哪个对象是弱可访问的。届时,它将自动清除对该对象的所有弱引用,以及对任何其他弱可及对象的所有弱引用,通过强引用和软引用链可以从这些弱可及对象中获得该对象。同时,它将声明所有以前弱可达的对象都是可终结的

当我们使用弱引用时,在同一时间或稍后的某个时间,它将使那些新清除的弱引用进入引用队列

这是从垃圾收集器中发生的…所以我们不知道什么时候会发生

您可以使用try-catch块保存代码,以避免崩溃

public void onprint(){
    hereMapViewProvider = HandheldMapViewProvider.getInstance();
    try{
        hereMapViewProvider.get().onprint("somevalue");
    }catch(Exception e){
      Log.e("Err", e.toString()+"");
    }
}
解决方案如下

public static synchronized HandheldMapViewProvider getInstance(){
  HandheldMapViewProvider mapProvider = mInstance == null ? null :mInstance.get();

  if(mapProvider == null){

       mInstance = new WeakReference<HandheldMapViewProvider>(mapProvider =new HandheldMapViewProvider());
   }

   return mapProvider;

  }
public static synchronized HandheldMapViewProvider getInstance(){
HandheldMapViewProvider mapProvider = mInstance == null ? null :mInstance.get();

if(mapProvider == null){

   mInstance = new WeakReference<HandheldMapViewProvider>(mapProvider =new 
   HandheldMapViewProvider());
}

return mapProvider;

}
公共静态同步HandheldMapViewProvider getInstance()的{ HandheldMapViewProvider mapProvider=MinInstance==null?null:MinInstance.get(); if(mapProvider==null){ MinInstance=new WeakReference(映射提供程序=new HandheldMapViewProvider()); } 返回地图提供者; } 解决方案如下

public static synchronized HandheldMapViewProvider getInstance(){
  HandheldMapViewProvider mapProvider = mInstance == null ? null :mInstance.get();

  if(mapProvider == null){

       mInstance = new WeakReference<HandheldMapViewProvider>(mapProvider =new HandheldMapViewProvider());
   }

   return mapProvider;

  }
public static synchronized HandheldMapViewProvider getInstance(){
HandheldMapViewProvider mapProvider = mInstance == null ? null :mInstance.get();

if(mapProvider == null){

   mInstance = new WeakReference<HandheldMapViewProvider>(mapProvider =new 
   HandheldMapViewProvider());
}

return mapProvider;

}
公共静态同步HandheldMapViewProvider getInstance()的{ HandheldMapViewProvider mapProvider=MinInstance==null?null:MinInstance.get(); if(mapProvider==null){ MinInstance=new WeakReference(映射提供程序=new HandheldMapViewProvider()); } 返回地图提供者; }
您的主要故障首先是取消了singelton模式。从何处调用最后几段代码?@EnamulHaque From activity。基本上相同的代码工作得很好。有时我会变空。您是否经常调用该活动。。比如2-3次repetitively@EnamulHaque不,你的主要缺点是首先调整singelton模式。从何处调用最后几段代码?@EnamulHaque From activity。基本上相同的代码工作得很好。有时我会变空。您是否经常调用该活动。。比如2-3次repetitively@EnamulHaque不,嗨。。。你有别的解决办法吗?如果有,请分享。是的,将分享解决方案。嗨。。。你有别的解决办法吗?如果有,请分享。是的,将分享解决方案。