0

我是 Android 编程新手,我在 2 个 Activites 中编写了大量代码,将近 6500 行,当我访问(使用)这些 activites 时,它运行良好,但一段时间后它运行缓慢并崩溃,我在 logcat 中看到了显示无法分配 xxxx(某个值)字节的内存。在活动开始(或完成工作)之前,我已经为我在这些活动中使用过的所有视图初始化了空值,并调用 System.gc(); 但我仍然收到此错误。如何避免这些缺陷。

我的文件中使用的视图数:

EditText et_af_loc;
Spinner spn_astStatus, spn_af_astName;
TableRow tr_af_newAst;
ArrayAdapter<String> adp_status, adp_reason;
Button btn_takePic, btn_save, btn_send, btn_gps;
String gpsData = "";
MyLocationListener listener = null;
LocationManager locationManager;
Location loc = null;
ImageView iv_MEPhoto;
Button btn_METakePhoto;
ProgressDialog progressBar, progDailog;
String SENDING_IMAGE_PATH = "";
Spinner spn_af_area;
String image_PATH = "", image_str, ImgString = "";
boolean photoTaken = false;
ArrayAdapter<String> adp_subCompo, adp_drainExi, adp_platExi;
ProgressDialog progServerHit;
String strResponse, sendingString;
DBHelper dbHelper;
SQLiteDatabase db;
TableRow tr_reason;
Spinner spn_reason;
Spinner spn_asstCate, spn_af_drainExist, spn_af_platFormExist,
        spn_af_cisType;
Button btn_af_Send;
ArrayAdapter<String> adp_asstCate, adp_new, adp_area, adp_cisType, adp_sub,
        adp_soFor;
EditText et_other, et_af_capi;
// String newSourId[];
EditText et_af_noOfFill;
TableRow tr_af_capacity, tr_af_noOfFills, tr_af_drainExist,
        tr_af_platExist, tr_af_capaLPM, tr_af_disCharge, tr_af_loc,
        tr_af_areaCoverage, tr_af_make, tr_af_yeildLMP, tr_af_cistern;

EditText et_af_capiLmp, et_af_disCharge, et_af_make, et_af_yeildLMP;

TableRow tr_af_sub, tr_af_MiSsTankCapi, tr_af_soFor;
Spinner spn_af_sourSubType, spn_af_soFor;
TableRow tr_af_sourHeading;
LinearLayout ll_af_sourL, ll_af_sourR;
ArrayList<String> allChkBox;

TableRow tr_af_diaOfBwell, tr_af_deptOfBoreWell, tr_af_noOfGIpipes,
        tr_af_drainCondition, tr_af_watQualPara, tr_af_dtComOfStr,
        tr_af_pumpSetCap, tr_af_pmpSetDesi, tr_af_pmpSetDesHead,
        tr_af_pmpingHrs, tr_af_InfiWellDia, tr_af_InfiWellDepth,
        tr_af_InfiGalLenDia, tr_af_InfiGalLoc, tr_af_InTakStrDiame,
        tr_af_InTakStrDepth, tr_af_OtClWatQualPar, tr_af_IntSumpCap;

EditText et_af_MiSsTankCapi, et_af_diaOfBorewell, et_af_deptOfBwell,
        et_af_noOfGIpipes, et_af_wQualPara, et_af_dtOfComStr,
        et_af_pumpSetCap, et_af_pumpSetDes, et_af_pumpSetDesHead,
        et_af_pumpHrs, et_af_InfiWellDia, et_af_InfiWellDepth,
        et_af_InfiGalLenDia, et_af_InfiGalLoc, et_af_InTakStrDiame,
        et_af_InTakStrDepth, et_af_OtClWatQualPar, et_af_IntSumpCap;
Spinner spn_af_drainCondi;
String sourFrmToFlag;
TextView tv_af_gpsLati, tv_af_gpsLongi;
int count = 0;
String newACCount, newSCCount;
String newAssetCode, newSourceCode, district_id, sourAstID;
String HAB_ID;

TableRow tr_tacassCode_pc, tr_tacassCode_dd, tr_tacassCode_ph,
        tr_tacassCode_dh;
EditText et_tac_psCapInHP, et_tac_psDesDisch, et_tac_psDesHead,
        et_tac_psPumpingHrs;
CheckBox cb_ps;
Spinner spn_tac_subMer;
ArrayAdapter<String> adp_subMer = null, adp_goodBad;
TableRow tr_af_rsFil, tr_af_ssFil, tr_af_clorniation, tr_af_ariea,
        tr_af_miFil, tr_af_claFil, tr_tacassCode_subMer;
String psAvi = "NA", uid;
CheckBox cb_af_rsFil, cb_af_ssFil, cb_af_clori, cb_af_ariea, cb_af_miFil,
        cb_af_claFic;
String cbRsFill = "", cbSsFill = "", cbClori = "", cbAriea = "",
        cbmiFill = "", cbClafi = "";
String otherMandLink = "NA";
EditText et_af_REMARKS;

String ASSET_CODE_GEN, SOURCE_CODE_GEN;
String mandalID;
String panchID, todayDate;

Spinner spn_af_linking;
ArrayAdapter<String> adp_linking;

TableRow tr_af_linking, tr_tacassCode_ssOW, tr_tacassCode_ssHp,
        tr_tacassCode_ssSubSurAss;

// openwell
RadioGroup rg_owIsThereLat10mOfWell, rg_owIsTheLatHighGrThanWell,
        rg_owIsTherSouOfPollWithin10mOfWell,
        rg_IsTherDrainPoorCausStWat2mOfWell, rg_owIsTherFaultDrainChannel,
        rg_owIsTherAdeqParapet, rg_owIsThereCemeFloorLesThan1m,
        rg_owAreWallOfWellInadwqSealed, rg_owAreTherAnyCraksOnCemFloor,
        rg_owAreTheRopeAndBucket, rg_owIsTheWelCovered;

RadioButton rg_owIsThereLat10mOfWell1, rg_owIsTheLatHighGrThanWell1,
        rg_owIsTherSouOfPollWithin10mOfWell1,
        rg_IsTherDrainPoorCausStWat2mOfWell1,
        rg_owIsTherFaultDrainChannel1, rg_owIsTherAdeqParapet1,
        rg_owIsThereCemeFloorLesThan1m1, rg_owAreWallOfWellInadwqSealed1,
        rg_owAreTherAnyCraksOnCemFloor1, rg_owAreTheRopeAndBucket1,
        rg_owIsTheWelCovered1, rg_owIsThereLat10mOfWell2,
        rg_owIsTheLatHighGrThanWell2, rg_owIsTherSouOfPollWithin10mOfWell2,
        rg_IsTherDrainPoorCausStWat2mOfWell2,
        rg_owIsTherFaultDrainChannel2, rg_owIsTherAdeqParapet2,
        rg_owIsThereCemeFloorLesThan1m2, rg_owAreWallOfWellInadwqSealed2,
        rg_owAreTherAnyCraksOnCemFloor2, rg_owAreTheRopeAndBucket2,
        rg_owIsTheWelCovered2;

String owIsThereLat10mOfWell = "NA", owIsTheLatHighGrThanWell = "NA",
        owIsTherSouOfPollWithin10mOfWell = "NA",
        IsTherDrainPoorCausStWat2mOfWell = "NA",
        owIsTherFaultDrainChannel = "NA", owIsTherAdeqParapet = "NA",
        owIsThereCemeFloorLesThan1m = "NA",
        owAreWallOfWellInadwqSealed = "NA",
        owAreTherAnyCraksOnCemFloor = "NA", owAreTheRopeAndBucket = "NA",
        owIsTheWelCovered = "NA";

// handPump
RadioGroup rg_hpIsThereLat10m_mtsOfHP, rg_hpIsTheNeaLatOnHiGr,
        rg_hpIsTheAnyOthSou, rg_hpIsTheDrainPooSta,
        rg_hpIsTheHpDraiChanFaulty, rg_hpIsTheCemFlorles,
        rg_hpIsTherAnyPondingOnCem, rg_hpIsTherAnyCrackOnCem,
        rg_tac_hpIsABuckUsed, rg_hpIsTheHpLooseAtPt,
        rg_hpIsTheCoverOfWelInsani, rg_hpAreTheWalOfWellInadeSea;

RadioButton rg_hpIsThereLat10m_mtsOfHP1, rg_hpIsTheNeaLatOnHiGr1,
        rg_hpIsTheAnyOthSou1, rg_hpIsTheDrainPooSta1,
        rg_hpIsTheHpDraiChanFaulty1, rg_hpIsTheCemFlorles1,
        rg_hpIsTherAnyPondingOnCem1, rg_hpIsTherAnyCrackOnCem1,
        rg_tac_hpIsABuckUsed1, rg_hpIsTheHpLooseAtPt1,
        rg_hpIsTheCoverOfWelInsani1, rg_hpAreTheWalOfWellInadeSea1,

        rg_hpIsThereLat10m_mtsOfHP2, rg_hpIsTheNeaLatOnHiGr2,
        rg_hpIsTheAnyOthSou2, rg_hpIsTheDrainPooSta2,
        rg_hpIsTheHpDraiChanFaulty2, rg_hpIsTheCemFlorles2,
        rg_hpIsTherAnyPondingOnCem2, rg_hpIsTherAnyCrackOnCem2,
        rg_tac_hpIsABuckUsed2, rg_hpIsTheHpLooseAtPt2,
        rg_hpIsTheCoverOfWelInsani2, rg_hpAreTheWalOfWellInadeSea2;

String hpIsThereLat10m_mtsOfHP = "NA", hpIsTheNeaLatOnHiGr = "NA",
        hpIsTheAnyOthSou = "NA", hpIsTheDrainPooSta = "NA",
        hpIsTheHpDraiChanFaulty = "NA", hpIsTheCemFlorles = "NA",
        hpIsTherAnyPondingOnCem = "NA", hpIsTherAnyCrackOnCem = "NA",
        tac_hpIsABuckUsed = "NA", hpIsTheHpLooseAtPt = "NA",
        hpIsTheCoverOfWelInsani = "NA", hpAreTheWalOfWellInadeSea = "NA";

// subsurface assets

RadioGroup rg_tac_ssIsTherLat30m, rg_tac_ssIsTheNearlatUnsewe,
        rg_tac_ssIsTherAnyOthSouWit10m, rg_tac_ssIsTherAnUnCapped,
        rg_tac_ssIsTheDraiAreaPhFaulty, rg_tac_ssIsTheFencAroTheInstDamag,
        rg_tac_ssIsTheFlrOfHpPerTowat, rg_tac_ssIsTheWellSealInsani,
        rg_tac_ssIsTheChlorRecShow, rg_tac_ssIsTheFreeChlResi;

RadioButton rg_tac_ssIsTherLat30m1, rg_tac_ssIsTheNearlatUnsewe1,
        rg_tac_ssIsTherAnyOthSouWit10m1, rg_tac_ssIsTherAnUnCapped1,
        rg_tac_ssIsTheDraiAreaPhFaulty1,
        rg_tac_ssIsTheFencAroTheInstDamag1, rg_tac_ssIsTheFlrOfHpPerTowat1,
        rg_tac_ssIsTheWellSealInsani1, rg_tac_ssIsTheChlorRecShow1,
        rg_tac_ssIsTheFreeChlResi1, rg_tac_ssIsTherLat30m2,
        rg_tac_ssIsTheNearlatUnsewe2, rg_tac_ssIsTherAnyOthSouWit10m2,
        rg_tac_ssIsTherAnUnCapped2, rg_tac_ssIsTheDraiAreaPhFaulty2,
        rg_tac_ssIsTheFencAroTheInstDamag2, rg_tac_ssIsTheFlrOfHpPerTowat2,
        rg_tac_ssIsTheWellSealInsani2, rg_tac_ssIsTheChlorRecShow2,
        rg_tac_ssIsTheFreeChlResi2;

String ssIsTherLat30m = "NA", ssIsTheNearlatUnsewe = "NA",
        ssIsTherAnyOthSouWit10m = "NA", ssIsTherAnUnCapped = "NA",
        ssIsTheDraiAreaPhFaulty = "NA", ssIsTheFencAroTheInstDamag = "NA",
        ssIsTheFlrOfHpPerTowat = "NA", ssIsTheWellSealInsani = "NA",
        ssIsTheChlorRecShow = "NA", ssIsTheFreeChlResi = "NA";

CheckBox cb_tacassCode_ssOW, cb_tacassCode_ssHp, cb_tacassCode_ssSubSurAss;
LinearLayout ll_tac_owRbs, ll_tac_hpRbs, ll_tac_ssRbs;

String owSanataryExisting = "NA", hpSanataryExisting = "NA",
        ssurSanataryExisting = "NA", sanataryValues = "";

TextView tv_af_assetMarker;
TableRow tb_tacassCode_psAvi;

在此之后,我正在使用 findviewbyId(); 阅读视图。然后根据业务逻辑,我隐藏/显示一些视图并使用大量字符串进行验证,并从 EditText 和其他视图读取值以保存数据、捕获图像并将图像转换为字符串

前任:

if(editText.getText().toString().equals("")){
Toast.makeText(this,"Enter Value for xxx",5000).show();
}else if(){...... so on ...!   

最后在完成操作/任务后,我取消所有视图,bitmap=null 并调用 System.gc();。但是应用程序仍然缺乏性能(不响应,崩溃)。

我还能为这段代码做什么?

4

1 回答 1

1

您需要认真地将此代码分解为其他活动或片段,并在您的应用程序中创建适当的流程。您正在将此处的每个对象加载到内存中,并且大多数 Android 设备并未设置为在一个视图中处理整个应用程序的这种大规模分配。

你的问题没有很好的“你如何解决这个问题”的答案。重新考虑您的应用程序的设计,并仅将对象加载到当时需要的内存中。

重复使用同一个微调器并调整其属性,或者在不需要时释放微调器,并在需要时创建新的微调器。不要让视图分配不必要的对象并在需要时以编程方式处理它。例如,通过包含类似代码的片段,并根据需要使用片段管理器交换这些组件视图。

http://developer.android.com/design/patterns/app-structure.html http://developer.android.com/guide/components/fundamentals.html http://developer.android.com/tools/debugging/调试-ui.html

于 2013-11-14T07:19:48.460 回答