1

内部ストレージに保存するためのコードに何らかの論理的な問題があります。ペットのインスタンスを保存およびロードしようとしている場所で、クラス pet に load および save 用の 2 つのメソッドを作成しました。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));
            }
        }
                );
    }
}
4

1 に答える 1