Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.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
内部类的Android弱引用_Android_Memory Leaks_Static_Weak References_Inner Classes - Fatal编程技术网

内部类的Android弱引用

内部类的Android弱引用,android,memory-leaks,static,weak-references,inner-classes,Android,Memory Leaks,Static,Weak References,Inner Classes,我已经看完了这篇文章。本文建议使用弱引用的静态内部类 public class GalleryVideo extends Activity { private int AUDIO_NO = 1; ........................... ................ @Override public void onCreate(Bundle savedInstanceState) { super.onCreat

我已经看完了这篇文章。本文建议使用弱引用的静态内部类

public class GalleryVideo extends Activity {

    private int AUDIO_NO = 1; 
    ...........................
    ................  

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        gallery = (Gallery) findViewById(R.id.examplegallery);
    gallery.setAdapter(new AddImgAdp(this));
    }



    static  public class AddImgAdp extends BaseAdapter {

    private int GalItemBg;
        private Context cont;
        private WeakReference<GalleryVideo> mGalleryVideo;

        public AddImgAdp(Context c) {

            mGalleryVideo = new WeakReference<GalleryVideo>(c);

            TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
            GalItemBg = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);
            typArray.recycle();
        }


        public long getItemId(int position) {

            final GalleryVideo galleryVideo = mGalleryVideo.get();
            if(galleryVideo == null){

              if(galleryVideo.AUDIO_NO==4){

               ..................
               ...............
              }
            }
       }
   }

}
公共类GalleryVideo扩展活动{
专用int音频_NO=1;
...........................
................  
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
gallery=(gallery)findViewById(R.id.examplegallery);
gallery.setAdapter(新AddImgAdp(本));
}
静态公共类AddimGapp扩展BaseAdapter{
私人国际加利腾BG;
私人语境控制;
私人WeakReference mGalleryVideo;
公共AddImgAdp(上下文c){
mGalleryVideo=新的WeakReference(c);
TypedArray typArray=获取StyledAttributes(R.styleable.GalleryTheme);
GalItemBg=typArray.getResourceId(R.styleable.GalleryTheme\u android\u galleryItemBackground,0);
typArray.recycle();
}
公共长getItemId(int位置){
final GalleryVideo GalleryVideo=mGalleryVideo.get();
如果(galleryVideo==null){
if(galleryVideo.AUDIO_NO==4){
..................
...............
}
}
}
}
}

这是使用弱引用的内部类的正确方法吗。?上述代码内存泄漏安全吗?

如果仅在
GalleryVideo
活动中使用适配器对象,则不需要使用弱引用

您的代码段是内存泄漏安全的,这取决于您对该代码段之外的对象所做的操作,尽管您的应用程序是否安全

只需确保在活动中创建的对象中没有引用该活动(尤其包括非静态内部类和匿名类)的对象离开该活动。

您能否详细说明这一点:“这取决于您对外部对象的处理……”?谢谢