2013-05-17 81 views
1

我在保存內部存儲時遇到了一些邏輯問題。 我在類寵物中創建了兩個方法來加載並保存我想要保存並加載寵物實例的地方。我沒有在logcat中得到任何錯誤消息,但是當我退出然後再次打開應用程序時沒有保存任何錯誤消息。內部存儲節省android

package Model; 

import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.ObjectInputStream; 
import java.io.ObjectOutputStream; 
import java.io.Serializable; 


import edu.chl.dat255.sofiase.readyforapet.CreatePet; 
import android.content.Context; 



public class Pet implements Serializable{ 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 
    PetMood petMood = new PetMood(); 
    private int hungerCounter; 
    private int walkCounter; 
    private int playCounter; 



    /** 
    * Method that increases mood bar while eating 
    * 
    * @return String with the pet's reaction 
    */ 
    public String eat() { 
     //walkCounter = petMood.getWalkMood(); 
     hungerCounter = petMood.getFoodMood(); 
     //playCounter = petMood.getPlayMood(); 
     if (hungerCounter < 5) { 
      hungerCounter = hungerCounter + 1; 
      petMood.setFoodMood(hungerCounter); 
      return "Yummie!"; 
     } 

     else{ 
      return "I am full"; 
     } 

    } 

    /** 
    * Method that increases mood bar while walking 
    * and decides that the dog can't walk when it is too hungry or too tired 
    * 
    * @return String with the pet's reaction 
    */ 
    public String walk() { 
     walkCounter = petMood.getWalkMood(); 
     hungerCounter = petMood.getFoodMood(); 
     playCounter = petMood.getPlayMood(); 
     if (hungerCounter < 3 && walkCounter < 5) 
      return "I'm too hungry!"; 
     else if (playCounter + walkCounter > 6) 
      return "I'm tired! I want to rest!"; 
     else if (walkCounter < 5) { 
      walkCounter = walkCounter + 1; 
      petMood.setWalkMood(walkCounter); 
      return "Yeey! Great exercise!"; 
     } 
     else{ 
      return "I'm tired! I want to rest!"; 
     } 

    } 

    /** 
    * Method that increases mood bar while playing 
    * and decides that the dog can't play when it is too hungry or too tired 
    * 
    * @return String with the pet's reaction 
    */ 
    public String play() { 
     walkCounter = petMood.getWalkMood(); 
     hungerCounter = petMood.getFoodMood(); 
     playCounter = petMood.getPlayMood(); 
     if (playCounter + walkCounter > 6) { 
      return "I'm tired! I want to rest!"; 
     } 
     else if (hungerCounter <3 && playCounter < 5) 
      return "I'm too hungry!"; 
     else if (playCounter < 5) { 
      playCounter = playCounter + 1; 
      petMood.setPlayMood(playCounter); 
      return "Yeey! Lots of fun!"; 
     } 
     else{ 
      return "I'm tired! I want to rest!"; 
     } 

    } 

    public void save(String FILENAME, Context context) throws FileNotFoundException, IOException{ 
     FileOutputStream fos = context.openFileOutput(FILENAME, Context.MODE_PRIVATE); 
     ObjectOutputStream savedPet = new ObjectOutputStream(fos); 
     savedPet.writeObject(context.getApplicationContext()); 
     savedPet.close(); 
    } 

    public static Pet load(String FILENAME, Context context) throws FileNotFoundException, IOException, ClassNotFoundException{ 
     FileInputStream fis = context.openFileInput(FILENAME); 
     ObjectInputStream ois = new ObjectInputStream(fis); 
     Pet pet = (Pet) ois.readObject(); 
     ois.close(); 
     CreatePet.setPet(pet); 
     return pet; 
    } 

    } 

    package edu.chl.dat255.sofiase.readyforapet; 

import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.io.Serializable; 

import Model.Dog; 
import Model.Pet; 
import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 

import android.widget.Button; 
import android.widget.EditText; 

public class CreatePet extends Activity implements OnClickListener, Serializable { //lagt till interface serializivble. kanske inte n�dv�ndigt 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 
    String petName; 
    private static Dog dog; 
    String FILENAME = "pet_file.dat";//lagts till f�r nullpointerexeption 

    /** 
    * onCreate Method 
    * 
    * 
    * @param savedInstanceState - Bundle 
    */ 
    @Override 
    protected void onCreate (Bundle savedInstanceState) { 
     super.onCreate (savedInstanceState); 
     setContentView(R.layout.createpet); 


     Button create = (Button) findViewById(R.id.puppy_settings); 
     create.setOnClickListener(this); 
    } 

    public void onClick (View v){ 
     startActivity(new Intent(CreatePet.this, PetActivity.class)); 
     EditText setName = (EditText) findViewById(R.id.edit_pet_name); 
     petName = setName.getText().toString(); 
     dog = new Dog(petName); 

     try { 
      dog.save("pet_file.dat", this); 
     } catch (FileNotFoundException e) { 
      System.out.print("File not found kastad i CreatePet"); 
      e.printStackTrace(); 
     } catch (IOException e) { 
      System.out.print("IOException kastad i CreatePet"); 
      e.printStackTrace(); 
     } 
    } 

    /** 
    * getPet Method 
    * 
    * makes the created pet available to other classes 
    * 
    * @return dog - an instance of the class Dog 
    */ 
    public static Pet getPet(){ 
     return dog; 
    } 

    /** 
    * getPet Method 
    * 
    * makes the created pet available to other classes 
    * 
    * @return dog - an instance of the class Dog 
    */ 
    public static void setPet(Pet pet){ 
     dog = (Dog) pet; 
    } 

} 

    package edu.chl.dat255.sofiase.readyforapet; 


import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.io.Serializable; 

import Model.Pet; 

import Model.Dog; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 



public class SelectGame extends Activity implements Serializable {// la till f�r att objektet m�ste vara serializible 
    private static final long serialVersionUID = 1L; 
    TextView failMessage; 
    String FILENAME = "pet_file.dat";// lgts till f�r nullpointerexep 




    /** 
    * onCreate method 
    * 
    * @param savedInstanceState - Bundle 
    */ 
    @Override 
    protected void onCreate (Bundle savedInstanceState) { 
     super.onCreate (savedInstanceState); 
     setContentView(R.layout.selectgame); 


     //The continue button reacts to a click and starts PetActivity 
     Button continuePreviousGame = (Button) findViewById(R.id.continuegame); 
     continuePreviousGame.setOnClickListener(new OnClickListener() { 

      /** 
      * Method onClick for the continue previous game button 
      * 
      * @param v - View 
      */ 
      public void onClick (View v){ 

        try { 
        Pet.load("pet_file.dat",SelectGame.this); 
        } catch (FileNotFoundException e) { 
        System.out.print("File not found "); 
        e.printStackTrace(); 
       } catch (IOException e) { 
        System.out.print("IO Exception "); 
        e.printStackTrace(); 
       } catch (ClassNotFoundException e) { 
        System.out.print("Class not found exception "); 
        e.printStackTrace(); 
       } 

       if (CreatePet.getPet() != null){ 
        startActivity(new Intent(SelectGame.this, PetActivity.class));  
       } 
       else{ 
        failMessage = (TextView) findViewById(R.id.failmessage); 
        failMessage.setText("Create a pet first!"); 

       } 
      } 
     } 

       ); 


     //To send the button CreateNewPet to the activity CreatePet 
     Button createNewPet = (Button) findViewById(R.id.createnewpet); 
     createNewPet.setOnClickListener(new OnClickListener() { 
      /** 
      * Method onClick for the create new pet button 
      * 
      * @param v - View 
      */ 
      public void onClick (View v){ 
       startActivity(new Intent(SelectGame.this, CreatePet.class)); 
      } 
     } 
       ); 
    } 
} 

回答

1

您正在保存錯誤的對象。您的代碼會保存一個上下文並嘗試將其重新加載爲Pet。取而代之的

savedPet.writeObject(context.getApplicationContext()); 

你應該做

savedPet.writeObject(this); 
+0

哦,感謝上帝!我試圖讓這個工作永遠。謝謝! –

+0

很高興有幫助。如果這有效,請記住接受答案。 – HexAndBugs