有人知道我怎么能解决这个错误吗? 我假设窗口泄露与进度对话框有关但我不知道如何解决问题,因为我只是按照这2个链接提供的教程
第一个链接用于下载pdf文件并显示进度条的进度。
秒http://www.coderzheaven.com/2013/03/06/download-pdf-file-open-android-installed-pdf-reader/
第二个链接是在下载完成后打开文件。
logcat错误
06-27 11:09:13.592: E/WindowManager(2967): android.view.WindowLeaked: Activity
com.example.coco.content_co2 has leaked window
com.android.internal.policy.impl.PhoneWindow$DecorView@430e0198 that was originally
added here
06-27 11:09:13.592: E/WindowManager(2967): at
com.example.coco.content_co2.showProgress(content_co2.java:333)
06-27 11:09:13.592: E/WindowManager(2967): at
com.example.coco.content_co2$3.onClick(content_co2.java:103)
logcat中的另一个错误
06-27 11:09:04.572: E/AndroidRuntime(2967): FATAL EXCEPTION: Thread-570
06-27 11:09:04.572: E/AndroidRuntime(2967): java.lang.NullPointerException: file
06-27 11:09:04.572: E/AndroidRuntime(2967): at
android.net.Uri.fromFile(Uri.java:441)
06-27 11:09:04.572: E/AndroidRuntime(2967): at
com.example.coco.content_co2$5.run(content_co2.java:150)
06-27 11:09:04.572: E/AndroidRuntime(2967): at
java.lang.Thread.run(Thread.java:856)
06-27 11:09:13.592: E/WindowManager(2967): android.view.WindowLeaked: Activity
com.example.coco.content_co2 has leaked window
com.android.internal.policy.impl.PhoneWindow$DecorView@430e0198 that was originally
added here
06-27 11:09:13.592: E/WindowManager(2967): at
com.example.coco.content_co2.showProgress(content_co2.java:333)
06-27 11:09:13.592: E/WindowManager(2967): at
com.example.coco.content_co2$3.onClick(content_co2.java:103)
以下是错误指向的部分:
co2_2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String path = Environment.getExternalStorageDirectory().getPath() + "/CO22.pdf";
File f = new File(path);
if (f.exists()) {
showError("File has been downloaded."); // this is 103
co2_2.setEnabled(false);
}
else {
showProgress(dwnload_file_path2);
new Thread(new Runnable() {
public void run() {
downloadAndOpenPDF2();
}
}).start();
}
}});
如果进度条如此:
void showProgress(String file_path){
dialog = new Dialog(content_co2.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.myprogressdialog);
dialog.setTitle("Download Progress");
TextView text = (TextView) dialog.findViewById(R.id.tv1);
text.setText("Downloading file from ... " + file_path);
cur_val = (TextView) dialog.findViewById(R.id.cur_pg_tv);
cur_val.setText("Starting download...");
dialog.show(); //this is line 333
pb = (ProgressBar)dialog.findViewById(R.id.progress_bar);
pb.setProgress(0);
pb.setProgressDrawable(getResources().getDrawable(R.drawable.green_progress));
}
这是发生空指针错误的地方:
void downloadAndOpenPDF2 () {
new Thread(new Runnable() {
public void run() {
Uri path =
Uri.fromFile(downloadFile2(dwnload_file_path2)); //this is line 150
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
} catch (ActivityNotFoundException e) {
showError("PDF Reader application is not installed in your device");
}
}
}).start();
}
谢谢...
答案 0 :(得分:0)
Windowleaked错误是由进度对话框引起的。
理想情况下,您应该拥有AsyncTask
并相应地更新进度。
public class DownloadPDF extends AsyncTask{
Context mContext;
public DownloadPDF(Context context) {
this.mContext = context;
}
@Override
protected void onPreExecute() {
progressDialog = new ProgressDialog(mContext);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(false);
progressDialog.show();
}
}
启动DownloadPDF调用,如
DownloadPDF task = new DownloadPDF(MyActivity.This);
task.execute();