2012-03-19 44 views
0

離開一個活動後使用onBackPressed()方法返回給它,我得到一個空指針異常。在onResume()調用後刷新onCreate()

活動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) 

的活性中的代碼是在這裏:

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(); 

回答

1

在家庭類,你可以設置你的所有method.So這Example.May是它爲你工作。

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

,或者您可以使用startActivityForResult()

+1

不知道爲什麼這個工作。(我是一個相對較新的程序員)。但它的確如此。謝謝 – gurpsone 2012-03-19 23:39:52

+0

因爲當你把「a」活動切換到「b」活動,然後再從「b」活動切換到「a」活動時,它被稱爲onResume方法。 – 2012-03-20 05:01:46

相關問題