调用onResume()后刷新onCreate()

时间:2012-03-19 18:50:30

标签: java android database sqlite countdowntimer

在离开活动并稍后使用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();

2 个答案:

答案 0 :(得分:1)

在家庭课程中你可以设置你的ALL方法。所以这个例子。可能对你有用。

public void onResume() {
super.onResume();
referenceXML();
hudSetup();
listenerSetup();
}

或者您可以使用startActivityForResult()

答案 1 :(得分:0)

您是否尝试过使用startActivityForResult()而不是startActivity()?

请参阅Using onResume() to refresh activity