【问题标题】:Android: How to get WebView working?Android:如何让 WebView 工作?
【发布时间】:2018-04-15 23:16:08
【问题描述】:

我正在尝试在我的 android 应用中创建一个WebView,并且我的 imports 如下:

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>

【问题讨论】:

  • 您最近是否更改过包名等?
  • 请发布您的布局(xml)文件
  • 添加了它,是的,包是正确的

标签: android android-studio webview android-webview


【解决方案1】:

您定义了自己的名为WebView 的Java 类,它用作您的Activity。结果,编译器很难区分您自己的WebViewandroid.webkit.WebView。这涵盖了您的大部分问题。

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

【讨论】:

  • 那么与 Javaclass 相关的具体更改是什么?
  • 一旦我将R.id.webView 更改为R.id.webview 就会出现此错误:Inconvertible types; cannot cast 'android.view.View' to 'fyp.com.pastec_currency_recognition.WebView'
  • 我创建了另一个名为 Browser_Web_View 的活动并将其全部复制并更改了必要的区域并修复了大部分问题,所以我认为解决了你的意思,唯一的问题是如前所述在之前关于R.id.webViewR.id.webview 的评论中
【解决方案2】:

Java代码:

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>

【讨论】:

    猜你喜欢
    • 2013-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-07
    • 1970-01-01
    • 1970-01-01
    • 2020-07-21
    • 1970-01-01
    相关资源
    最近更新 更多