在片段显示图像内存问题 [英] Memory issues in fragments showing images

查看:152
本文介绍了在片段显示图像内存问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的片段显示图像/ pages.I有一个活动(主),它包含了所有的片段。

 包com.example.hscroll.demo;

进口android.os.Bundle;
。进口的Andr​​oid preference preferenceManager。
进口android.support.v4.app.Fragment;
进口android.support.v4.app.FragmentActivity;
进口android.support.v4.app.FragmentManager;
进口android.support.v4.app.FragmentStatePagerAdapter;
进口android.support.v4.app.ListFragment;
进口android.support.v4.view.ViewPager;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.LinearLayout;

进口com.crittercism.app.Crittercism;
进口com.example.hscroll.customer.BitmapWeakReference;
进口com.example.hscroll.customer.PromotionalPriceListAdditionsDataAdaptor;
进口com.example.hscroll.customer.PromotionalPriceListDataAdaptor;
进口com.example.hscroll.library.imagezoom.ImageViewTouch;

公共类MainAct扩展FragmentActivity {

私人ImageViewTouch mImageView;
MyPagerAdapter mPagerAdapter;
ViewPager viewPager;
 静态最终诠释NUM_ITEMS = 58;

@覆盖
保护无效的onCreate(包savedInstanceState){

    super.onCreate(savedInstanceState);
    Crittercism.init(getApplicationContext(),4f391d0fb09315319d00048d);
    preferenceManager.setDefaultValues​​(这一点,R.xml preference,假的。);

    的setContentView(R.layout.viewpager_layout);

    mPagerAdapter =新MyPagerAdapter(getSupportFragmentManager());

    viewPager =(ViewPager)findViewById(R.id.viewpager);
    viewPager.setAdapter(mPagerAdapter);
}


公共静态类MyPagerAdapter扩展FragmentStatePagerAdapter {
    公共MyPagerAdapter(FragmentManager FM){
        超(FM);
    }

    @覆盖
    公众诠释getCount将(){
        返回NUM_ITEMS;
    }

    @覆盖
    公共片段的getItem(INT位置){
            返回的newInstance(位置);
    }


}

静态片段的newInstance(INT位置){

    片段数据= NULL;
    开关(位置){

    情况下0:
        数据=新片段1();
    打破;

    情况1 :
        数据=新FragmentSignupForm();
    打破;

    案例2:
        数据=新Fragment2();
    打破;

    案例3:
        数据=新Fragment3();
    打破;

    壳体4:
        数据=新Fragment4();
    打破;

    壳体5:
        数据=新Fragment5();
    打破;

    情况6:
        数据=新Fragment6();
    打破;

    案例7:
        数据=新Fragment7();
    打破;

    案例8:
        数据=新Fragment8();
    打破;

    案例9:
        数据=新Fragment9();
    打破;

    案例10:
        数据=新MyMapFragment();
    打破;

    案例11:
        数据=新FragmentQuestionaire();
        打破;

    案例12:
        数据=新Fragment10();
        打破;

    案例13:
        数据=新Fragment11();
        打破;

    案例14:
        数据=新Fragment12();
        打破;

    案例15:
        数据=新Fragment13();
        打破;

    案例16:
        数据=新Fragment14();
        打破;

    案例17:
        数据=新Fragment15();
        打破;

    案例18:
        数据=新Fragment16();
        打破;

    案例19:
        数据=新Fragment17();
        打破;

    案例20:
        数据=新Fragment18();
        打破;

    案例21:
        数据=新Fragment19();
        打破;

    案例22:
        数据=新Fragment20();
        打破;

    案例23:
        数据=新Fragment21();
        打破;

    案例24:
        数据=新Fragment22();
        打破;

    案例25:
        数据=新Fragment23();
        打破;

    案例26:
        数据=新Fragment24();
        打破;

    案例27:
        数据=新Fragment25();
        打破;

    案例28:
        数据=新Fragment26();
        打破;

    案例29:
        数据=新Fragment27();
        打破;

    案例30:
        数据=新Fragment28();
        打破;

    案例31:
        数据=新Fragment29();
        打破;

    案例32:
        数据=新Fragment30();
        打破;

    案例33:
        数据=新Fragment31();
        打破;

    案例34:
        数据=新Fragment32();
        打破;

    案例35:
        数据=新Fragment33();
        打破;

    案例36:
        数据=新Fragment34();
        打破;

    案例37:
        数据=新Fragment35();
        打破;

    案例38:
        数据=新Fragment36();
        打破;

    案例39:
        数据=新Fragment37();
        打破;

    案例40:
        数据=新Fragment38();
        打破;

    案例41:
        数据=新Fragment39();
        打破;

    案例42:
        数据=新Fragment40();
        打破;

    案例43:
        数据=新Fragment41();
        打破;

    案例44:
        数据=新Fragment42();
        打破;

    案例45:
        数据=新Fragment43();
        打破;

    案例46:
        数据=新Fragment44();
        打破;

    案例47:
        数据=新Fragment45();
        打破;

    案例48:
        数据=新Fragment46();
        打破;

    案例49:
        数据=新Fragment47();
        打破;

    案例50:
        数据=新Fragment48();
        打破;

    案例51:
        数据=新Fragment49();
        打破;

    案例52:
        数据=新Fragment50();
        打破;

    案例53:
        数据=新Fragment51();
        打破;

    案例54:
        数据=新Fragment52();
        打破;

    案例55:
        数据=新Fragment53();
        打破;

    案例56:
        数据=新Fragment54();
        打破;

    案例57:
        数据=新Fragment55();
        打破;

    默认 :
        数据=新FragmentQuestionaire();
    打破;

    }

        返回的数据;
}

@覆盖
保护无效的onDestroy(){
    super.onDestroy();
    FragmentSignupForm.CUSTOMER_ADDRESS =;
    FragmentSignupForm.CUSTOMER_ADDRESS1 =;
    FragmentSignupForm.CUSTOMER_ADDRESS2 =;
    FragmentSignupForm.CUSTOMER_EMAIL =;
    FragmentSignupForm.CUSTOMER_ID =;
    FragmentSignupForm.CUSTOMER_POST code =;
    FragmentSignupForm.CUSTOMER_NAME =;
    FragmentSignupForm.Telephone =;
    Fragment50.Total =0;
    Fragment47.amount2 = 0;
    Fragment47.systemSize =;
    PromotionalPriceListAdditionsDataAdaptor.TotalAdditionalPrice = 0;
    PromotionalPriceListDataAdaptor.TotalPrice = 0;
    PromotionalPriceListDataAdaptor.Capacity =;
    PromotionalPriceListDataAdaptor.NumberOfPanels =;
    PromotionalPriceListDataAdaptor.PanelSize =;
    PromotionalPriceListDataAdaptor.PanelCapacity =;
    PromotionalPriceListDataAdaptor.SurfaceAreaReuired =;
    PromotionalPriceListDataAdaptor.Price =;
    PromotionalPriceListDataAdaptor.PromotionalOffer =;
    PromotionalPriceListDataAdaptor.TotalOfferPrice =;
   }

   }
 

下面是code的片段类 -

 包com.example.hscroll.demo;

进口java.io.BufferedInputStream中;
进口java.io.FileInputStream中;

进口android.os.Bundle;
进口android.support.v4.app.Fragment;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.LinearLayout;

进口com.example.hscroll.customer.BitmapWeakReference;
 进口com.example.hscroll.library.imagezoom.ImageViewTouch;

公共类Fragment2扩展片段{

ImageViewTouch imgview;
LayoutInflater充气;
在的FileInputStream;
的BufferedInputStream BUF;
BitmapWeakReference位图;
的ViewGroup CON;

私人最终字符串PATH =到/ mnt / SD卡/理想太阳能/ Layout_1.png;

公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstance)
{
    this.inflater =充气;
    如果(集装箱== NULL)返回NULL;
    容器=(的LinearLayout)inflater.inflate(R.layout.fragment0_layout,集装箱,假);
    CON =容器;
    imgview =(ImageViewTouch)container.findViewById(R.id.imageView1);
    位图=新BitmapWeakReference(imgview.selectImage(inflater.getContext(),PATH));
    如果(位图!= NULL)
        imgview.setImageBitma preSET(bitmap.get(),TRUE);

    返回容器;
}

/ * @覆盖
    公共无效onResume(){

        如果(位图== NULL)
        {
            位= SelectImageFunctions.selectImage(inflater.getContext(),PATH);
        }
        super.onResume();
    } * /

 @覆盖
公共无效onDestroyView(){
     super.onDestroyView();
        imgview.setImageBitmap(空);
        bitmap.clear();
        位= NULL;
        Fragment1.unbindDrawables(con.findViewById(R.id.ll));
        System.gc()的;
    }

   }
 

每次当我移动到下一个片段,previous片段onDestroyView()被调用,我删除的位图和imageviews但还是位图或可绘制的记忆中所有引用不得到释放,内存不断增加。而最后它得到大约50MB的崩溃。

任何一个可以帮助,可以吗?我开发这个应用程序的Galaxy Tab的唯一。

更新 - code动态页面 -

 包com.example.hscroll.demo;


   进口java.io.BufferedReader中;
   进口java.io.IOException异常;
   进口的java.io.InputStream;
   进口java.io.InputStreamReader中;
   进口的java.util.ArrayList;

   进口org.apache.http.HttpEntity;
   进口org.apache.http.Htt presponse;
   进口org.apache.http.NameValuePair;
   进口org.apache.http.client.HttpClient;
   进口org.apache.http.client.entity.UrlEn codedFormEntity;
   进口org.apache.http.client.methods.HttpPost;
   进口org.apache.http.impl.client.DefaultHttpClient;
   进口org.apache.http.message.BasicNameValuePair;
   进口org.json.JSONArray;
   进口org.json.JSONObject;

   进口android.content.Context;
   进口android.os.AsyncTask;
   进口android.os.Bundle;
   进口android.support.v4.app.Fragment;
   进口android.view.LayoutInflater;
   进口android.view.View;
   进口android.view.ViewGroup;
   进口android.view.inputmethod.InputMethodManager;
   进口android.widget.Button;
   进口android.widget.EditText;
   进口android.widget.LinearLayout;
   进口android.widget.ScrollView;
   进口android.widget.Toast;


   公共类FragmentSignupForm扩展片段{

LayoutInflater充气;
字符串结果= NULL;
InputStream的是= NULL;
StringBuilder的SB = NULL;
JSONArray jArray;
的EditText c_nameText;
的EditText c_addressText;
EditText上c_post codeTEXT;
的EditText c_emailText;
的EditText s_nameText;
的EditText c_addressLine2Text;
的EditText telephone_Text;
公共静态字符串CUSTOMER_NAME =;
公共静态字符串CUSTOMER_ADDRESS =;
公共静态字符串CUSTOMER_ADDRESS1 =;
公共静态字符串CUSTOMER_ADDRESS2 =;
公共静态字符串CUSTOMER_POST code =;
公共静态字符串CUSTOMER_ID;
公共静态字符串CUSTOMER_EMAIL =;
公共静态字符串SALES_CONTACT =;
公共静态字符串电话=;

字符串输出= NULL;

ArrayList的<的NameValuePair> namevaluepairs中=新的ArrayList<的NameValuePair>();
的ViewGroup CON;


公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstance)
{
    this.inflater =充气;
    如果(集装箱== NULL)返回NULL;
    容器=(滚动型)inflater.inflate(R.layout.signup_form_layout,集装箱,假);
    CON =容器;
    滚动型SV =(滚动型)container.findViewById(R.id.SV);
    sv.setBackgroundDrawable(AssestUtil.getBitmapFromAsset(inflater.getContext(),bg.jpg));

    的LinearLayout LL =(的LinearLayout)container.findViewById(R.id.ll);
    ll.setBackgroundDrawable(AssestUtil.getBitmapFromAsset(inflater.getContext(),formbg2.png));

    c_nameText =(EditText上)container.findViewById(R.id.cust_name);
    c_nameText.setText(CUSTOMER_NAME);
    c_addressText =(EditText上)container.findViewById(R.id.cust_address);
    c_addressText.setText(CUSTOMER_ADDRESS1);
    c_addressLine2Text =(EditText上)container.findViewById(R.id.cust_Address2);
    c_addressLine2Text.setText(CUSTOMER_ADDRESS2);
    c_post codeTEXT =(EditText上)container.findViewById(R.id.post code);
    c_post codeText.setText(CUSTOMER_POST code);
    c_emailText =(EditText上)container.findViewById(R.id.cust_email);
    c_emailText.setText(CUSTOMER_EMAIL);
    s_nameText =(EditText上)container.findViewById(R.id.sales_name);
    s_nameText.setText(SALES_CONTACT);
    telephone_Text =(EditText上)container.findViewById(R.id.telephone);
    telephone_Text.setText(电话);

        按钮保存=(按钮)container.findViewById(R.id.save_btn);
        按钮复位=(按钮)container.findViewById(R.id.reset_btn);
        最终的上下文语境= inflater.getContext();
          //将表单数据发送到服务器上的点击动作
       save.setOnClickListener(新View.OnClickListener()
       {

            公共无效的onClick(视图v){

                CUSTOMER_NAME = c_nameText.getText()的toString()。
                的System.out.println(CUSTOMER_NAME);
                CUSTOMER_ADDRESS = c_addressText.getText()的toString()+,+ c_addressLine2Text.getText()。的toString()+,+ c_post codeText.getText()的toString();;
                CUSTOMER_POST code = c_post codeText.getText()的toString()。
                电话= telephone_Text.getText()的toString()。

                新SendDataToServer().execute(c_nameText.getText().toString(),c_addressText.getText().toString(),c_addressLine2Text.getText().toString(),c_post$c$cText.getText().toString(),c_emailText.getText().toString(),s_nameText.getText().toString(),telephone_Text.getText().toString());
                InputMethodManager IMM =(InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
              imm.hideSoftInputFromWindow(s_nameText.getWindowToken(),0);
            }
       });

     Reset.setOnClickListener(新View.OnClickListener()
       {

            公共无效的onClick(视图v){

                c_nameText.setText();
                c_addressText.setText();
                c_addressLine2Text.setText();
                c_post codeText.setText();
                c_emailText.setText();
                s_nameText.setText();
                telephone_Text.setText();
            }
       });

    返回容器;
}

私有类SendDataToServer扩展的AsyncTask<字符串,太虚,字符串>
{

    @覆盖
    保护字符串doInBackground(字符串... PARAMS){

        尝试
        {
        nameValuePairs.add(新BasicNameValuePair(CUST_NAME,则params [0]));
        nameValuePairs.add(新BasicNameValuePair(cust_address,则params [1] +,+参数[2]));
        nameValuePairs.add(新BasicNameValuePair(cust_post code,则params [3]));
        nameValuePairs.add(新BasicNameValuePair(CUST_EMAIL,则params [4]));
        nameValuePairs.add(新BasicNameValuePair(sales_name,则params [5]));
        nameValuePairs.add(新BasicNameValuePair(pvgis_data,则params [6]));

             HttpClient的HttpClient的=新DefaultHttpClient();
              HttpPost httppost =新HttpPost(http://ideal.contrastgroup.info/idealsolar/initializeCustomer.php);
                httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));
                HTT presponse响应= httpclient.execute(httppost);
                HttpEntity实体= response.getEntity();
                是= entity.getContent();

                JSONObject的myAway =新的JSONObject(读(是));
                CUSTOMER_ID = myAway.getString(客户ID);
                输出= myAway.getString(信息);


        }
        赶上(例外五)
        {
            输出=空;

        }
        返回输出;
    }

    @覆盖
    保护无效onPostExecute(字符串结果){
        super.onPostExecute(结果);
        如果(result.equalsIgnoreCase(成功))
        {
        Toast.makeText(inflater.getContext(),数据库更新,Toast.LENGTH_SHORT).show();
        }
        其他
        {
            Toast.makeText(inflater.getContext(),错误连接,Toast.LENGTH_SHORT).show();
        }
    }

}

公共静态字符串读取(InputStream的中)抛出IOException异常{
    StringBuilder的SB =新的StringBuilder();
    BufferedReader中R =新的BufferedReader(新的InputStreamReader(在));

    对(串线= r.readLine(!);行= NULL;行= r.readLine()){
        sb.append(线);
    }

    附寄();
    的System.out.println(sb.toString());
    返回sb.toString();
}


@覆盖
公共无效onDestroyView(){
    super.onDestroyView();

    Fragment1.unbindDrawables(con.findViewById(R.id.SV));
    System.gc()的;
}
 

}

 公共静态无效unbindDrawables(查看视图){
        如果(view.getBackground()!= NULL){
        view.getBackground()setCallback(空)。
        }
        如果(查看的instanceof的ViewGroup){
            的for(int i = 0;我≤((ViewGroup中)查看).getChildCount();我++){
            unbindDrawables(((ViewGroup中)查看).getChildAt(一));
            }
        ((ViewGroup中)查看).removeAllViews();
        }
 

解决方案

从谷歌:

  

片段州寻呼机支持       演示如何使用支持类ViewPager的一个FragmentStatePagerAdapter构建用户界面,用户可以   猛冲向左或向右片段之间切换。这个版本的   适配器不保持周围的片段实例的ViewPager有   销毁。

<一个href="http://developer.android.com/resources/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentStatePagerSupport.html" rel="nofollow">http://developer.android.com/resources/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentStatePagerSupport.html

这是正常的寻呼机所不同的是它扩展FragmentStatePagerAdapter。

编辑: 在谷歌code你有这个在适配器:

 公共静态类MyAdapter扩展FragmentStatePagerAdapter {
    公共MyAdapter(FragmentManager FM){
        超(FM);
    }

    @覆盖
    公众诠释getCount将(){
        返回NUM_ITEMS;
    }

    @覆盖
    公共片段的getItem(INT位置){
        返回ArrayListFragment.newInstance(位置);
    }
}
 

的通知getItem()时执行。我们得到的需求新的片段,所以我们不会创建片段之前,我们需要它。这种鲜明的对比您在其中创建所有碎片,并将它们添加到向量方法。 (JAVA的preFER的ArrayList顺便说一句。)你的方法碎片不会被从内存中删除,因为将永远是一个引用片段。

编辑2: 要具体。在下面的:

  @覆盖
公共片段的getItem(INT位置){
    返回ArrayListFragment.newInstance(位置);
}
 

你可以这样做:

  @覆盖
    公共片段的getItem(INT位置){
如果(位置== 1){
返回Fragment1.newInstance(位置);
} 其他 {
返回Fragment2.newInstance(位置);
}

    }
 

I am using fragments to show images/pages.I have one Activity(Main) which contains all the fragments.

package com.example.hscroll.demo;

import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.ListFragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.crittercism.app.Crittercism;
import com.example.hscroll.customer.BitmapWeakReference;
import com.example.hscroll.customer.PromotionalPriceListAdditionsDataAdaptor;
import com.example.hscroll.customer.PromotionalPriceListDataAdaptor;
import com.example.hscroll.library.imagezoom.ImageViewTouch;

public class MainAct extends FragmentActivity{

private ImageViewTouch  mImageView;
MyPagerAdapter mPagerAdapter;
ViewPager viewPager;
 static final int NUM_ITEMS = 58;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState); 
    Crittercism.init(getApplicationContext(), "4f391d0fb09315319d00048d");
    PreferenceManager.setDefaultValues(this, R.xml.preference, false);

    setContentView(R.layout.viewpager_layout);

    mPagerAdapter = new MyPagerAdapter(getSupportFragmentManager());

    viewPager = (ViewPager)findViewById(R.id.viewpager);
    viewPager.setAdapter(mPagerAdapter);
}


public static class MyPagerAdapter extends FragmentStatePagerAdapter {
    public MyPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public int getCount() {
        return NUM_ITEMS;
    }

    @Override
    public Fragment getItem(int position) {
            return newInstance(position);
    }


}

static Fragment newInstance(int position) {

    Fragment data = null;
    switch(position){

    case 0 :
        data = new Fragment1();
    break;

    case 1 :
        data = new FragmentSignupForm();
    break;

    case 2 :
        data = new Fragment2();
    break;

    case 3 :
        data = new Fragment3();
    break;

    case 4 :
        data = new Fragment4();
    break;

    case 5:
        data = new Fragment5();
    break;

    case 6 :
        data = new Fragment6();
    break;

    case 7 :
        data = new Fragment7();
    break;

    case 8 :
        data = new Fragment8();
    break;

    case 9 :
        data = new Fragment9();
    break;

    case 10 :
        data = new MyMapFragment();
    break;

    case 11:
        data = new FragmentQuestionaire();
        break;

    case 12:
        data = new Fragment10();
        break;

    case 13:
        data = new Fragment11();
        break;

    case 14:
        data = new Fragment12();
        break;

    case 15:
        data = new Fragment13();
        break;

    case 16:
        data = new Fragment14();
        break;

    case 17:
        data = new Fragment15();
        break;

    case 18:
        data = new Fragment16();
        break;

    case 19:
        data = new Fragment17();
        break;

    case 20:
        data = new Fragment18();
        break;

    case 21:
        data = new Fragment19();
        break;

    case 22:
        data = new Fragment20();
        break;

    case 23:
        data = new Fragment21();
        break;

    case 24:
        data = new Fragment22();
        break;

    case 25:
        data = new Fragment23();
        break;

    case 26:
        data = new Fragment24();
        break;

    case 27:
        data = new Fragment25();
        break;

    case 28:
        data = new Fragment26();
        break;

    case 29:
        data = new Fragment27();
        break;

    case 30:
        data = new Fragment28();
        break;

    case 31:
        data = new Fragment29();
        break;

    case 32:
        data = new Fragment30();
        break;

    case 33:
        data = new Fragment31();
        break;

    case 34:
        data = new Fragment32();
        break;

    case 35:
        data = new Fragment33();
        break;

    case 36:
        data = new Fragment34();
        break;

    case 37:
        data = new Fragment35();
        break;

    case 38:
        data = new Fragment36();
        break;

    case 39:
        data = new Fragment37();
        break;

    case 40:
        data = new Fragment38();
        break;

    case 41:
        data = new Fragment39();
        break;

    case 42:
        data = new Fragment40();
        break;

    case 43:
        data = new Fragment41();
        break;

    case 44:
        data = new Fragment42();
        break;

    case 45:
        data = new Fragment43();
        break;

    case 46:
        data = new Fragment44();
        break;

    case 47:
        data = new Fragment45();
        break;

    case 48:
        data = new Fragment46();
        break;

    case 49:
        data = new Fragment47();
        break;

    case 50:
        data = new Fragment48();
        break;

    case 51:
        data = new Fragment49();
        break;

    case 52:
        data = new Fragment50();
        break;

    case 53:
        data = new Fragment51();
        break;

    case 54:
        data = new Fragment52();
        break;

    case 55:
        data = new Fragment53();
        break;

    case 56:
        data = new Fragment54();
        break;

    case 57:
        data = new Fragment55();
        break;

    default :
        data = new FragmentQuestionaire();
    break;  

    }

        return data;
}

@Override
protected void onDestroy() {
    super.onDestroy();
    FragmentSignupForm.CUSTOMER_ADDRESS = "";
    FragmentSignupForm.CUSTOMER_ADDRESS1 = "";
    FragmentSignupForm.CUSTOMER_ADDRESS2 = "";
    FragmentSignupForm.CUSTOMER_EMAIL = "";
    FragmentSignupForm.CUSTOMER_ID = "";
    FragmentSignupForm.CUSTOMER_POSTCODE = "";
    FragmentSignupForm.CUSTOMER_NAME = "";
    FragmentSignupForm.Telephone = "";
    Fragment50.Total = "0";
    Fragment47.amount2 = 0;
    Fragment47.systemSize = "";
    PromotionalPriceListAdditionsDataAdaptor.TotalAdditionalPrice = 0;
    PromotionalPriceListDataAdaptor.TotalPrice = 0;
    PromotionalPriceListDataAdaptor.Capacity = "";
    PromotionalPriceListDataAdaptor.NumberOfPanels = "";
    PromotionalPriceListDataAdaptor.PanelSize = "";
    PromotionalPriceListDataAdaptor.PanelCapacity = "";
    PromotionalPriceListDataAdaptor.SurfaceAreaReuired = "";
    PromotionalPriceListDataAdaptor.Price = "";
    PromotionalPriceListDataAdaptor.PromotionalOffer = "";
    PromotionalPriceListDataAdaptor.TotalOfferPrice = "";
   }

   }

Here is the code in fragment class -

package com.example.hscroll.demo;

import java.io.BufferedInputStream;
import java.io.FileInputStream;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.example.hscroll.customer.BitmapWeakReference;
 import com.example.hscroll.library.imagezoom.ImageViewTouch;

public class Fragment2 extends Fragment{

ImageViewTouch imgview ;
LayoutInflater inflater;
FileInputStream in;
BufferedInputStream buf;
BitmapWeakReference bitmap;
ViewGroup con;

private final String  PATH = "/mnt/sdcard/Ideal Solar/Layout_1.png";

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance)
{
    this.inflater = inflater;
    if(container == null)return null;
    container = (LinearLayout)inflater.inflate(R.layout.fragment0_layout, container, false);
    con = container;
    imgview= (ImageViewTouch)container.findViewById(R.id.imageView1);
    bitmap = new BitmapWeakReference(imgview.selectImage(inflater.getContext(), PATH));
    if(bitmap!=null)
        imgview.setImageBitmapReset( bitmap.get(), true );

    return container;
}   

/* @Override
    public void onResume() {

        if(bitmap == null)
        {
            bitmap = SelectImageFunctions.selectImage(inflater.getContext(), PATH);
        }
        super.onResume();
    }*/

 @Override
public void onDestroyView() {
     super.onDestroyView();
        imgview.setImageBitmap(null);
        bitmap.clear();
        bitmap = null;
        Fragment1.unbindDrawables(con.findViewById(R.id.ll));
        System.gc();
    }

   }

Everytime when i move to next fragment,the onDestroyView() of previous fragment get called and i am removing all the references of bitmaps and imageviews but still the memory of bitmaps or drawables dont get released and memory keeps on increasing.and finally it get crashed at around 50MB.

Can any one help one this?I am developing this app for galaxy tab only.

UPDATE - Code for dynamic page -

 package com.example.hscroll.demo;


   import java.io.BufferedReader;
   import java.io.IOException;
   import java.io.InputStream;
   import java.io.InputStreamReader;
   import java.util.ArrayList;

   import org.apache.http.HttpEntity;
   import org.apache.http.HttpResponse;
   import org.apache.http.NameValuePair;
   import org.apache.http.client.HttpClient;
   import org.apache.http.client.entity.UrlEncodedFormEntity;
   import org.apache.http.client.methods.HttpPost;
   import org.apache.http.impl.client.DefaultHttpClient;
   import org.apache.http.message.BasicNameValuePair;
   import org.json.JSONArray;
   import org.json.JSONObject;

   import android.content.Context;
   import android.os.AsyncTask;
   import android.os.Bundle;
   import android.support.v4.app.Fragment;
   import android.view.LayoutInflater;
   import android.view.View;
   import android.view.ViewGroup;
   import android.view.inputmethod.InputMethodManager;
   import android.widget.Button;
   import android.widget.EditText;
   import android.widget.LinearLayout;
   import android.widget.ScrollView;
   import android.widget.Toast;


   public class FragmentSignupForm extends Fragment {

LayoutInflater inflater;
String result = null;
InputStream is = null;
StringBuilder sb = null;
JSONArray jArray;
EditText c_nameText;
EditText c_addressText;
EditText c_postcodeText;
EditText c_emailText;
EditText s_nameText;
EditText c_addressLine2Text;
EditText telephone_Text;
public static String CUSTOMER_NAME = "";
public static String CUSTOMER_ADDRESS = "";
public static String CUSTOMER_ADDRESS1 = "";
public static String CUSTOMER_ADDRESS2 = "";
public static String CUSTOMER_POSTCODE = "";
public static String CUSTOMER_ID;
public static String CUSTOMER_EMAIL = "";
public static String SALES_CONTACT = "";
public static String Telephone = "";

String output = null;

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
ViewGroup con;


public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance)
{
    this.inflater = inflater;
    if(container == null)return null;
    container = (ScrollView)inflater.inflate(R.layout.signup_form_layout, container, false);
    con = container;
    ScrollView sv = (ScrollView)container.findViewById(R.id.SV);
    sv.setBackgroundDrawable(AssestUtil.getBitmapFromAsset(inflater.getContext(), "bg.jpg"));

    LinearLayout ll = (LinearLayout)container.findViewById(R.id.ll);
    ll.setBackgroundDrawable(AssestUtil.getBitmapFromAsset(inflater.getContext(), "formbg2.png"));

    c_nameText = (EditText)container.findViewById(R.id.cust_name);
    c_nameText.setText(CUSTOMER_NAME);
    c_addressText = (EditText)container.findViewById(R.id.cust_address);
    c_addressText.setText(CUSTOMER_ADDRESS1);
    c_addressLine2Text = (EditText)container.findViewById(R.id.cust_Address2);
    c_addressLine2Text.setText(CUSTOMER_ADDRESS2);
    c_postcodeText = (EditText)container.findViewById(R.id.postcode);
    c_postcodeText.setText(CUSTOMER_POSTCODE);
    c_emailText = (EditText)container.findViewById(R.id.cust_email);
    c_emailText.setText(CUSTOMER_EMAIL);
    s_nameText = (EditText)container.findViewById(R.id.sales_name);
    s_nameText.setText(SALES_CONTACT);
    telephone_Text = (EditText)container.findViewById(R.id.telephone);
    telephone_Text.setText(Telephone);             

        Button save = (Button)container.findViewById(R.id.save_btn);
        Button Reset = (Button)container.findViewById(R.id.reset_btn);
        final Context context = inflater.getContext();
          //Send form data to server on click action
       save.setOnClickListener(new View.OnClickListener()
       {

            public void onClick(View v) {

                CUSTOMER_NAME = c_nameText.getText().toString();
                System.out.println(CUSTOMER_NAME);
                CUSTOMER_ADDRESS = c_addressText.getText().toString() + ","+ c_addressLine2Text.getText().toString()+","+c_postcodeText.getText().toString();;
                CUSTOMER_POSTCODE = c_postcodeText.getText().toString();
                Telephone = telephone_Text.getText().toString();

                new SendDataToServer().execute(c_nameText.getText().toString(),c_addressText.getText().toString(),c_addressLine2Text.getText().toString(),c_postcodeText.getText().toString(),c_emailText.getText().toString(),s_nameText.getText().toString(),telephone_Text.getText().toString());
                InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
              imm.hideSoftInputFromWindow(s_nameText.getWindowToken(), 0);
            }
       });

     Reset.setOnClickListener(new View.OnClickListener()
       {

            public void onClick(View v) {

                c_nameText.setText(""); 
                c_addressText.setText("");
                c_addressLine2Text.setText("");
                c_postcodeText.setText("");
                c_emailText.setText("");
                s_nameText.setText("");
                telephone_Text.setText("");
            }
       });

    return container;
}

private class SendDataToServer extends AsyncTask<String, Void, String>
{

    @Override
    protected String doInBackground(String... params) {

        try
        {                   
        nameValuePairs.add(new BasicNameValuePair("cust_name",params[0]));
        nameValuePairs.add(new BasicNameValuePair("cust_address",params[1]+","+params[2]));
        nameValuePairs.add(new BasicNameValuePair("cust_postcode",params[3]));
        nameValuePairs.add(new BasicNameValuePair("cust_email",params[4]));
        nameValuePairs.add(new BasicNameValuePair("sales_name",params[5]));
        nameValuePairs.add(new BasicNameValuePair("pvgis_data", params[6]));    

             HttpClient httpclient = new DefaultHttpClient();
              HttpPost httppost = new HttpPost("http://ideal.contrastgroup.info/idealsolar/initializeCustomer.php");
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();

                JSONObject myAway = new JSONObject(read(is));
                CUSTOMER_ID = myAway.getString("custID");
                output = myAway.getString("message");


        }
        catch(Exception e)
        {
            output = "null";

        }
        return output;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        if(result.equalsIgnoreCase("success"))
        {
        Toast.makeText(inflater.getContext(), "Database updated", Toast.LENGTH_SHORT).show();
        }
        else
        {
            Toast.makeText(inflater.getContext(), "Error in Connection", Toast.LENGTH_SHORT).show();
        }
    }

}

public static String read(InputStream in) throws IOException {
    StringBuilder sb = new StringBuilder();
    BufferedReader r = new BufferedReader(new InputStreamReader(in));

    for (String line = r.readLine(); line != null; line = r.readLine()) {
        sb.append(line);
    }

    in.close();
    System.out.println(sb.toString());
    return sb.toString();
}


@Override
public void onDestroyView() {
    super.onDestroyView();

    Fragment1.unbindDrawables(con.findViewById(R.id.SV));
    System.gc();
}

}

     public static void unbindDrawables(View view) {
        if (view.getBackground() != null) {
        view.getBackground().setCallback(null);
        }
        if (view instanceof ViewGroup) {
            for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
            unbindDrawables(((ViewGroup) view).getChildAt(i));
            }
        ((ViewGroup) view).removeAllViews();
        }

解决方案

From google:

Fragment State Pager Support Demonstrates the use of the support class ViewPager with a FragmentStatePagerAdapter to build a user interface where the user can fling left or right to switch between fragments. This versions of the adapter doesn't keep around the fragment instances that ViewPager has destroyed.

http://developer.android.com/resources/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentStatePagerSupport.html

The difference from normal pager is it extends FragmentStatePagerAdapter.

Edit: In the google code you have this in the adapter:

public static class MyAdapter extends FragmentStatePagerAdapter {
    public MyAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public int getCount() {
        return NUM_ITEMS;
    }

    @Override
    public Fragment getItem(int position) {
        return ArrayListFragment.newInstance(position);
    }
}

Notice getItem() implementation. We get a new fragment on demand, so we wont create the fragment before we need it. This stands in contrast to your approach where you create all fragments and add them to an vector. (java prefer arraylists btw.) With your approach the fragments wont be removed from memory as there always will be a reference to the fragments.

Edit 2: To be specific. In following:

@Override
public Fragment getItem(int position) {
    return ArrayListFragment.newInstance(position);
}

you could do something like:

    @Override
    public Fragment getItem(int position) {
if(position == 1) {
return Fragment1.newInstance(position);
} else {
return Fragment2.newInstance(position);
}

    }

这篇关于在片段显示图像内存问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆