I already tried referring both to this StackOverFlow question as well as this tutorial but still have not found a solution to my problem.
I set permissions already in the Android Manifest XML file, and have my webview problem under the FullscreenActivity file. My code is here:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
WebView view = (WebView) this.findViewById(R.id.webView);
view.setWebViewClient(new WebViewClient());
view.loadUrl("platinumskincare.com");
view.getSettings().setJavaScriptEnabled(true);
mVisible = true;
mControlsView = findViewById(R.id.fullscreen_content_controls);
mContentView = findViewById(R.id.fullscreen_content);
The error I keep getting says 'Cannot Resolve Symbol 'WebViewClient', and I believe it is on line 44 of my code. What should I do and how do I fix this problem?
Check if you have these imported
import android.webkit.WebView;
import android.webkit.WebViewClient;
Clean and rebuild your project in Android Studio.
Related
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.
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.
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:
Essentially, you might just get over this error by adding jvm argument specified in error -Xlint:deprecation.
If that does does not help you can indicate JVM explicitly instructing method is deprecated with #Deprecated annotation.
Lower the jdk/jre (Jvm) to the version in which method is not yet deprecated
Correct solution is to update you code with removing usage of deprecated method and replace it with associated alternative method.
I think it has to do with this line:
webSettings.setJavaScriptEnabled(true);
The function 'setJavaScriptEnabled' seems to be deprecated.
I've got this short code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView mywebview = (WebView) findViewById(R.id.webView);
mywebview.loadUrl("https://onet.pl");
mywebview.setWebViewClient(new WebViewClient());
}
How to able here javascript? I tried various version from stackoverflow, but nothing worked for me, i got a lot of errors. Anyone can help me?
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.webkit.WebSettings;
import android.webkit.WebView;
import static com.example.tencho.cst.R.id.*;
public class MainActivity extends AppCompatActivity {
private WebView MywebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MywebView =(WebView)this.findViewById(WebView);
WebSettings webSettings=MywebView.getSettings();
webSettings.setJavaScriptEnabled(true);
MywebView.loadUrl("www.cst.edu.bt");
}
}
This is the MainActivity.java. it's always giving me an error cannot find symbol variable WebView.
I did read the past similar post but I couldn't solve it error. Please need help!
At this line
MywebView =(WebView)this.findViewById(id);
id should be like R.id.webview1
webview1 is the id in your XML layout
The error is on this line:
MywebView =(WebView)this.findViewById(WebView);
The cause of this error is that the generated R.id class has no field named WebView (the argument you are passing to the findViewById() method). This is probably happening because your layout's <WebView> tag is missing an id attribute. Add this line to your <WebView> tag:
android:id="#+id/WebView"
Ben's answer is correct. In addition, if your new to Android, if you added this part in your tag which can be found in your activity_main.xml layout..
android:id="#+id/webview"
then in your MainActivity.java, it should be like this:
MywebView =(WebView) findViewById(R.id.webview);
I don't know how to access my index.html webpage when the app is loaded. Actually on my code I use file:///android_asset/index.html, I've tried to load a Google page to test, it works, but it launch the Chrome app...
Here is my code of my MainActivity.java :
package com.totominc.fr.webapp;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
public class MainActivity extends ActionBarActivity {
#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.setWebChromeClient(new WebChromeClient());
webView.loadUrl("file:///android_asset/index.html"); // error may be here...
}
}
But the error which I got is that it can't find the file index.html.
Here is a screenshot of my project.
Thanks for helping me!
try this
file:///Android_res/drawable/index.html
As your index html is in drawable folder. this would work
You need to put your index.html file into your assets folder, and not /res/drawable.
So your index.html should be in /assets/index.html.
Create your assets folder in src/main/assets/ and put your index.html there, not inside your drawables. Regarding the page opening in Chrome, try this instead
#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);
// Changed here
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("file:///android_asset/index.html"); // error may be here...
}
This is my code:
package sai.datla.game;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
public class GametestActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView webView = (WebView)findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient());
webView.clearSslPreferences();
webView.loadUrl("file:///android_asset/www/index.html");
}
}
but it is saying that it cannot find the link in the emulator. I have checked many times if my index.html file is in the www folder, which is in my assets, but it won't work. Please help.
By the way I am only twelve years old so please make the answers easy to understand for a child.
// Find view in layout
WebView wv = (WebView) findViewById(R.id.webView_tobe_loaded);
// Get settings
WebSettings wbset = wv.getSettings();
// Enable JavaScript
wbset.setJavaScriptEnabled(true);
// Set new client (to handle website in your app)
wv.setWebViewClient(new MyWebViewClient());
// Example of the URL
String url = "http://www.google.com";
// Load website
wv.loadUrl(url);
This code will help you to solve the problem.
It worked for me.
instead of loadUrl, try using the loadDataWithBaseURL method:
webview.loadDataWithBaseURL("android.resource://<package_name>/assets/www/file_name", html, mimeType, encoding, "");