title: Copying database to SD card (java.lang.IllegalStateException)
body: <p>I have created a function that copies the database from the data/data folder of my app but I keep getting force closes when I use the function CopyDatabase().</p>
  
  <p>Here is my code binded to the button</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>public class StudentsFormScreen extends Activity
  {
      SQLiteDatabase db;
  
      @Override
      public void onCreate(Bundle savedInstanceState)
      {
          super.onCreate(savedInstanceState);
          getActionBar().hide();
          getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
          setContentView(R.layout.studentsform_layout);
          db=openOrCreateDatabase("MyDB1",MODE_WORLD_WRITEABLE, null);
          db.execSQL("CREATE TABLE IF NOT EXISTS Student(fname VARCHAR,lname VARCHAR,email VARCHAR);");
  
          String path = db.getPath();
  
          TextView path1 = (TextView) findViewById(R.id.pather);
          path1.setText(path);
      }
  
  
      public void CopyDatabase()
      {
          try
          {
              File sd = Environment.getExternalStorageDirectory();
              File data = Environment.getDataDirectory();
  
              if (sd.canWrite())
              {
                  String currentDBPath = "/data/test.com.classmanagertest/databases/MyDB1.db";
                  String backupDBPath = "MyDB1.db";
                  File currentDB = new File(data,currentDBPath);
                  File backupDB = new File(sd, backupDBPath);
  
                  if (currentDB.exists())
                  {
                      FileChannel src = new FileInputStream(currentDB).getChannel();
                      FileChannel dst = new FileOutputStream(backupDB).getChannel();
  
                      dst.transferFrom(src, 0, src.size());
                      src.close();
                      dst.close();
                      Toast.makeText(getApplicationContext(),"Backup Complete", Toast.LENGTH_SHORT).show();
                  }
  
              }
          }
  
          catch (Exception e)
          {
              e.printStackTrace();
              displayExceptionMessage(e.getMessage());
          }
  
      }
  
      public void displayExceptionMessage(String msg)
      {
          Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
      }
  
  /*   public void CopyDatabase()
      {
  
          DataBaseHelper myDbHelper = new DataBaseHelper(this);
  
          try
          {
              myDbHelper.createDataBase();
          }
          catch (IOException ioe)
          {
              throw new Error("Unable to create database");
          }
  
          try
          {
              myDbHelper.openDataBase();
          }
  
          catch (SQLException e)
          {
              e.printStackTrace();
          }
  
      }
  
  */
      public void CreateScreen(View view)
      {
          Intent StudentCreateScreenIntent = new Intent(this, StudentsFormCreate.class);
  
          startActivity(StudentCreateScreenIntent);
  
          overridePendingTransition(R.anim.transition,R.anim.right2left);
      }
  
      public void UpdateScreen (View view)
      {
          Intent StudentUpdateScreenIntent = new Intent(this, StudentsFormUpdate.class );
  
          startActivity(StudentUpdateScreenIntent);
  
          overridePendingTransition(R.anim.transition,R.anim.right2left);
      }
  
      public void DeleteScreen (View view)
      {
          Intent StudentDeleteScreenIntent = new Intent (this, StudentsFormDelete.class);
  
          startActivity(StudentDeleteScreenIntent);
  
          overridePendingTransition(R.anim.transition,R.anim.right2left);
      }
  
      public void ShowData(View view)
      {
          Cursor c=db.rawQuery("SELECT * from Student", null);
          int count= c.getCount();
          c.moveToFirst();
          TableLayout tableLayout = new TableLayout(getApplicationContext());
          tableLayout.setVerticalScrollBarEnabled(true);
          TableRow tableRow;
          TextView textView,textView1,textView2,textView3,textView4,textView5;
          tableRow = new TableRow(getApplicationContext());
          textView=new TextView(getApplicationContext());
          textView.setText("Firstname");
          textView.setTextColor(Color.RED);
          textView.setTypeface(null, Typeface.BOLD);
          textView.setPadding(20, 20, 20, 20);
          tableRow.addView(textView);
          textView4=new TextView(getApplicationContext());
          textView4.setText("LastName");
          textView4.setTextColor(Color.RED);
          textView4.setTypeface(null, Typeface.BOLD);
          textView4.setPadding(20, 20, 20, 20);
          tableRow.addView(textView4);
          textView5=new TextView(getApplicationContext());
          textView5.setText("Email");
          textView5.setTextColor(Color.RED);
          textView5.setTypeface(null, Typeface.BOLD);
          textView5.setPadding(20, 20, 20, 20);
          tableRow.addView(textView5);
          tableLayout.addView(tableRow);
          for (Integer j = 0; j &lt; count; j++)
          {
              tableRow = new TableRow(getApplicationContext());
              textView1 = new TextView(getApplicationContext());
              textView1.setText(c.getString(c.getColumnIndex("fname")));
              textView2 = new TextView(getApplicationContext());
              textView2.setText(c.getString(c.getColumnIndex("lname")));
              textView3 = new TextView(getApplicationContext());
              textView3.setText(c.getString(c.getColumnIndex("email")));
              textView1.setPadding(20, 20, 20, 20);
              textView2.setPadding(20, 20, 20, 20);
              textView3.setPadding(20, 20, 20, 20);
              tableRow.addView(textView1);
              tableRow.addView(textView2);
              tableRow.addView(textView3);
              tableLayout.addView(tableRow);
              c.moveToNext() ;
          }
          setContentView(tableLayout);
          db.close();
      }
  
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>Here is the error I'm getting:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>01-23 20:53:07.539  32699-32699/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
      Process: test.com.classmanagertest, PID: 32699
      java.lang.IllegalStateException: Could not find a method CopyDatabase(View) in the activity class test.com.classmanagertest.StudentsFormScreen for onClick handler on view class android.widget.Button with id 'CopyDatabase'
              at android.view.View$1.onClick(View.java:3994)
              at android.view.View.performClick(View.java:4756)
              at android.view.View$PerformClick.run(View.java:19749)
              at android.os.Handler.handleCallback(Handler.java:739)
              at android.os.Handler.dispatchMessage(Handler.java:95)
              at android.os.Looper.loop(Looper.java:135)
              at android.app.ActivityThread.main(ActivityThread.java:5221)
              at java.lang.reflect.Method.invoke(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:372)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
       Caused by: java.lang.NoSuchMethodException: CopyDatabase [class android.view.View]
              at java.lang.Class.getMethod(Class.java:664)
              at java.lang.Class.getMethod(Class.java:643)
              at android.view.View$1.onClick(View.java:3987)
              at android.view.View.performClick(View.java:4756)
              at android.view.View$PerformClick.run(View.java:19749)
              at android.os.Handler.handleCallback(Handler.java:739)
              at android.os.Handler.dispatchMessage(Handler.java:95)
              at android.os.Looper.loop(Looper.java:135)
              at android.app.ActivityThread.main(ActivityThread.java:5221)
              at java.lang.reflect.Method.invoke(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:372)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)</code></pre>
  </div>
  </div>
  </p>
  
  <p>I have the permission:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>        &lt;uses-permission
              android:name = "android.permission.WRITE_EXTERNAL_STORAGE"/&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>Any help is highly appreciated!</p>