0

我在主要活动中执行以下功能

 @Override
 public void onDestroy() {
 super.onDestroy();
 SimpleBookManager.getSimpleBookManager().saveChanges(this);  
}

当 Android 稍后在我关闭应用程序时调用方法函数 saveChanges() 时,我得到“SaveChanges”输出,但不是最后一个打印“After Saving”,这意味着它没有执行整个函数并且我没有得到任何输出打印例外行。你知道为什么吗?我是 android 新手,不明白您是否可以在 onDestroy() 上运行此代码。=)

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.util.ArrayList;

import android.content.Context;

public class SimpleBookManager implements BookManager, java.io.Serializable {

    private ArrayList<Book> allBooks = new ArrayList<Book>();
    private static SimpleBookManager ref;       


    public void saveChanges(Context context){
        try{
            System.out.println("SaveChanges");
            FileOutputStream f_out = context.openFileOutput("myobject.data",Context.MODE_PRIVATE);

            // Write object with ObjectOutputStream
            ObjectOutputStream obj_out = new    ObjectOutputStream (f_out);
        // Write object out to disk
        obj_out.writeObject(allBooks);
        System.out.println("After Saving");

    }catch (FileNotFoundException e) {
        System.out.println("No file found saveChanges");
    } catch (IOException e) {
        System.out.println("IOException in SaveChanges");
    }   
}




public void loadBooks(Context context){
    try{
    // Read from disk using FileInputStream
        FileInputStream f_in = context.openFileInput("myobject.data");

    // Read object using ObjectInputStream
    ObjectInputStream obj_in =  new ObjectInputStream (f_in);

    // Read an object
    Object obj = obj_in.readObject();
    //System.out.println(obj);
    if (obj instanceof ArrayList<?>)
    {
        allBooks = (ArrayList<Book>) obj;

        System.out.println("Worked");
        System.out.println(allBooks);

    }
    }catch (FileNotFoundException e) {
        System.out.println("No file found LoadBooks");
    } catch (IOException e) {


    System.out.println("IOException in loadBooks");
    } 
    catch (ClassNotFoundException e) {
        System.out.println("ClassNotFound in loadBooks");
    }

}

  public static SimpleBookManager getSimpleBookManager()
  {
    if (ref == null){
        ref = new SimpleBookManager();
    }

    return ref;
  }
public SimpleBookManager(){
    Book book1 = createBook();
    book1.setAuthor("RandomBookAuthor");
    book1.setCourse("Kunskap");
    book1.setPrice(199);
    book1.setTitle("Some Random Title");
    book1.setIsbn("9780590353403");
}   

}

在 main 中执行 onCreate() 时,我也会得到 IOException,所以我的加载和保存函数确实有些可疑。

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        SimpleBookManager.getSimpleBookManager().loadBooks(this);
        setContentView(R.layout.activity_action_bar_main);
}

_ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ _ __ _ __ _ __ _ _ __ _ __ _

我已经在清单的最后几行添加了写入存储的权限。

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.av.BookApp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="16" />

    <application
        android:icon="@drawable/ic_launcher"

        >
        <activity
            android:name=".BookApp"
            android:label="@string/title_bookapp"
            android:uiOptions="splitActionBarWhenNarrow" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
            <activity
        android:name=".AddBookActivity"
        android:uiOptions="splitActionBarWhenNarrow" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="edu.chalmers.BookApp" />
    </activity>
     <activity
        android:name=".DetailActivity"
        android:uiOptions="splitActionBarWhenNarrow" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="edu.chalmers.BookApp" />
    </activity>
     <activity
        android:name=".EditBookActivity"
        android:uiOptions="splitActionBarWhenNarrow" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="edu.chalmers.BookApp" />
    </activity>

    </application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
</manifest>

编辑

在我遵循下面的建议之后,我现在在 saveChanges() 中得到一个 IOException

我确实像这样正确设置数据(在 SimpleBookManager 中)

public SimpleBookManager(){
Book book1 = createBook();
    book1.setAuthor("RandomBookAuthor");
    book1.setCourse("Kunskap");
    book1.setPrice(199);
    book1.setTitle("Some Random Title");
    book1.setIsbn("9780590353403");
}
4

2 回答 2

0

重新排序您的onCreate()方法

 @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_action_bar_main);
    SimpleBookManager.getSimpleBookManager().loadBooks(this);
 }

和覆盖onStop()方法和不覆盖onDestroy方法:

 @Override
 public void onStop() {
    super.onStop();
    SimpleBookManager.getSimpleBookManager().saveChanges(this);  
 }
于 2012-11-18T13:41:01.657 回答
0

我认为您将数据保存在 onResume 方法中,以保证系统会在终止进程之前执行操作

于 2012-11-18T13:06:52.457 回答