我是Android的新手。用户从长时间闲置返回时,我的应用程序崩溃我已阅读Android应用生命周期并调试应用。当用户从长时间闲置状态返回时,看起来我的应用程序在onCreate方法中崩溃。下面是我的onCreate代码。它获得从先前活动设置的全局变量(CustomerName)。然后,从基于CustomerName的SQLLite中提取数据以显示数据。我认为Android VM已经在用户返回活动时终止了该过程,但我认为当onCreate再次调用时该过程应该有效。任何帮助将不胜感激。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_report_review);
intialize();
adddynamiccheckboexes();
//Initialize
metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
panelWidth = (int) ((metrics.widthPixels)*0.75);
headerPanel = (RelativeLayout) findViewById(R.id.header);
headerPanelParameters = (LinearLayout.LayoutParams) headerPanel.getLayoutParams();
headerPanelParameters.width = metrics.widthPixels;
headerPanel.setLayoutParams(headerPanelParameters);
menuPanel = (RelativeLayout) findViewById(R.id.menuPanel);
menuPanelParameters = (FrameLayout.LayoutParams) menuPanel.getLayoutParams();
menuPanelParameters.width = panelWidth;
menuPanel.setLayoutParams(menuPanelParameters);
slidingPanel = (LinearLayout) findViewById(R.id.slidingPanel);
slidingPanelParameters = (FrameLayout.LayoutParams) slidingPanel.getLayoutParams();
slidingPanelParameters.width = metrics.widthPixels;
slidingPanel.setLayoutParams(slidingPanelParameters);
CustomerName = ((GlobalVariables) this.getApplication()).getSomeVariable();
//Check to see if CustName exist. If not, go to Main screen
CustomerName.trim();
if (CustomerName.equals("null") || CustomerName.equals("")){
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
return;
}
assessHelper = new AssessmentDBAdaptor(this);
assessHelper.open();
String fCustomerName = CustomerName;
fCustomerName = fCustomerName.replaceAll("''", "'");
fCustomerName = fCustomerName.replaceAll("'", "''");
Cursor AssessInfo = assessHelper.fetchAssessmentByExactName(fCustomerName);
if (AssessInfo.moveToFirst()){
City = AssessInfo.getString(AssessInfo.getColumnIndexOrThrow("City")).toString();
State = AssessInfo.getString(AssessInfo.getColumnIndexOrThrow("State")).toString();
AssessDate = AssessInfo.getString(AssessInfo.getColumnIndexOrThrow("CreatedDate")).toString();
TA_Name = AssessInfo.getString(AssessInfo.getColumnIndexOrThrow("PresentedBy")).toString();
CustEmail = AssessInfo.getString(AssessInfo.getColumnIndexOrThrow("Email")).toString();
}else{
Toast.makeText(getApplicationContext(), "Error: Unable to retrieve Assessment Information", Toast.LENGTH_SHORT).show();
}
ppHelper = new DBPlantPhoto(this);
ppHelper.open();
fCustomerName = CustomerName;
fCustomerName = fCustomerName.replaceAll("''", "'");
fCustomerName = fCustomerName.replaceAll("'", "''");
Cursor sPP = ppHelper.fetchPlantPhotoByAssessment(fCustomerName);
if (sPP.moveToFirst()){
FileName = sPP.getString(sPP.getColumnIndexOrThrow("FileName")).toString();
}else{
//Toast.makeText(getApplicationContext(), "Error: Unable to retrieve Plant Photo", Toast.LENGTH_SHORT).show();
}
//Tien
dbBulletHelper = new DBSlideBullet(this);
dbBulletHelper.open();
fCustomerName = CustomerName;
fCustomerName = fCustomerName.replaceAll("''", "'");
fCustomerName = fCustomerName.replaceAll("'", "''");
Cursor cAssess = dbBulletHelper.fetchSlideInfobyCateandAssess("Best Practice Summary", fCustomerName);
if (cAssess.moveToFirst()){
//Exist
}else{
//Not Exist
DBSlideBullet entry = new DBSlideBullet(ReportReview.this); //call upon database pass in context of this class
entry.open();
entry.CreateEntry("Best Practice Summary", "Best Practice Summary", "1", " ", "", "" ,"" ,"", "", "", "", "", "", "", "", "", "", "", "", "", CustomerName, "" ); //Create Method in order to pass information into
entry.close();
}
cAssess = dbBulletHelper.fetchSlideInfobyCateandAssess("Current Product Overview", fCustomerName);
if (cAssess.moveToFirst()){
//Exist
}else{
//Not Exist
DBSlideBullet entry = new DBSlideBullet(ReportReview.this); //call upon database pass in context of this class
entry.open();
entry.CreateEntry("Current Product Overview", "Current Product Overview", "1", " ", "", "" ,"" ,"", "", "", "", "", "", "", "", "", "", "", "", "", CustomerName, "" ); //Create Method in order to pass information into
entry.close();
}
cAssess = dbBulletHelper.fetchSlideInfobyCateandAssess("Major Improvement Areas", fCustomerName);
if (cAssess.moveToFirst()){
//Exist
}else{
//Not Exist
DBSlideBullet entry = new DBSlideBullet(ReportReview.this); //call upon database pass in context of this class
entry.open();
entry.CreateEntry("Major Improvement Areas", "Major Improvement Areas - Review", "1", " ", "", "" ,"" ,"", "", "", "", "", "", "", "", "", "", "", "", "", CustomerName, "" ); //Create Method in order to pass information into
entry.close();
}
cAssess = dbBulletHelper.fetchSlideInfobyCateandAssess("Next Steps", fCustomerName);
if (cAssess.moveToFirst()){
//Exist
}else{
//Not Exist
DBSlideBullet entry = new DBSlideBullet(ReportReview.this); //call upon database pass in context of this class
entry.open();
entry.CreateEntry("Next Steps", "Next Steps", "1", " ", "", "" ,"" ,"", "", "", "", "", "", "", "", "", "", "", "", "", CustomerName, "" ); //Create Method in order to pass information into
entry.close();
}
Bundle gotBasket = getIntent().getExtras();
Storage = gotBasket.getStringArray("Storage");
ProductApp = gotBasket.getStringArray("ProductApp");
StandardProc = gotBasket.getStringArray("StandardProc");
Training = gotBasket.getStringArray("Training");
Contamination = gotBasket.getStringArray("Contamination");
OilCondition = gotBasket.getStringArray("OilCondition");
ReliabilityAssessment = gotBasket.getStringArray("ReliabilityAssessment");
Planning = gotBasket.getStringArray("Planning");
//Get User and Customer Email
upHelper = new UserProfileDBAdaptor(this);
upHelper.open();
Cursor cUP = upHelper.fetchAllUserProfile();
cUP.moveToFirst();
UserEmail = cUP.getString(cUP.getColumnIndexOrThrow("Email")).toString();
upHelper.close();
}
答案 0 :(得分:1)
android可能会从内存中丢弃整个应用程序(即如果需要内存)。
这样,变量CustomerName
的内容可能为null,调用trim()方法可能会导致nullpionterexception。
CustomerName = ((GlobalVariables) this.getApplication()).getSomeVariable();
CustomerName.trim();