0

I am currently working on webview where I need user to dowanload pdf , mp4 files from link and found solution in some forum but as I paste the given code..I'm constantly getting error of Non-static method cannot be referenced from static context in line WebView.setWebViewClient(new WebViewClient() . I'm new to programming can anyone suggest me what am I doing wrong Here??

Thanks in advance.

    public class MainActivity extends AppCompatActivity {

WebView webView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    WebView webView = (WebView) findViewById(R.id.webView);
    webView.getSettings().setJavaScriptEnabled(true);



    webView.setWebViewClient(new WebViewClient() {


        public void onPageFinished(WebView view, String url) {
            findViewById(R.id.image1).setVisibility((View.GONE));
            findViewById(R.id.webView).setVisibility(View.VISIBLE);

        }
    });
    webView.loadUrl("file:///android_asset/index.html");

    WebView.setWebViewClient(new WebViewClient() {
        @Override
        public void onReceivedError(WebView view, int errorCode,
                                    String description, String failingUrl) {
            Log.d("WEB_VIEW_TEST", "error code:" + errorCode + " - " + description);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // handle different requests for different type of files
            // this example handles downloads requests for .apk and .mp3 files
            // everything else the webview can handle normally
            if (url.endsWith(".apk")) {
                Uri source = Uri.parse(url);
                // Make a new request pointing to the .apk url
                DownloadManager.Request request = new DownloadManager.Request(source);
                // appears the same in Notification bar while downloading
                request.setDescription("Description for the DownloadManager Bar");
                request.setTitle("YourApp.apk");
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                    request.allowScanningByMediaScanner();
                    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                }
                // save the file in the "Downloads" folder of SDCARD
                request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "SmartPigs.apk");
                // get download service and enqueue file
                DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                manager.enqueue(request);
            }
            else if(url.endsWith(".mp3")) {
                // if the link points to an .mp3 resource do something else
            }
            // if there is a link to anything else than .apk or .mp3 load the URL in the webview
            else view.loadUrl(url);
            return true;
        }
    });


}

}

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • Please look at [What is the reason behind “non-static method cannot be referenced from a static context”?](http://stackoverflow.com/questions/290884/what-is-the-reason-behind-non-static-method-cannot-be-referenced-from-a-static) – Hovercraft Full Of Eels Jul 17 '17 at 00:38
  • I already checked that one..but no help either. Can you please only suggest me what should i change so that It will work. – Sagar Rawal Jul 17 '17 at 12:15
  • This error means that you're trying to call a non-static method on a class not an object, and suggests that you don't yet understand the very basic concepts of what an object is or why you need to make them. Your best solution is to read the basic tutorials on this and then create the necessary object where the error is being caused, and calling the method on the object, not the class. Please start at this tutorial: [classes and objects](https://docs.oracle.com/javase/tutorial/java/javaOO/index.html) – Hovercraft Full Of Eels Jul 17 '17 at 16:02
  • Thanks for replying. I really am getting to knew about the miracles of java but I'd be really greatful if anyone could edit the above code or how can I remove the error in above code. So I will know and learn more by such example and equally my project would be finally complete. – Sagar Rawal Jul 17 '17 at 19:00
  • Note the difference between `webView.setWebViewClient` and `WebView.setWebViewClient`. Which one works? – Hovercraft Full Of Eels Jul 17 '17 at 19:50
  • Indeed I change to webView.setWebViewClient from WebView.setWebViewClient, but as I developed an app and installed it...now it stucks in splash screen. – Sagar Rawal Jul 18 '17 at 12:44
  • And so now you've got a different problem, but at least your code compiles. Keep on plugging. – Hovercraft Full Of Eels Jul 18 '17 at 13:37

0 Answers0