문제

I would kindly ask someone here to help me in a question which seems to has a very simple solution and yet, the application keeps crashing again and again in the emulator.

Exercise -

Create an application that includes three activities:
1. (Main) First activity includes two buttons that clicking on each of them operates one of the other activities.
2. Second Activity allows you to enter customer information: Customer name, whether it is male / female and his age And includes a button to add - while clicking it will add a Customer to the ArrayList. When you exit from your activity - you need to keep all customer ArrayList added to the file.
3. Third Activity that allows the user to see how existing customers total -> data read from the file.

The "Cusomer" class which implements Serializable, the "Main" Activity and the "ViewCustomers" activity are all pretty easy to write. The "AddCustomer" Activity causes the application to crash. Please, enlighten me as to why the application repeatedly crashes based on the following code of that (annoying) second activity:

 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();
            }       
        }
    }



    }   

P.S. The code in "OnPause" was supposed to create the file at the internal storage and make the code work, at least in the second run, but it doesn't.


  • U P D A T E (due a request for LogCat)

Those are the Logs,
While the first red\error line appears right at the button pressing which suppose to lead (by explicit intent)
To the "AddCustomerActivity" from the "ManuActivity",
But then the application has crashed.

Links to the error logs shown right at the begining of "AddCustomerActivity":

(part A)
http://tinypic.com/r/14brzgn/5

(part B)
http://tinypic.com/r/12546qa/5

도움이 되었습니까?

해결책

You crash is caused by the line

ois.close();

Because in this scenario (when you still don't have a file to read), you never initialized ois - which means it's null. I recommend solving this by wrapping it with a simple if:

try {
    if(ois != null) {
        ois.close();
    }
} catch(IOException e) {
...
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top