0

Does somebody know how i could fix this error? I assume the window leaked has something to do with the progress dialog but i dont know how to fix the problem since i just followed tutorials provided in this 2 links

first http://www.coderzheaven.com/2012/04/29/download-file-android-device-remote-server-custom-progressbar-showing-progress/

The first link is for downloading the pdf file and showing the progress on progress bar.

second http://www.coderzheaven.com/2013/03/06/download-pdf-file-open-android-installed-pdf-reader/

The second link is to open the file after the download is completed.

Error in 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)

Another error in 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)

Here is the part where the error is pointing:

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

        }   
        }});

This if for the progress bar:

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

This is where the null pointer error occurs:

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

  }

Thanks ...

Ann ann
  • 21
  • 2
  • 7

1 Answers1

0

Windowleaked error is due to the progress dialog. Ideally you should be having an AsyncTask and update the progress accordingly.

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

To start DownloadPDF call like

 DownloadPDF task = new DownloadPDF(MyActivity.This);
 task.execute();
blganesh101
  • 3,647
  • 1
  • 24
  • 44
  • thanks for your response, so all i have to do is add the class you mentioned above then call it on download click? sorry, I am just a newbie on android development :) – Ann ann Jun 27 '13 at 04:21
  • Yes on download click you need to call `DownloadPDF task = new DownloadPDF(MyActivity.This); task.execute();` – blganesh101 Jun 27 '13 at 04:22
  • why is the progress dialog not showing once I click the download button? i did as what you mentioned above. thanks – Ann ann Jun 27 '13 at 05:06
  • i just gave you a partial class you need to be downloding the file in doInBackground and then show progress. Not sure how you are using the AysncTask, For reference please see this post http://stackoverflow.com/questions/3028306/download-a-file-with-android-and-showing-the-progress-in-a-progressdialog – blganesh101 Jun 27 '13 at 05:13