2013-05-17 3 views
1

내부 저장소에 저장하기 위해 코드에 논리적 인 문제가 있습니다. 클래스 pet에 두 개의 메소드를로드하여 저장하고 애완 동물의 인스턴스를 저장하고로드하려고합니다. 내가 logcat에서 어떤 오류 메시지를 얻지는 않지만 종료하고 응용 프로그램을 다시 열 때 아무것도 저장되지 않습니다.내부 저장소의 비용 절감

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

잘못된 개체를 저장하고 있습니다. 귀하의 코드는 컨텍스트를 저장하고 애완 동물로 다시로드하려고 시도합니다. 대신

savedPet.writeObject(context.getApplicationContext()); 

당신은

savedPet.writeObject(this); 
+0

오 하나님 감사합니다 일을해야한다! 나는이 일을 영원히하려고 노력했다. 고맙습니다! –

+0

도움이 된 것을 기쁘게 생각합니다. 이 방법이 효과가 있으면 답을 수락하는 것을 잊지 마십시오. – HexAndBugs

관련 문제