我正在开发一个Android应用程序,需要将数据库导出到CSV文件并将其写入SD卡或本地手机存储。 但是当我尝试运行我的方法时,我收到了这个错误:
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) at 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) at java.lang.reflect.Method.invoke(Native Method) 在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上搜索,但我找到的答案仍然没有解决我的问题。
请高手帮帮我。 谢谢你。
编辑:我的问题与另一个问题不同,因为我的错误日志提供了一些不同的报告。
NB。这是我的活动代码:
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);
}
}
答案 0 :(得分:1)
在您调用Toast.maketext的onPostExecute中,您使用的是ExportOrImport.this,它可能是OnClickListener类型。
创建一个Context变量(context)并在onCreate方法context=this
中分配它,然后使用:
Toast.makeText(context, "Export Succesful!", Toast.LENGTH_SHORT).show();