【问题标题】:Weird Error on "com.android.internal.policy.impl.PhoneWindow$"“com.android.internal.policy.impl.PhoneWindow$”上的奇怪错误
【发布时间】:2015-11-06 03:51:35
【问题描述】:

我现在正在开发一个需要将数据库导出为 CSV 文件并将其写入 SD 卡或本地手机存储的 Android 应用程序。 但是当我尝试运行我的方法时,我得到了这个错误:

11-06 10:41:44.266 2340-2340/? E/WindowManager﹕android.view.WindowLeaked: Activity com.gook.ever_ncn.cashflow.ExportOrImport 已泄露窗口 com.android.internal.policy.impl.PhoneWindow$DecorView{6b26f41 VE.... R......最初在此处添加的 D 0,0-1026,348} 在 android.view.ViewRootImpl.(ViewRootImpl.java:363) 在 android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:271) 在 android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85) 在 android.app.Dialog.show(Dialog.java:298) 在 com.gook.ever_ncn.cashflow.ExportOrImport$ExportDatabaseCSVTask.onPreExecute(ExportOrImport.java:96) 在 android.os.AsyncTask.executeOnExecutor(AsyncTask.java:591) 在 android.os.AsyncTask.execute(AsyncTask.java:539) 在 com.gook.ever_ncn.cashflow.ExportOrImport.onClick(ExportOrImport.java:52) 在 android.view.View.performClick(View.java:4780) 在 android.view.View$PerformClick.run(View.java:19866) 在 android.os.Handler.handleCallback(Handler.java:739) 在 android.os.Handler.dispatchMessage(Handler.java:95) 在 android.os.Looper.loop(Looper.java:135) 在 android.app.ActivityThread.main(ActivityThread.java:5257) 在 java.lang.reflect.Method.invoke(本机方法) 在 java.lang.reflect.Method.invoke(Method.java:372) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

我不知道到底发生了什么以及这意味着什么,我在 stackoverflow.com 上进行了搜索,但我找到的答案仍然无法解决我的问题。

请高手帮帮我。 之前谢谢。

编辑:我的问题与另一个不同,因为这里我的错误日志给出了一些不同的报告。

注意。这是我的活动代码:

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

}

【问题讨论】:

标签: android database csv export


【解决方案1】:

在您调用 Toast.maketext 的 onPostExecute 中,您正在使用可能是 OnClickListener 类型的 ExportOrImport.this。 创建一个 Context 变量(上下文)并在你的 onCreate 方法中赋值,context=this 然后使用:

Toast.makeText(context, "Export Succesful!", Toast.LENGTH_SHORT).show();

【讨论】:

  • 但我删除了我的 dialog.show();现在它可以工作了,但没有出现对话框消息。
  • 您遇到了我在上面的回复中指出的同样问题。您正在使用 ExportOrImport.this 初始化对话框,您需要使用我在响应中提到的相同上下文变量以避免此错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-27
  • 2012-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多