Java:ArrayList没有De / Serializing正确

时间:2013-06-18 19:52:53

标签: java android

我正在创建一个应用程序,您可以在其中创建将随机选择的列表。在这个应用程序中,我在列表中选择一个随机条目。该应用程序将ArrayList序列化为txt文件。我正在创建它反序列化ArrayList的部分,并使用该信息打开一个新活动。问题是它打开了那个活动。我相信它与它有关,而不是正确的序列化。 Anyhelp会很棒,谢谢!

BTW它取消排除的原因是反序列化的ArrayList是空的。

这是第一个Java文件:

package com.frostbytedev.randomgenie;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Steven on 6/11/13.
 */
public class NewList extends Activity implements Serializable, View.OnClickListener{
    String ListName;
    String ItemText;
    int i = 0;

    List<String> List = new ArrayList<String>();
    Button save;
    EditText FileName, etItem1, etItem2, etItem3, etItem4, etItem5;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.newlist);
        initilize();
    }

    private void IndexList() {
        List.add(FileName.getText().toString());
        List.add(etItem1.getText().toString());
        List.add(etItem2.getText().toString());
        List.add(etItem3.getText().toString());
        List.add(etItem4.getText().toString());
        List.add(etItem5.getText().toString());

        for(i=1;i<5;i++){
            ItemText = List.get(i);
            if(ItemText.contentEquals("")){
                List.remove(List.get(i));
            }
        }

    }

    private void initilize() {
        save = (Button)findViewById(R.id.bSave);
        FileName = (EditText)findViewById(R.id.etFileName);
        etItem1 = (EditText)findViewById(R.id.etItem1);
        etItem2 = (EditText)findViewById(R.id.etItem2);
        etItem3 = (EditText)findViewById(R.id.etItem3);
        etItem4 = (EditText)findViewById(R.id.etItem4);
        etItem5 = (EditText)findViewById(R.id.etItem5);
        save.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch(view.getId()){

            case R.id.bSave:
                IndexList();
                try {
                    SaveList();
                } catch (IOException e) {
                    e.printStackTrace();
                }


        }
    }


    private void SaveList() throws IOException {
        String filename = FileName.getText().toString()+".txt";
        FileOutputStream fos;
        try {
            fos = openFileOutput(filename,Context.MODE_PRIVATE);
            ObjectOutputStream out = new ObjectOutputStream(fos);
            out.writeObject(List);
            out.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }
    }
}

第二个:

package com.frostbytedev.randomgenie;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Steven on 6/15/13.
 */
public class ListSelect extends ListActivity implements Serializable {
    List<String> List = new ArrayList<String>();
    List<String> textFiles = new ArrayList<String>();
    List<String> ListStrings = new ArrayList<String>();

    List<String> textFiles(String directory) {
        File dir = new File(directory);
        for (File file : dir.listFiles()) {
            if (file.getName().endsWith((".txt"))) {
                textFiles.add(file.getName().replace(".txt", ""));
            }
        }
        return textFiles;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, textFiles("data/data/com.frostbytedev.randomgenie/files")));
    }
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
            String FileName = textFiles.get(position);
        deserialize(FileName);
        Bundle b = new Bundle();
        Intent OpenList = new Intent(this, ListRandom.class);
        OpenList.putExtra("ListItem1",List.get(1));
        OpenList.putExtra("ListItem2", List.get(2));
        OpenList.putExtra("ListItem3", List.get(3));
        OpenList.putExtra("ListItem4",List.get(4));
        OpenList.putExtra("ListItem5", List.get(5));
        startActivity(OpenList);

    }

    private void deserialize(String filename) {
        FileInputStream fis = null;
        ObjectInputStream in = null;
        ObjectOutputStream out = null;
        try {
            fis = new FileInputStream(filename);
            in = new ObjectInputStream(fis);
            List = (ArrayList<String>) in.readObject();
            out.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }
}

LogCat:

06-19 00:00:53.497    8447-8447/com.frostbytedev.randomgenie   E/AndroidRuntime: FATAL EXCEPTION: main
        java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0
        at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
        at java.util.ArrayList.get(ArrayList.java:304)
        at com.frostbytedev.randomgenie.ListSelect.onListItemClick(ListSelect.java:44)
        at android.app.ListActivity$2.onItemClick(ListActivity.java:319)
        at android.widget.AdapterView.performItemClick(AdapterView.java:298)
        at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
        at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
        at android.widget.AbsListView$1.run(AbsListView.java:3423)
        at android.os.Handler.handleCallback(Handler.java:725)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5041)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
        at dalvik.system.NativeStart.main(Native Method)

任何帮助都会很棒!

2 个答案:

答案 0 :(得分:0)

方法deserialize 不在堆栈跟踪上,因此空列表FileName中的错误行。

String FileName = textFiles.get(position);

答案 1 :(得分:0)

从文件系统中读取文件时,您剪切了txt扩展名:

textFiles.add(file.getName().replace(".txt", ""))

单击该项目时,使用该文件名对列表进行反序列化。原始文件是使用txt扩展名编写的,因此找不到文件并且反序列化失败,列表为空,List.get(1)将抛出IndexOutOfBoundsException。 你能做的是:

fis = new FileInputStream(filename+".txt");

我很确定你的logcat输出在IndexOutOfBoundsException之上也有一个FileNotFoundException。

顺便说一句,将变量命名为方法是一个非常糟糕的主意。如果变量名为textFiles,则使用getTextFiles()作为方法。