0

i am now developing an android app which need to export database to CSV file and write it on the SD Card or Local phone storage. But when i try to run my method, i got this error :

11-06 10:41:44.266 2340-2340/? E/WindowManager﹕ android.view.WindowLeaked: Activity com.gook.ever_ncn.cashflow.ExportOrImport has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{6b26f41 V.E..... R......D 0,0-1026,348} that was originally added here at android.view.ViewRootImpl.(ViewRootImpl.java:363) at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:271) at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85) at android.app.Dialog.show(Dialog.java:298) at com.gook.ever_ncn.cashflow.ExportOrImport$ExportDatabaseCSVTask.onPreExecute(ExportOrImport.java:96) at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:591) at android.os.AsyncTask.execute(AsyncTask.java:539) at com.gook.ever_ncn.cashflow.ExportOrImport.onClick(ExportOrImport.java:52) at android.view.View.performClick(View.java:4780) at android.view.View$PerformClick.run(View.java:19866) 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:5257) 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:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

I don't know exactly what happen and what it's mean, i have searching on stackoverflow.com but still the answer that i found doesn't solve my case.

Please master help me. Thanks Before.

Edit : My question is different than the other one, because here my error log give some different report.

NB. this my code for the Activity :

public class ExportOrImport extends Activity implements View.OnClickListener {
Button btnImport;
Button btnExport;
private static final String TAG="ExportOrImport";
File file=null;
IODatabaseHelper dbHelper=null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_export_or_import);
    btnImport = (Button) findViewById(R.id.btnImportCSV);
    btnImport.setOnClickListener(this);
    btnExport = (Button)findViewById(R.id.btnExportCSV);
    btnExport.setOnClickListener(this);
}

@Override
public void onClick(View arg0) {

    Log.v(TAG, "onClick called");
    if (arg0 == btnExport) {

        ExportDatabaseCSVTask task = new ExportDatabaseCSVTask();
        task.execute();
    } else if (arg0 == btnImport) {


        File exportDir = new File(Environment.getExternalStorageDirectory(), "");
        if (!exportDir.exists()) {
            exportDir.mkdirs();
        }

        file = new File(exportDir, "Database.csv");
        try {
            CSVReader reader = new CSVReader(new FileReader(file));
            String[] nextLine;
            try {
                while ((nextLine = reader.readNext()) != null) {

                    // nextLine[] is an array of values from the line

                    String transname = nextLine[0];
                    String amount = nextLine[1];
                    String transtype = nextLine[2];
                    String transdate = nextLine[3];
                    String transdateshow = nextLine[4];
                    String categid = nextLine[5];
                    String _id = nextLine[6];

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

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

private class ExportDatabaseCSVTask extends AsyncTask<String, Void, Boolean> {
    private final ProgressDialog dialog = new ProgressDialog(ExportOrImport.this);

    @Override
    protected void onPreExecute() {

        this.dialog.setMessage("Exporting database...");
        this.dialog.show();

    }
    protected Boolean doInBackground(final String... args){

        File dbFile=getDatabasePath("database.db");
        Log.v(TAG, "Db path is: " + dbFile);  //get the path of db

        File exportDir = new File(Environment.getExternalStorageDirectory(), "");
        if (!exportDir.exists()) {
            exportDir.mkdirs();
        }

        file = new File(exportDir, "Database.csv");
        try {

            file.createNewFile();
            CSVWriter csvWrite = new CSVWriter(new FileWriter(file));

            //ormlite core method
            List<Transaction> listdata=dbHelper.GetDataPerson();
            Transaction transaction=null;

            // this is the Column of the table and same for Header of CSV file
            String arrStr1[] ={"TransID", "TransName",
                    "TransType", "TransDate", "TransDateShow",
                    "CategId", "_id"};
            csvWrite.writeNext(arrStr1);

            if(listdata.size() > 1)
            {
                for(int index=0; index < listdata.size(); index++)
                {
                    transaction=listdata.get(index);
                    String arrStr[] ={transaction.getTransid(),
                            transaction.getTransname(),
                            transaction.getTranstype(),
                            transaction.getTransdate(),
                            transaction.getTransdateshow(),
                            transaction.getCategid(),
                            transaction.get_id()};
                    csvWrite.writeNext(arrStr);
                }
            }
            // sqlite core query

            /* SQLiteDatabase db = DBob.getReadableDatabase();
            //Cursor curCSV=mydb.rawQuery("select * from " + TableName_ans,null);
            Cursor curCSV = db.rawQuery("SELECT * FROM table_ans12",null);
            csvWrite.writeNext(curCSV.getColumnNames());

           while(curCSV.moveToNext())  {

                String arrStr[] ={curCSV.getString(0),curCSV.getString(1)};
                curCSV.getString(2),curCSV.getString(3),curCSV.getString(4)
                csvWrite.writeNext(arrStr);

            }
       */
            csvWrite.close();
            return true;
        }
        catch (IOException e){
            Log.e("MainActivity", e.getMessage(), e);
            return false;
        }
    }

    @Override
    protected void onPostExecute(final Boolean success) {

        if (this.dialog.isShowing()){
            this.dialog.dismiss();
        }
        if (success){
            Toast.makeText(ExportOrImport.this, "Export successful!", Toast.LENGTH_SHORT).show();
        }
        else {
            Toast.makeText(ExportOrImport.this, "Export failed!", Toast.LENGTH_SHORT).show();
        }
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_export_or_import, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

Main Bareng
  • 31
  • 1
  • 9
  • Possible duplicate of [Activity has leaked window that was originally added](http://stackoverflow.com/questions/2850573/activity-has-leaked-window-that-was-originally-added) – Rajesh Jadav Nov 06 '15 at 03:55

1 Answers1

1

In your onPostExecute where you call Toast.maketext, you are using ExportOrImport.this which could be of OnClickListener type. Create a Context variable (context) and assign it in your onCreate method, context=this then use:

Toast.makeText(context, "Export Succesful!", Toast.LENGTH_SHORT).show();
Avezou
  • 91
  • 1
  • 6
  • but i remove that my dialog.show( ); and now it's work but not with dialog msg appear. – Main Bareng Nov 06 '15 at 04:56
  • You are having the same issue I pointed out in the response above. You are initializing your dialog with ExportOrImport.this, you need to use the same context variable you I mentioned in the response to avoid this error. – Avezou Nov 09 '15 at 18:17