离开活动并稍后使用onBackPressed()方法返回活动后,我得到了空指针异常。
活动A链接到活动B。返回A时,出现以下错误。
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)
活动A的代码在这里:
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);
}
});
}
}
活动B中的onBackPressed代码在此处:
Home h = new Home();
h.hudSetup();
finish();
最佳答案
在家庭类中,您可以设置ALL方法,因此本示例可能对您有用。
public void onResume() {
super.onResume();
referenceXML();
hudSetup();
listenerSetup();
}
或者您可以使用startActivityForResult()