Android:如何让WebView正常工作?

时间:2018-04-15 23:16:08

标签: android android-studio webview android-webview

我正在尝试在我的Android应用中创建WebView,我有导入,如下所示:

import android.webkit.WebView;
import android.webkit.WebViewClient;

我已添加 uses-permission ,如下所示:

<uses-permission android:name="android.permission.INTERNET" />

我将此作为 onCreate():

    private WebView webview;

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

    webview =(WebView)findViewById(R.id.webView);

    webview.setWebViewClient(new WebViewClient());
    webview.getSettings().setJavaScriptEnabled(true);
    webview.getSettings().setDomStorageEnabled(true);
    webview.setOverScrollMode(WebView.OVER_SCROLL_NEVER);
    webview.loadUrl("https://www.google.com");

}

我仍然无法让它工作,因为我在以下内容中遇到Cannot Resolve Method错误:

setWebViewClient
getSettings()
setOverScrollMode
loadUrl
以下是

Cannot Resolve Symbol

R.id.webView
OVER_SCROLL_NEVER

任何人都可以帮忙吗?

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=".WebView">


<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    /> 

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="48dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="10dp"
    android:onClick="Home"
    android:text="Back"
    app:layout_constraintEnd_toStartOf="@+id/WebView"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

2 个答案:

答案 0 :(得分:0)

您定义了自己的名为WebView的Java类,其中一个用作Activity。因此,编译器难以区分您自己的WebViewandroid.webkit.WebView。这涵盖了你的大多数问题。

例外是R.id.webView的符号未找到错误。你得到的是因为你的布局有android:id="@+id/webview"。 Android中的大多数内容都区分大小写,包括小部件ID。我建议在这两个地方使用小写webview

答案 1 :(得分:0)

<强> JavaCode:

public class YourClassName extends AppCompatActivity{

private WebView wv_content = null;
private WebSettings webSettings = null;

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

    wv_content = (WebView) findViewById(R.id.webview);

    wv_content.getSettings().setAllowFileAccess(true);
    wv_content.getSettings().setSupportZoom(true);
    wv_content.setVerticalScrollBarEnabled(true);
    wv_content.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    wv_content.getSettings().setLayoutAlgorithm(LayoutAlgorithm.NORMAL);
    wv_content.getSettings().setLoadWithOverviewMode(true);
    wv_content.getSettings().setUseWideViewPort(true);
    wv_content.getSettings().setJavaScriptEnabled(true);
    wv_content.getSettings().setPluginState(WebSettings.PluginState.ON);

    wv_content.getSettings().setSaveFormData(false);
    wv_content.getSettings().setSavePassword(false);

    wv_content.getSettings().setRenderPriority(RenderPriority.HIGH);
    wv_content.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);

    wv_content.getSettings().setJavaScriptEnabled(true);
    wv_content.getSettings().setLoadWithOverviewMode(true);
    wv_content.getSettings().setUseWideViewPort(true);

    webSettings = wv_content.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setSupportZoom(true);
    webSettings.setBuiltInZoomControls(true);

    wv_content.loadUrl("https://www.google.com");
   }
    }

<强> 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=".WebView">


 <WebView 
  android:id="@+id/webview"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"/> 

<Button
  android:id="@+id/button3"
  android:layout_width="wrap_content"
  android:layout_height="48dp"
  android:layout_marginEnd="8dp"
  android:layout_marginStart="10dp"
  android:onClick="Home"
  android:text="Back"
  app:layout_constraintEnd_toStartOf="@+id/WebView"
  app:layout_constraintHorizontal_bias="0.0"
  app:layout_constraintStart_toStartOf="parent"
  app:layout_constraintTop_toTopOf="parent" />
 </android.support.constraint.ConstraintLayout>