force close error after downloading file from app

2019-08-27 21:12发布

问题:

after creating an web-view app , whenever i download a file from it , it downloads and save in local storage and show notification in bar , but when i click it to open it from notification bar it force close the application and the PDF reader too

my main activity.java file code

ackage net.jptechsolutions.jptechinvoice;

import android.app.DownloadManager;
import android.net.Uri;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.CookieManager;
import android.webkit.DownloadListener;
import android.webkit.URLUtil;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

public WebView mywebview;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mywebview=(WebView)findViewById(R.id.webview);
    WebSettings webSettings=mywebview.getSettings();
    webSettings.setJavaScriptEnabled(true);
    mywebview.loadUrl("https://*******"****""*n");

    mywebview.setWebViewClient(new WebViewClient());
    mywebview.setDownloadListener(new DownloadListener()
    {

        @Override


        public void onDownloadStart(String url, String userAgent,
                                    String contentDisposition, String mimeType,
                                    long contentLength) {

            DownloadManager.Request request = new DownloadManager.Request(
                    Uri.parse(url));
            request.setMimeType(mimeType);
            String cookies = CookieManager.getInstance().getCookie(url);
            request.addRequestHeader("cookie", cookies);
            request.addRequestHeader("User-Agent", userAgent);
            request.setDescription("Downloading file...");
            request.setTitle(URLUtil.guessFileName(url, contentDisposition,
                    mimeType));
            request.allowScanningByMediaScanner();
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
            request.setDestinationInExternalPublicDir(
                    Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(
                            url, contentDisposition, mimeType));
            DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
            dm.enqueue(request);
            Toast.makeText(getApplicationContext(), "Downloading File",
                    Toast.LENGTH_LONG).show();
        }});
}

@Override
public void onBackPressed() {
    if (mywebview.canGoBack())
    {
        mywebview.goBack();
    }
    else
    {
        super.onBackPressed();
    }

}
}

what should i do .