Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从内部存储(Android)中保存的文件中保存(和加载)Arraylist?_Android_Arraylist_Fileinputstream_Objectinputstream_Internal Storage - Fatal编程技术网

如何从内部存储(Android)中保存的文件中保存(和加载)Arraylist?

如何从内部存储(Android)中保存的文件中保存(和加载)Arraylist?,android,arraylist,fileinputstream,objectinputstream,internal-storage,Android,Arraylist,Fileinputstream,Objectinputstream,Internal Storage,我想请这里的人帮我解决一个问题,这个问题似乎有一个非常简单的解决方案,但应用程序在模拟器中一次又一次地崩溃 练习- 创建包含三个活动的应用程序: 1.主要的第一个活动包括两个按钮,分别单击它们 操作其他活动之一。 2.第二个活动允许您输入客户信息: 客户名称,是否为男性/女性及其年龄 并包含一个要添加的按钮-单击该按钮将向ArrayList添加客户。 退出活动时-需要将所有客户阵列列表添加到文件中。 3.第三个活动,允许用户查看现有客户如何从文件中读取总计->数据 实现可序列化的Cusomer类

我想请这里的人帮我解决一个问题,这个问题似乎有一个非常简单的解决方案,但应用程序在模拟器中一次又一次地崩溃

练习- 创建包含三个活动的应用程序: 1.主要的第一个活动包括两个按钮,分别单击它们 操作其他活动之一。 2.第二个活动允许您输入客户信息: 客户名称,是否为男性/女性及其年龄 并包含一个要添加的按钮-单击该按钮将向ArrayList添加客户。 退出活动时-需要将所有客户阵列列表添加到文件中。 3.第三个活动,允许用户查看现有客户如何从文件中读取总计->数据

实现可序列化的Cusomer类, 主活动和ViewCustomers活动都非常容易编写。 AddCustomer活动导致应用程序崩溃。 请告诉我,为什么应用程序会根据第二个恼人活动的以下代码反复崩溃:

 import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.*;
    import java.io.*;
    import java.util.ArrayList;

    public class AddCustomerActivity extends Activity {
    static final String CUSTOMERSFILE="customers";
    ArrayList<Customer> customers;
    EditText name, age;
    CheckBox male;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.addcustomerscreen);
        name=(EditText)findViewById(R.id.nameText);
        age=(EditText)findViewById(R.id.ageText);
        male=(CheckBox)findViewById(R.id.maleCheckBox);
        Button add=(Button)findViewById(R.id.addcustomerbutton);

        FileInputStream fis;
        ObjectInputStream ois=null;
        try {
            fis=openFileInput(CUSTOMERSFILE);
            ois=new ObjectInputStream(fis);
            customers=(ArrayList<Customer>)ois.readObject();
            if(customers==null)
                customers=new ArrayList<Customer>();
        }
        catch(ClassNotFoundException ex)
        {
            Log.e("add customers","serialization problem",ex);
        }
        catch(IOException ex)
        {
            Log.e("add customers","No customers file",ex);
            customers=new ArrayList<Customer>();
        }
        finally
        {
            try {
                ois.close();
            } catch (IOException e) {
                e.printStackTrace();
            }       
        }

        add.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                String theName=name.getText().toString();
                int theAge=Integer.parseInt(age.getText().toString());
                boolean isMale=male.isChecked();
                customers.add(new Customer(theName, theAge, isMale));
                name.setText("");
                age.setText("");
            }
        });

    }

    @Override
    protected void onPause() {
        super.onPause();
        FileOutputStream fos;
        ObjectOutputStream ous=null;
        try {
            fos=openFileOutput(CUSTOMERSFILE, Activity.MODE_PRIVATE);
            ous=new ObjectOutputStream(fos);
            ous.writeObject(customers);
        }

        catch(IOException ex)
        {
            Log.e("add customers","some problem",ex);
        }
        finally
        {
            try {
                ous.close();
            } catch (IOException e) {
                e.printStackTrace();
            }       
        }
    }



    }   
附言。 OnPause中的代码应该在内部存储器中创建文件 并使代码正常工作,至少在第二次运行时是这样,但事实并非如此

U P D A T E因请求LogCat而到期 那些是日志, 而第一条红色\错误线出现在按下按钮的右侧,该按钮假定是出于明确的意图 从ManuActivity添加到AddCustomerActivity, 但随后应用程序崩溃了

AddCustomerActivity开头显示的错误日志链接:

A部分

乙部
您的崩溃是由线路引起的

ois.close();
因为在这个场景中,当您仍然没有要读取的文件时,您从未初始化ois,这意味着它是空的。 我建议用一个简单的if来解决这个问题:

try {
    if(ois != null) {
        ois.close();
    }
} catch(IOException e) {
...

你能把你收到的日志发出去吗?当然,我刚刚发了。谢谢。你发布的图片没有包含导致崩溃的错误。您可以在图像未捕获处理程序的末尾找到该错误:线程主线程正在退出。。。你能为那个错误贴日志吗?天哪,你完全正确!对于这样一个令人筋疲力尽的问题,这是一个小小的解决方案。添加客户错误日志是相同的,但是应用程序没有崩溃,而是崩溃了!输入AddCustomerActivity并完美运行!非常感谢:P.S.-虽然添加客户错误日志是相同的-但是,它们最终只出现一次!。。。你确实启发了我:很高兴我能帮忙。你可以接受我的回答作为回报