Issue
I am trying to create a webview android application with Android Studio and Java. I had it working but then realized that I also needed to navigate between pages on the website. Looking at https://developer.android.com/guide/webapps/webview, I found that I had to add this line of code: myWebView.setWebViewClient(MyWebViewClient);. but, this line of code is giving me an error.
I have tried cleaning and rebuilding the project. I have tried importing import android.webkit.WebViewClient;. I have tried MyWebViewClient myWebviewClientInstance = new MyWebViewClient();
myWebView.setWebViewClient(myWebviewClientInstance); and myWebView.setWebViewClient(new MyWebViewClient());. But, all of this does not seem to work.
Here is my code: activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</android.support.constraint.ConstraintLayout>
And my MainActivity.java file (IN THIS FILE IS THE ERROR):
package com.rosaliewessels.mywebview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
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.setWebViewClient(MyWebViewClient); \\ERROR on MyWebViewClient
myWebView.loadUrl("https://www.mijnmedicijn.nl");
}
}
And AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rosaliewessels.mywebview">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
How can I resolve the error and be able to navigate between the pages on the website? I am really new to Android Development and help would be greatly appreciated.
Solution
The problem is here with this -
myWebView.setWebViewClient(MyWebViewClient); \ERROR on MyWebViewClient
Whenever we use setWebViewClient method, we need to create its inner class, just create it like this -
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);
// Following line will be change
**myWebView.setWebViewClient(new MyWebViewClient());**
myWebView.loadUrl("https://www.mijnmedicijn.nl");
}
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
// For performing click operation inside the mobile web view, you can enable Java script like this-
**myWebView.getSettings().setJavaScriptEnabled(true);**
Hope it may help you, Happy Coding :)
Answered By - Rajshree Tiwari
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.