有人知道我该如何解决这个错误吗?我认为泄漏的窗口与进度对话框有关,但我不知道如何解决问题,因为我只是按照这 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();
}
谢谢 ...