I'm getting the error "Abstract methods do not specify a body" with the below code....
DCWebView.setWebViewClient(new MyWebViewClient() {
@Override
public abstract void launchExternalBrowser(String url) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
}
});
MyWebViewClient is an abstract class. I'm wondering what the above code is doing exactly. It's clearly intending on defining the function for the class... can I just put any code within the new MyWebViewClient() { } to define variables and other functions?
What other awesome things can be done in here?
Just remove abstract and it will work. If the method launchExternalBrowser is not defined in MyWebViewCLient you should also remove @Override annotation. @Override means that you override a method which is defined in an extended class hierarchy.
DCWebView.setWebViewClient methods requires an object of type MyWebViewClient as a parameter. By writing new MyWebClient() { .. } you create an instance of an anonymous class which extends MyWebViewClient.
Anonymous classes are (almost) the same as "normal" classes. You can define both new variables and methods there. The main difference is that an anonymous class is defined in a place where it is used (there is no external class definition and because of that it has no name, it is "anonymous") and it is used only in this one place (as there is no external definition it cannot be used elsewhere).
Anonymous classes are very commonly used in Java, however this is something a little more advanced. As esaj said in his answer it is a good idea to read something about this. There is plenty of information in the web e.g. here or here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With