我已经编写了一个应用程序,在加载意图时,它会在一个特定设备上长时间崩溃。
所以我假设崩溃的原因是这个。在我开始改变应用程序的整个结构之前,我需要一些专家指导。
我将intent中所需的所有控制变量一起创建为字段变量。然后在OnCreate()方法中,我调用一个方法setReference(),它使用引用初始化所有控件变量。有问题的应用程序有200多个控制变量,我在OnCreate()中创建了至少200个控件变量的引用。
我假设这会对内存分配造成很大负担,并且是导致崩溃的原因。 这是代码(首先我将所有控制变量创建为字段变量):
ImageButton ibRep, ibRep2, ibEmail, ibShare;
EditText eName, ed_Sa, ed_Ann, ed_prm, ed_mat, ed_pr_age, ed_asa, ed_asa1, ed_cirsa, ed_term, ed_la, ed_trsa, t_ppt;
EditText ed_age, i_ed_age;
Spinner sp_ttl, spOpt189, spPlans_List, sp_ann_pmt_mode, sp_834_opt, sp_term, sp_ppt, sMode;
LinearLayout llsur_ben, llPlan_List, ll_Mat, ll_Trm, ll_ann, ll_output, ll_bas_input, ll_calc_ch,
ll_sa_b_fab, ll_xtra_ben, ll, ll_temp1, ll_temp2;
TextView hdMoney_Back, tvSur_ben1, tvSur_ben2, tvSur_ben3, tvSur_ben4, tvSur_ben5,tvSur_ben6, tv_disclaimer;
TextView tvSur_ben_amt1, tvSur_ben_amt2, tvSur_ben_amt3, tvSur_ben_amt4, tvSur_ben_amt5, tv_term, tv_defper;
TextView tvPrm_Hdr, tv_hd_mat, tv_ermsg1, tv_ermsg2, tv_ermsg3, tv_ermsg4, tv_pr_age, tv_ext_ben, tv_tpp,
tv_maturity, tv_spp_amt, tv_la_amt, tv_sa_amt, tv_mat_sa_per, tv_sa_per_amt, tv_bonus, tv_fab,
tv_sa, tv_sa1, tv_mat_sa, /*tv_ydab, tv_hdab, tv_qdab, tv_mdab,*/ tv_age_op, tv_term_op, tv_ppt, tv_ch_amt,
tv_pl_sel, tv_ann_op, tv_ann_pr, tv_y_ann, tv_h_ann, tv_q_ann, tv_m_ann, tv_y_gp, tv_hy_gp, tv_q_gp,
tv_m_gp, tv_d_gp, tv_ytx1, tv_htx1, tv_qtx1, tv_mtx1, tv_dtx1, tv_ytx2, tv_htx2, tv_qtx2, tv_mtx2,
tv_dtx2, tv_ytot1, tv_htot1, tv_qtot1, tv_mtot1, tv_dtot1, tv_ytot2, tv_htot2, tv_qtot2, tv_mtot2,
tv_dtot2, tv_ytpp, tv_htpp, tv_qtpp, tv_mtpp, /*tv422,*/ tv42, tv_tax1, tv_crrep, tv_crrep2, tv_xrisk_cover,
tv_mb_title, tv_mat_disclaimer, tv_prm_disclaimer, tv_ch_repval, tv_834_opt, tv_mode, tv_send_msg;
TableRow trPlan_List, trAnnOpt, trAnOptHd, tr834Opt, tr834Opt_Hd, tr_acc_sa, tr_trsa, tr_pwb, tr_tax2,
tr_netprm2, tr_la, tr_ret_at_mat, tr_spp, tr_la1, tr_sa, tr_mathd,tr_mathd1,tr_mathd2,tr_mathd3,
tr_tax1, tr_acc_sa1, tr_cir, tr_gender, tr_sprm;
RadioButton rd_prm, rd_mat, rd_ann, rd_sa, rd_male, rd_female;
CheckBox chk_addb, chk_tr, chk_pwb, chk_addb_845, chk_ab_845, chk_cir;
Button btn_calc, btn_rep;
ImageButton btn_recalc, btn_recalc1, btn_ch_repval, btn_send_msg;
ScrollView sv1;
ProgressDialog pd;
ProgressBar pb;
然后我在onCreate()方法中调用setReference()方法:
public void setReference() {
try {
sv1 = (ScrollView) findViewById(R.id.sv);
hsv_repInput = (HorizontalScrollView) findViewById(R.id.hsvReportInput);
ibRep = (ImageButton) findViewById(R.id.btnReport);
ibRep2 = (ImageButton) findViewById(R.id.btnReport2);
sp_ttl = (Spinner) findViewById(R.id.spTitle);
spOpt189 = (Spinner) findViewById(R.id.sp189option);
spPlans_List = (Spinner) findViewById(R.id.spPlansList);
sp_ann_pmt_mode = (Spinner) findViewById(R.id.spAnnuityPmtMode);
sp_834_opt = (Spinner) findViewById(R.id.sp834option);
sp_term = (Spinner) findViewById(R.id.spTerm);
sp_ppt = (Spinner) findViewById(R.id.spPPT);
sMode = (Spinner)findViewById(R.id.spMode);
llsur_ben = (LinearLayout) findViewById(R.id.llSurBen);
llPlan_List = (LinearLayout) findViewById(R.id.llPlanList);
ll_Mat = (LinearLayout) findViewById(R.id.llMaturity);
ll_Trm = (LinearLayout) findViewById(R.id.llTerm);
ll_ann = (LinearLayout) findViewById(R.id.llAnn);
ll_output = (LinearLayout) findViewById(R.id.llOutput);
ll_bas_input = (LinearLayout) findViewById(R.id.llBasicInput);
ll_calc_ch = (LinearLayout) findViewById(R.id.llCalcChoice);
ll_sa_b_fab = (LinearLayout) findViewById(R.id.llsabfab);
ll_xtra_ben = (LinearLayout) findViewById(R.id.llExtraBenefit);
ll = (LinearLayout) findViewById(R.id.content_prem_calculator);
ll_temp1 = (LinearLayout) findViewById(R.id.lltemp1);
ll_temp2 = (LinearLayout) findViewById(R.id.lltemp2);
hdMoney_Back = (TextView) findViewById(R.id.hdMoneyBack);
tvSur_ben1 = (TextView) findViewById(R.id.sb1);
tvSur_ben2 = (TextView) findViewById(R.id.sb2);
tvSur_ben3 = (TextView) findViewById(R.id.sb3);
tvSur_ben4 = (TextView) findViewById(R.id.sb4);
tvSur_ben5 = (TextView) findViewById(R.id.sb5);
tvSur_ben6 = (TextView) findViewById(R.id.sb6);
tvSur_ben_amt1 = (TextView) findViewById(R.id.sba1);
tvSur_ben_amt2 = (TextView) findViewById(R.id.sba2);
tvSur_ben_amt3 = (TextView) findViewById(R.id.sba3);
tvSur_ben_amt4 = (TextView) findViewById(R.id.sba4);
tvSur_ben_amt5 = (TextView) findViewById(R.id.sba5);
tvPrm_Hdr = (TextView) findViewById(R.id.tvPremiumHeader);
tv_hd_mat = (TextView) findViewById(R.id.hdMaturity);
tv_mode = (TextView) findViewById(R.id.tvMode);
tv_ermsg1 = (TextView) findViewById(R.id.tvErrMsg1);
tv_ermsg2 = (TextView) findViewById(R.id.tvErrMsg2);
tv_ermsg3 = (TextView) findViewById(R.id.tvErrMsg3);
tv_ermsg4 = (TextView) findViewById(R.id.tvErrMsg4);
tv_pr_age = (TextView) findViewById(R.id.tvPrAge);
tv_ext_ben = (TextView) findViewById(R.id.tvExBen);
tv_disclaimer = (TextView) findViewById(R.id.tvDisclaimer);
tv_term = (TextView) findViewById(R.id.tvTerm);
tv_defper = (TextView) findViewById(R.id.tvDefper);
tv_maturity = (TextView) findViewById(R.id.tvRetAtMAt);
tv_spp_amt = (TextView) findViewById(R.id.tvSppAmt);
tv_la_amt = (TextView) findViewById(R.id.tvLaAmt);
tv_sa_amt = (TextView) findViewById(R.id.tvSaAmt);
tv_mat_sa_per = (TextView) findViewById(R.id.matSaPer);
tv_sa_per_amt = (TextView) findViewById(R.id.tvsaPer);
tv_bonus = (TextView) findViewById(R.id.tvBonus);
tv_fab = (TextView) findViewById(R.id.tvFab);
tv_sa = (TextView) findViewById(R.id.tvsa);
tv_sa1 = (TextView) findViewById(R.id.tvSa1);
tv_mat_sa = (TextView) findViewById(R.id.matSa);
tv_age_op = (TextView) findViewById(R.id.tvAgeOp);
tv_term_op = (TextView) findViewById(R.id.tvTermOp);
tv_ppt = (TextView) findViewById(R.id.tvPPT);
tv_ch_amt = (TextView) findViewById(R.id.tvChoiceAmt);
tv_pl_sel = (TextView) findViewById(R.id.tvPlanSelected);
tv_tax1 = (TextView) findViewById(R.id.tvTax1);
tv_ann_op = (TextView) findViewById(R.id.tAnnOption);
tv_ann_pr = (TextView) findViewById(R.id.tannPrem);
tv_y_ann = (TextView) findViewById(R.id.tYlyAnn);
tv_h_ann = (TextView) findViewById(R.id.tHlyAnn);
tv_q_ann = (TextView) findViewById(R.id.tQlyAnn);
tv_m_ann = (TextView) findViewById(R.id.tMlyAnn);
tv_y_gp = (TextView) findViewById(R.id.ygp);
tv_hy_gp = (TextView) findViewById(R.id.hygp);
tv_q_gp = (TextView) findViewById(R.id.qgp);
tv_m_gp = (TextView) findViewById(R.id.mgp);
tv_d_gp = (TextView) findViewById(R.id.dgp);
tv_ytx1 = (TextView) findViewById(R.id.ytx1);
tv_htx1 = (TextView) findViewById(R.id.hytx1);
tv_qtx1 = (TextView) findViewById(R.id.qtx1);
tv_mtx1 = (TextView) findViewById(R.id.mtx1);
tv_dtx1 = (TextView) findViewById(R.id.dtx1);
tv_ytx2 = (TextView) findViewById(R.id.ytx2);
tv_htx2 = (TextView) findViewById(R.id.hytx2);
tv_qtx2 = (TextView) findViewById(R.id.qtx2);
tv_mtx2 = (TextView) findViewById(R.id.mtx2);
tv_dtx2 = (TextView) findViewById(R.id.dtx2);
tv_ytot1 = (TextView) findViewById(R.id.ytot1);
tv_htot1 = (TextView)findViewById(R.id.hytot1);
tv_qtot1 = (TextView)findViewById(R.id.qtot1);
tv_mtot1 = (TextView)findViewById(R.id.mtot1);
tv_dtot1 =(TextView)findViewById(R.id.dtot1);
tv_ytot2 = (TextView) findViewById(R.id.ytot2);
tv_htot2 = (TextView)findViewById(R.id.hytot2);
tv_qtot2 = (TextView)findViewById(R.id.qtot2);
tv_mtot2 = (TextView)findViewById(R.id.mtot2);
tv_dtot2 =(TextView)findViewById(R.id.dtot2);
tv_ytpp = (TextView) findViewById(R.id.yTotPremPaid);
tv_htpp = (TextView) findViewById(R.id.hyTotPremPaid);
tv_qtpp = (TextView) findViewById(R.id.qTotPremPaid);
tv_mtpp = (TextView) findViewById(R.id.mTotPremPaid);
tv42 = (TextView) findViewById(R.id.t42);
tv_tpp = (TextView) findViewById(R.id.tvtpp);
tv_xrisk_cover = (TextView) findViewById(R.id.tvExtraRiskCover);
tv_mb_title = (TextView) findViewById(R.id.tvMoneyBackTitle);
tv_mat_disclaimer = (TextView) findViewById(R.id.tvMatDisclaimer);
tv_prm_disclaimer = (TextView) findViewById(R.id.tvPrmDisclaimer);
tv_ch_repval = (TextView) findViewById(R.id.txtChRepValues);
tv_834_opt = (TextView) findViewById(R.id.tv834Option);
tv_send_msg = (TextView) findViewById(R.id.txtSendMessage);
btn_ch_repval =(ImageButton) findViewById(R.id.btnChRepValues);
btn_send_msg = (ImageButton) findViewById(R.id.btnSendMessage);
trPlan_List = (TableRow) findViewById(R.id.trPlanList);
trAnnOpt = (TableRow) findViewById(R.id.trAnnOption);
trAnOptHd = (TableRow) findViewById(R.id.trAnnOptHd);
tr834Opt = (TableRow) findViewById(R.id.tr834Option);
tr834Opt_Hd = (TableRow) findViewById(R.id.tr834OptHd);
tr_acc_sa = (TableRow) findViewById(R.id.trAccSA);
tr_acc_sa1 = (TableRow) findViewById(R.id.trAccSA1);
tr_trsa = (TableRow) findViewById(R.id.trTRSA);
tr_pwb = (TableRow) findViewById(R.id.trpwb);
tr_cir = (TableRow) findViewById(R.id.trCirSa);
tr_tax1 = (TableRow) findViewById(R.id.trTax1);
tr_tax2 = (TableRow) findViewById(R.id.trTax2);
tr_netprm2 = (TableRow) findViewById(R.id.trNetPrem2);
tr_la = (TableRow) findViewById(R.id.trLA);
tr_ret_at_mat = (TableRow) findViewById(R.id.trRetAtMat);
tr_spp = (TableRow) findViewById(R.id.trSpp);
tr_sa = (TableRow) findViewById(R.id.trSa);
tr_la1 = (TableRow) findViewById(R.id.trla1);
tr_mathd = (TableRow) findViewById(R.id.trMathd);
tr_mathd1 = (TableRow) findViewById(R.id.trMatHd1);
tr_mathd2 = (TableRow) findViewById(R.id.trMatHd2);
tr_mathd3 = (TableRow) findViewById(R.id.trMathd3);
tr_gender = (TableRow) findViewById(R.id.trGender);
t_ppt = (EditText) findViewById(R.id.tPPT);
ed_Sa = (EditText) findViewById(R.id.edtSA);
ed_Ann = (EditText) findViewById(R.id.edtAnnuity);
ed_prm = (EditText) findViewById(R.id.edtPremium);
ed_mat = (EditText) findViewById(R.id.edtMaturity);
eName = (EditText) findViewById(R.id.edtName);
ed_pr_age = (EditText) findViewById(R.id.edtPra);
ed_asa = (EditText) findViewById(R.id.edtASA);
ed_term = (EditText) findViewById(R.id.edtTerm);
ed_la = (EditText) findViewById(R.id.edLA);
ed_trsa = (EditText) findViewById(R.id.edtTRSA);
ed_asa1 = (EditText) findViewById(R.id.edtASA1);
ed_cirsa = (EditText) findViewById(R.id.edtCir);
ed_annuity_premium = (EditText) findViewById(R.id.edtAnnuityPremium);
rd_prm = (RadioButton) findViewById(R.id.rdPremium);
rd_mat = (RadioButton) findViewById(R.id.rdMaturity);
rd_ann = (RadioButton) findViewById(R.id.rdAnnuity);
rd_sa = (RadioButton) findViewById(R.id.rdSA);
rd_male = (RadioButton) findViewById(R.id.rdMale);
rd_female = (RadioButton) findViewById(R.id.rdFemale);
chk_addb = (CheckBox) findViewById(R.id.chkADDB);
chk_tr = (CheckBox) findViewById(R.id.chkTR);
chk_pwb = (CheckBox) findViewById(R.id.chkpwb);
chk_cir = (CheckBox) findViewById(R.id.chkCir);
chk_ab_845 = (CheckBox) findViewById(R.id.chkAB_845);
chk_addb_845 = (CheckBox) findViewById(R.id.chkADDB_845);
btn_calc = (Button) findViewById(R.id.btnCalc);
btn_recalc = (ImageButton) findViewById(R.id.btnRecalc);
btn_recalc1 = (ImageButton) findViewById(R.id.btnRecalc1);
tv_crrep = (TextView) findViewById(R.id.txtCreateReport);
tv_crrep2 = (TextView) findViewById(R.id.txtCreateReport2);
pd = new ProgressDialog(this);
}catch(Exception e){
Toast.makeText(this, "15 sr: "+ e.toString(), Toast.LENGTH_SHORT).show();
}
}
当然,所有变量都不是一直使用,而是被创造,引用,因此可能占用太多空间。当应用程序很小并且不依赖于这么多控制变量时,这可能是一个很好的做法。
我是否认为这是一种错误的做法导致其在某些设备上崩溃。
如果是,那么我应该在每个方法中创建控制变量,并在各个方法中设置引用。
答案 0 :(得分:0)
应该没有问题。您只是存储已创建的对象的引用。
但在单个活动中使用大量观点可能会有问题。