I am developing an Android App that allows the user to save their data (input and output) to a file, say on an external SD Card. Rather than use multiple 'save' buttons, it would be nice if this can be done automatically with a checkbox 'tick' and the user notified of successful data saved to a file via a toast. The user can choose when to save their data. The code (with suitable XML files) compiles to an APK package, but the program crashes on a real device. 

Also, it seems that in a simpler version with only one Add method, the checkbox would only allow one input/output entry, not repeated entries as desired if ticked. 

I wonder whether the statement `CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox1);` needs to be declared as a class statement? Thanks for any help with all this!

```
// import etc. goes here
public class MainActivity extends Activity
{
    private static final int READ_BLOCK_SIZE = 100;
    final Context context = this;
    private Button button;
    //initialize objects as class objects
    TextView result = (TextView) findViewById(R.id.result);
    EditText et1 = (EditText) findViewById(R.id.editText1);
    TextView result2 = (TextView) findViewById(R.id.result2);
    EditText et2 = (EditText) findViewById(R.id.editText2);

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

        button = (Button) findViewById(R.id.buttonShowCustomDialog);

        //add button listener
        button.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View arg0)
            {

                // custom dialog
                final Dialog dialog = new Dialog(context);
                dialog.setContentView(R.layout.custom);
                dialog.setTitle("About My App");

                // set the custom dialog components - text, image and button
                TextView text = (TextView) dialog.findViewById(R.id.text);
                text.setText("This App works!");
                ImageView image = (ImageView) dialog.findViewById(R.id.image);
                image.setImageResource(R.drawable.ic_launcher);

                Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
                // if button is clicked, close the custom dialog
                dialogButton.setOnClickListener(new OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        dialog.dismiss();
                    }
                });

                dialog.show();
            }

        });

    }

    //This method is called when we click on add button
    //and it is declare in main XML file in button tag
    //we have to pass View object in this method

    public void add(View v)
    {

        // in a simpler application the objects et1, result were initialised locally

        String Test = et1.getText().toString();
        if (Test.trim().equals(""))
        {
            Toast.makeText(MainActivity.this, "Please input a number", Toast.LENGTH_SHORT).show();
            return;
        }

        //get text from edit text boxes and convert into double
        double a = Double.parseDouble(String.valueOf(et1.getText()));

        //some maths,  whatever you like to use....
        double b = (a + 100) / 5;

        result.setText("Answer:" + b);

        //---CheckBox---
        CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox1);
        checkBox.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                String strA = et1.getText().toString();
                String strB = result.getText().toString();
                String strC = "Data: " + strA + " " + strB + " ";
                if (((CheckBox) v).isChecked())

                    try
                    {
                        File sdCard = Environment.getExternalStorageDirectory();
                        File directory = new File(sdCard.getAbsolutePath() + "/MyFiles");
                        if (!directory.exists())
                        {
                            directory.mkdirs();
                        }

                        File file = new File(directory, "textfile.txt");
                        if (!file.exists())
                        {
                            file.createNewFile();
                        }
                        FileOutputStream fOut = new FileOutputStream(file, true);

                        OutputStreamWriter osw = new OutputStreamWriter(fOut);

                        //---write the string to the file---
                        osw.append(strC);
                        osw.flush();
                        osw.close();

                        //---display file saved message---
                        Toast.makeText(MainActivity.this, "Data added", Toast.LENGTH_SHORT).show();

                    }
                    catch (IOException ioe)
                    {
                        ioe.printStackTrace();
                    }

            }

        });

    }

    public void add2(View v)
    {

        String TestB = et2.getText().toString();
        if (TestB.trim().equals(""))
        {
            Toast.makeText(MainActivity.this, "Please input a number", Toast.LENGTH_SHORT).show();
            return;
        }

        // get text from edit text boxes and convert into double
        double a2 = Double.parseDouble(String.valueOf(et2.getText()));

        double b2 = (a2 * 100) / 5;

        result2.setText("Answer:" + b2);

        //---CheckBox---
        CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox1);
        checkBox.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                String strD = et2.getText().toString();
                String strE = result.getText().toString();
                String strF = "Data 2: " + strD + " " + strE + " ";
                if (((CheckBox) v).isChecked())

                    try
                    {
                        File sdCard = Environment.getExternalStorageDirectory();
                        File directory = new File(sdCard.getAbsolutePath() + "/MyFiles");
                        if (!directory.exists())
                        {
                            directory.mkdirs();
                        }

                        File file = new File(directory, "textfile.txt");
                        if (!file.exists())
                        {
                            file.createNewFile();
                        }
                        FileOutputStream fOut = new FileOutputStream(file, true);

                        OutputStreamWriter osw = new OutputStreamWriter(fOut);

                        //---write the string to the file---
                        osw.append(strF);
                        osw.flush();
                        osw.close();

                        //---display file saved message---
                        Toast.makeText(MainActivity.this, "Data added", Toast.LENGTH_SHORT).show();

                    }
                    catch (IOException ioe)
                    {
                        ioe.printStackTrace();
                    }

            }

        });

    }
}

```

Changes made accordingly. Thanks for the tips. In response, here is the LogCat output on the Device emulator after entering a number in the application, checkbox tick and clicking the GUI button (the application crashes): 

```
12-19 05:41:31.810: D/dalvikvm(1110): GC_FOR_ALLOC freed 39K, 4% free 3132K/3256K, paused 61ms, total 64ms
12-19 05:41:31.830: I/dalvikvm-heap(1110): Grow heap (frag case) to 4.207MB for 1127536-byte allocation
12-19 05:41:31.900: D/dalvikvm(1110): GC_FOR_ALLOC freed 2K, 3% free 4230K/4360K, paused 59ms, total 59ms
12-19 05:41:32.820: D/(1110): HostConnection::get() New Host Connection established 0xb8269b38, tid 1110
12-19 05:41:33.010: W/EGL_emulation(1110): eglSurfaceAttrib not implemented
12-19 05:41:33.020: D/OpenGLRenderer(1110): Enabling debug mode 0
12-19 05:41:33.220: I/Choreographer(1110): Skipped 43 frames!  The application may be doing too much work on its main thread.
12-19 05:41:34.270: D/dalvikvm(1110): GC_FOR_ALLOC freed 56K, 3% free 4687K/4820K, paused 374ms, total 375ms
12-19 05:42:14.450: W/EGL_emulation(1110): eglSurfaceAttrib not implemented
12-19 05:42:17.250: W/EGL_emulation(1110): eglSurfaceAttrib not implemented
12-19 05:42:20.220: D/AndroidRuntime(1110): Shutting down VM
12-19 05:42:20.220: W/dalvikvm(1110): threadid=1: thread exiting with uncaught exception (group=0xb1affb90)
12-19 05:42:20.330: E/AndroidRuntime(1110): FATAL EXCEPTION: main
12-19 05:42:20.330: E/AndroidRuntime(1110): Process: com.example.tester, PID: 1110
12-19 05:42:20.330: E/AndroidRuntime(1110): java.lang.IllegalStateException: Could not execute method of the activity
12-19 05:42:20.330: E/AndroidRuntime(1110):     at android.view.View$1.onClick(View.java:3814)
12-19 05:42:20.330: E/AndroidRuntime(1110):     at android.view.View.performClick(View.java:4424)
12-19 05:42:20.330: E/AndroidRuntime(1110):     at android.view.View$PerformClick.run(View.java:18383)
12-19 05:42:20.330: E/AndroidRuntime(1110):     at android.os.Handler.handleCallback(Handler.java:733)
12-19 05:42:20.330: E/AndroidRuntime(1110):     at android.os.Handler.dispatchMessage(Handler.java:95)
12-19 05:42:20.330: E/AndroidRuntime(1110):     at android.os.Looper.loop(Looper.java:137)
12-19 05:42:20.330: E/AndroidRuntime(1110):     at android.app.ActivityThread.main(ActivityThread.java:4998)
12-19 05:42:20.330: E/AndroidRuntime(1110):     at java.lang.reflect.Method.invokeNative(Native Method)
12-19 05:42:20.330: E/AndroidRuntime(1110):     at java.lang.reflect.Method.invoke(Method.java:515)
12-19 05:42:20.330: E/AndroidRuntime(1110):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
12-19 05:42:20.330: E/AndroidRuntime(1110):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
12-19 05:42:20.330: E/AndroidRuntime(1110):     at dalvik.system.NativeStart.main(Native Method)
12-19 05:42:20.330: E/AndroidRuntime(1110): Caused by: java.lang.reflect.InvocationTargetException
12-19 05:42:20.330: E/AndroidRuntime(1110):     at java.lang.reflect.Method.invokeNative(Native Method)
12-19 05:42:20.330: E/AndroidRuntime(1110):     at java.lang.reflect.Method.invoke(Method.java:515)
12-19 05:42:20.330: E/AndroidRuntime(1110):     at android.view.View$1.onClick(View.java:3809)
12-19 05:42:20.330: E/AndroidRuntime(1110):     ... 11 more
12-19 05:42:20.330: E/AndroidRuntime(1110): Caused by: java.lang.NullPointerException
12-19 05:42:20.330: E/AndroidRuntime(1110):     at com.example.tester.MainActivity.add(MainActivity.java:106)
12-19 05:42:20.330: E/AndroidRuntime(1110):     ... 14 more

```