2

I have used the code tip from here to copy a pre-filled data file to the target and handled this in a asynch task.

On starting the application it gives error and shuts down first time, starting again without any change it works perfectly fine. So first time after the file is copied, the error comes but after that no issues.

private class CopyDatabase extends AsyncTask<String, Void, Boolean> {
    private final ProgressDialog dialog = new ProgressDialog(BabyNames.this);
    protected void onPreExecute() {
        this.dialog.setMessage("Loading...");
        this.dialog.show();
    }

    @Override
    protected Boolean doInBackground(String... params) {
        // TODO Auto-generated method stub
        try {
            namesDBSQLHelper.createDatabase();
            return null;
        } catch(IOException ioe){
            ioe.printStackTrace();
        }
        return null;

    }

    protected void onPostExecute(final Boolean success){
        if (this.dialog.isShowing()){
            this.dialog.dismiss();
        }
    }
}
Kara
  • 6,115
  • 16
  • 50
  • 57
Thinkcomplete
  • 181
  • 2
  • 7
  • 1
    What error do you see first time in your Log Cat? – Pentium10 Jun 02 '10 at 07:20
  • How/when are you invoking your asynctask? I'm guessing it's running before the context is fully initialized (which is required to initialize the db in your namesDBSQLHelper.createDatabase() method). This would happen if for example you start this task in an activity's constructor instead of onCreate() – Ricardo Villamil Jun 02 '10 at 18:00
  • @ Ricardo : The code attached is called in an activity class and is called in onCreate() method, .... * public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); namesDBSQLHelper = new NamesDataSQLHelper(this); new CopyDatabase().execute(); * So, I am not calling it through the constructor. @Pentium10: The Logcat details of error are: 06-03 18:34:53.056: ERROR/AndroidRuntime(217): Java.lang.IllegalStateException: Could not execute method of the activity 06-03 18:34:53.056: ERROR/AndroidRuntime(217 – Thinkcomplete Jun 03 '10 at 13:09
  • 06-03 18:34:53.056: ERROR/AndroidRuntime(217): java.lang.IllegalStateException: Could not execute method of the activity 06-03 18:34:53.056: ERROR/AndroidRuntime(217): at android.view.View$1.onClick(View.java:2031) 06-03 18:34:53.056: ERROR/AndroidRuntime(217): at android.view.View.performClick(View.java:2364) 06-03 18:34:53.056: ERROR/AndroidRuntime(217): at android.view.View.onTouchEvent(View.java:4179) 06-03 18:34:53.056: ERROR/AndroidRuntime(217): at android.widget.TextView.onTouchEvent(TextView.java:6532) 06-03 18:34:53.056: ERROR/AndroidRuntime(217): at android.view. – Thinkcomplete Jun 03 '10 at 13:10
  • Huh this sounds complicate. Can you post the launch section of the Activity. There is something going wrong when you click on a view. – Pentium10 Jun 03 '10 at 13:24
  • @Pentium10 - Here is the code on Button Click. And actually the error comes when I click the button first time after data load. Second time I start the app, when data file is already copied, no error, it runs perfectly... *public void myClickHandler(View view){ if(view.getId() == R.id.getnames){ SQLiteDatabase db = namesDBSQLHelper.getReadableDatabase(); if(curr_names !=null && (!curr_names.isClosed())){ curr_names.close(); curr_names = null; } if(table!=null) table.clear(); – Thinkcomplete Jun 07 '10 at 07:29
  • if(db!=null && db.isOpen()){ curr_names = db.query(NamesDataSQLHelper.TABLE, new String[]{"Name", "Meaning"}, " NAME LIKE "+"'"+csAlphabet+"%"+"'"+" AND ORIGIN LIKE "+"'"+csOrigin+"'", null, null, null, null);} int count = curr_names.getCount(); table = new Bundle(); table.putString("alphabet", csAlphabet.toString()); table.putString("origin", csOrigin.toString()); if(count>0 && curr_names.moveToFirst() ){ int i = curr_names.getCount(); int j=1; if(i>0){ String lsNames = "names"; Log.d("CURSOR COUNT i ",new Integer(i).toString()); – Thinkcomplete Jun 07 '10 at 07:34
  • do { table.putString(lsNames+j, curr_names.getString(0).toUpperCase()+"#"+curr_names.getString(1)); j++;}while (curr_names.moveToNext());}} if(count<=0){ /*Calling another intent to display results*/ Intent no_names = new Intent(ShowData.this, DialogActivity.class); startActivity(no_names); } – Thinkcomplete Jun 07 '10 at 07:35
  • I don't spot nothing unusual. – Pentium10 Jun 07 '10 at 09:46

1 Answers1

0

Try copying database by this way.It looks same as the answer of the link provided by you but it has some difference. Database not copying from assets

Community
  • 1
  • 1
Jaydeep Khamar
  • 5,975
  • 3
  • 32
  • 30