Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android上下载并打开PDF文件_Android_Pdf_Nullpointerexception_Progressdialog_Forceclose - Fatal编程技术网

在android上下载并打开PDF文件

在android上下载并打开PDF文件,android,pdf,nullpointerexception,progressdialog,forceclose,Android,Pdf,Nullpointerexception,Progressdialog,Forceclose,有人知道我如何修复这个错误吗? 我假设窗口泄漏与进度对话框有关,但我不知道如何解决这个问题,因为我只是遵循了这2个链接中提供的教程 首先 第一个链接用于下载pdf文件并在进度条上显示进度 第二 第二个链接是在下载完成后打开文件 logcat中的错误 06-27 11:09:13.592: E/WindowManager(2967): android.view.WindowLeaked: Activity com.example.coco.content_co2 has leaked windo

有人知道我如何修复这个错误吗? 我假设窗口泄漏与进度对话框有关,但我不知道如何解决这个问题,因为我只是遵循了这2个链接中提供的教程

首先

第一个链接用于下载pdf文件并在进度条上显示进度

第二

第二个链接是在下载完成后打开文件

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

  }

谢谢…

窗口泄漏的错误是由于进度对话框造成的。 理想情况下,您应该有一个
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();
  }
}
要开始下载PDF,请调用

 DownloadPDF task = new DownloadPDF(MyActivity.This);
 task.execute();

谢谢你的回复,所以我所要做的就是添加你上面提到的类,然后在下载时调用它?对不起,我只是一个android开发的新手:)是的,在下载时点击你需要调用
DownloadPDF task=new DownloadPDF(MyActivity.This);task.execute()为什么单击下载按钮后进度对话框不显示?我按照你上面提到的做了。thanksi刚刚给了你一个部分类,你需要在doInBackground中下载文件,然后显示进度。不确定你是如何使用AysncTask的,请参阅本文以供参考