-1

I have been getting an error while creating a WebView in android studio, the error says the API is deprecated. I'm adding the code of my file. I think it would be easy to find, but I have no experience in android so I'm not able to find the error.

I have read about the updates of the android / java but could not find this

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {
private WebView MywebView;

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

    MywebView = (WebView) findViewById(R.id.view2);
    WebSettings webSettings = MywebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    MywebView.loadUrl("http://www.ynaps.com");

    MywebView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.contains("tel:")) {
                Intent intent = new Intent(Intent.ACTION_CALL);
                intent.setData(Uri.parse(url));
                startActivity(intent);
                return true;
            }else{
                return false;
            }
        }
    });

}

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

I should not get this error:

 E:\APP\pro\sdddd- 
 dunia\app\src\main\java\com\business\ynaps_app\MainActivity.java:
  uses or overrides a deprecated API.
  Recompile with -Xlint:deprecation for details.
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Amani
  • 281
  • 3
  • 15

3 Answers3

2

You must use shouldOverrideUrlLoading(WebView view, WebResourceRequest request) instead of shouldOverrideUrlLoading(WebView view, String url) which has been deprecated in API 24. You can check the doc page here.

cesarmarch
  • 617
  • 4
  • 13
  • Do you have other information about the error, more than the one you posted first ? Like a line number or something else. – cesarmarch Jun 01 '19 at 13:59
1

Error is fairly self explanatory. Your class have deprected method shouldOverrideUrlLoading. If you're not aware what does this mean either of the following link will help you understand. Is it wrong to use Deprecated methods or classes in Java? What does it mean for a method to be deprecated?

Some of the suggested solutions:

  1. Essentially, you might just get over this error by adding jvm argument specified in error -Xlint:deprecation.
  2. If that does does not help you can indicate JVM explicitly instructing method is deprecated with @Deprecated annotation.
  3. Lower the jdk/jre (Jvm) to the version in which method is not yet deprecated
  4. Correct solution is to update you code with removing usage of deprecated method and replace it with associated alternative method.
Milan Desai
  • 1,228
  • 8
  • 23
0

I think it has to do with this line:

webSettings.setJavaScriptEnabled(true);

The function 'setJavaScriptEnabled' seems to be deprecated.

Salomi
  • 1
  • 3