问题
After leaving an activity and later returning to it using the onBackPressed() method I am getting a null pointer exception.
Activity A links to Activity B. When going back to A i am getting the following errors.
03-19 18:36:19.213: E/AndroidRuntime(441): FATAL EXCEPTION: main
03-19 18:36:19.213: E/AndroidRuntime(441): java.lang.NullPointerException
03-19 18:36:19.213: E/AndroidRuntime(441): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
03-19 18:36:19.213: E/AndroidRuntime(441): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118)
03-19 18:36:19.213: E/AndroidRuntime(441): at com.gurpswu.gurps.Player.open(Player.java:65)
03-19 18:36:19.213: E/AndroidRuntime(441): at com.gurpswu.gurps.Home.refresh(Home.java:85)
03-19 18:36:19.213: E/AndroidRuntime(441): at com.gurpswu.gurps.Crime.onBackPressed(Crime.java:295)
The code for Activity A is here:
package com.gurpswu.gurps;
public class Home extends Activity {
Button crime, missions, bank,hospital, travel, bossVisit, weapons;
TextView name, city, energy, health, cash, rank,countdown;
public String characterName, cityName;
public int playerHealth,playerEnergy,playerRank,playerCash;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
referenceXML();
hudSetup();
listenerSetup();
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
countdown.setText("seconds remaining: " + millisUntilFinished
/ 1000);
}
public void onFinish() {
countdown.setText("Energy replenished.");
start();
}
}.start();
if (playerHealth <= 0) {
Intent death = new Intent(Home.this, Death.class);
startActivity(death);
finish();
}
}
public void referenceXML() {
countdown = (TextView) findViewById(R.id.tvCountdownTimer);
crime = (Button) findViewById(R.id.bCrime);
//gamble = (Button) findViewById(R.id.bGamble);
bank = (Button) findViewById(R.id.bBank);
hospital = (Button) findViewById(R.id.bHospital);
bossVisit = (Button) findViewById(R.id.bBossVisit);
travel = (Button) findViewById(R.id.bTravel);
name = (TextView) findViewById(R.id.TextViewName);
city = (TextView) findViewById(R.id.TextViewCity);
weapons = (Button) findViewById(R.id.bWeapons);
energy = (TextView) findViewById(R.id.TextViewEnergy);
health = (TextView) findViewById(R.id.TextViewHealth);
cash = (TextView) findViewById(R.id.TextViewCash);
rank = (TextView) findViewById(R.id.TextViewRank);
}
public void hudSetup() {
Player stats = new Player(this);
stats.open();
String playerName = stats.getStringField(stats.KEY_NAME);
String playerCity = stats.getStringField(stats.KEY_CITY);
playerHealth = stats.getIntField(stats.KEY_HEALTH);
playerEnergy = stats.getIntField(stats.KEY_ENERGY);
playerRank = stats.getIntField(stats.KEY_RANK);
playerCash = stats.getIntField(stats.KEY_CASH);
stats.close();
name.setText("Name: " + playerName);
city.setText("City: " + playerCity);
energy.setText("Energy:" + playerEnergy);
health.setText("Health:" + playerHealth);
cash.setText("Cash: $" + playerCash);
rank.setText("Rank:" + playerRank);
}
public void onPause() {
super.onPause();
}
public void onResume() {
super.onResume();
}
public void listenerSetup() {
bank.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent openBank = new Intent(Home.this, Bank.class);
startActivity(openBank);
}
});
crime.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent openCrime = new Intent(Home.this, Crime.class);
startActivity(openCrime);
}
});
hospital.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent openHospital = new Intent(Home.this, Hospital.class);
startActivity(openHospital);
}
});
bossVisit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent openBoss = new Intent(Home.this, VisitTheBoss.class);
startActivity(openBoss);
//finish();
}
});
weapons.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent openWeapons = new Intent(Home.this, Weapons.class);
startActivity(openWeapons);
}
});
travel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent openTravel = new Intent(Home.this, Travel.class);
startActivity(openTravel);
}
});
}
}
The onBackPressed code in activity B is here:
Home h = new Home();
h.hudSetup();
finish();
回答1:
In home class you can set your ALL method.So this Example.May be it's work for you.
public void onResume() {
super.onResume();
referenceXML();
hudSetup();
listenerSetup();
}
or you can used startActivityForResult()
回答2:
Have you tried using startActivityForResult() instead of startActivity()?
See Using onResume() to refresh activity
来源:https://stackoverflow.com/questions/9776059/refresh-oncreate-after-call-to-onresume