将arrayList <class>写入文件</class>

时间:2014-05-20 20:09:50

标签: java android file-io arraylist

我是Android应用程序开发的新手,我尝试将一个arrayList写入一个文件,但在我的代码中尝试执行此操作时遇到了问题:

public class AddActivity extends Activity implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = 1L;
public transient Context ctx;
private final String filename = "Accounts";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add);

    final EditText name = (EditText) findViewById(R.id.name);
    final EditText username = (EditText) findViewById(R.id.username);
    final EditText password = (EditText) findViewById(R.id.password);
    final EditText type = (EditText) findViewById(R.id.type);
    Button add = (Button) findViewById(R.id.add);

    add.setOnClickListener(new View.OnClickListener() {
        @SuppressWarnings("unchecked")
        public void onClick(View arg0) {
            String n = name.getText().toString();
            String u = username.getText().toString();
            String p = password.getText().toString();
            String t = type.getText().toString();
            Account newAccount = new Account (n, u , p, t);
            ArrayList<Account> accounts;
            String filePath = getFilesDir().getPath().toString() + "/" + filename;
            File fl = new File(filePath);
            try {
                if (fl.exists()){
                    FileInputStream fis = ctx.openFileInput(filename);
                    ObjectInputStream oi = new ObjectInputStream(fis);
                    accounts = (ArrayList<Account>) oi.readObject();        
                    accounts.add(newAccount);
                    oi.close();
                    fis.close();
                    fl.delete();
                }else{
                    accounts = new ArrayList<Account>();
                    accounts.add(newAccount);
                }
                fl.createNewFile();
                FileOutputStream fos = (openFileOutput(filename, Context.MODE_PRIVATE));
                ObjectOutputStream oos = new ObjectOutputStream(fos);
                oos.writeObject(accounts);
                oos.flush();
                oos.close();
                fos.close();

            } catch (IOException e) {
                Log.e("InternalStorage", e.getMessage()); 
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }

        }


    });

}

}

我的logCat说:

05-20 18:56:23.782: E/AndroidRuntime(2242): FATAL EXCEPTION: main
05-20 18:56:23.782: E/AndroidRuntime(2242): Process: com.chenw15.accountmanager, PID: 2242
05-20 18:56:23.782: E/AndroidRuntime(2242): java.lang.NullPointerException    
05-20 18:56:23.782: E/AndroidRuntime(2242):     at com.chenw15.accountmanager.AddActivity$1.onClick(AddActivity.java:56)
05-20 18:56:23.782: E/AndroidRuntime(2242):     at android.view.View.performClick(View.java:4438)
05-20 18:56:23.782: E/AndroidRuntime(2242):     at android.view.View$PerformClick.run(View.java:18422)
05-20 18:56:23.782: E/AndroidRuntime(2242):     at android.os.Handler.handleCallback(Handler.java:733)
05-20 18:56:23.782: E/AndroidRuntime(2242):     at android.os.Handler.dispatchMessage(Handler.java:95)
05-20 18:56:23.782: E/AndroidRuntime(2242):     at android.os.Looper.loop(Looper.java:136)
05-20 18:56:23.782: E/AndroidRuntime(2242):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-20 18:56:23.782: E/AndroidRuntime(2242):     at java.lang.reflect.Method.invokeNative(Native Method)
05-20 18:56:23.782: E/AndroidRuntime(2242):     at java.lang.reflect.Method.invoke(Method.java:515)
05-20 18:56:23.782: E/AndroidRuntime(2242):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-20 18:56:23.782: E/AndroidRuntime(2242):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-20 18:56:23.782: E/AndroidRuntime(2242):     at dalvik.system.NativeStart.main(Native Method)

每次点击添加按钮时,都会收到错误消息“不幸的是,(应用程序名称)已停止”。请帮忙。感谢。

1 个答案:

答案 0 :(得分:0)

我想你也可以尝试下面的代码来编写文件中的列表:

File myfile = getFileStreamPath(FileName);
    try {
        if (myfile.exists() || myfile.createNewFile()) {
            FileOutputStream fos = openFileOutput(FileName, MODE_PRIVATE);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(masterlistrev);
            fos.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }