Android Fragment中的内存泄漏

时间:2019-06-07 14:45:40

标签: android

我的应用程序中有一个内存泄漏,当我打开第一个片段时,它会感染。 这是我的应用程序中的适配器:

public class card_Adapter extends RecyclerView.Adapter<card_Adapter.myViewHolder> {

    Context mcontext;
    List<card_item> mData;

    public card_Adapter(Context mcontext, List<card_item> mlist) {
        this.mcontext = mcontext;
        this.mData = mData;
    }


    @Override
    public myViewHolder onCreateViewHolder( ViewGroup viewGroup, int i) {

        LayoutInflater inflater=LayoutInflater.from(mcontext);
        View view=  inflater.inflate(R.layout.card_itm,viewGroup, false);

        return new myViewHolder(view);
    }

    @Override
    public void onBindViewHolder( myViewHolder myViewHolder, int i) {
        myViewHolder.background_img.setImageResource(mData.get(i).getBackground());
        myViewHolder.profile_photo.setImageResource(mData.get(i).getProfilephotp());
        myViewHolder.tv_name.setText(mData.get(i).getProfilename());
        myViewHolder.tv_nbabout.setText(mData.get(i).getNbabout()+"Followers");

    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public class myViewHolder extends RecyclerView.ViewHolder{
        ImageView profile_photo,background_img;
        TextView tv_name,tv_nbabout;

        public myViewHolder( View itemView) {
            super(itemView);
            profile_photo=itemView.findViewById(R.id.profile_img);
            background_img=itemView.findViewById(R.id.card_background);
            tv_name=itemView.findViewById(R.id.card_name);
            tv_nbabout=itemView.findViewById(R.id.card_about);
        }
    }
}

这是我发生内存泄漏的片段

public class Homefrag extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view =inflater.inflate(R.layout.frag_home, null);

        Window  window =getActivity().getWindow();
        window.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        RecyclerView  recyclerView =  view. findViewById(R.id.rv_list);
        List<card_item> mlist = new ArrayList<>();

        mlist.add(new card_item(R.drawable.ali_name,"ali",R.drawable.ali,2500));
        mlist.add(new card_item(R.drawable.h_name,"ali",R.drawable.h_name,2500));
        mlist.add(new card_item(R.drawable.kha,"ali",R.drawable.kha_name,2500));
        mlist.add(new card_item(R.drawable.moh_name,"ali",R.drawable.moh,2500));
        mlist.add(new card_item(R.drawable.sal,"ali",R.drawable.sal_name,2500));


        card_Adapter card_adapter =new card_Adapter(getContext(),mlist);
        recyclerView.setAdapter(card_adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        return view;
    }
}

1 个答案:

答案 0 :(得分:1)

不知道这是否与您的内存泄漏有关,但它确实是一个错字。您有this.mData = mData,但您可能应该有this.mData = mlist。

public card_Adapter(Context mcontext, List<card_item> mlist) {
    this.mcontext = mcontext;
    this.mData = mData;
}