无法在Android Studio中解析符号webView

3

我正在尝试使用HTML/CSS/Javascript制作一个应用程序,并且我已经将其运行在MainActivity.java中:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WebView webView = (WebView)findViewById(R.id.webView); #The line in question
        webView.loadUrl("file:///android_asset/www/index.html");
    }
}

我遇到的问题在倒数第二行的 R.id.webView 中。出于某种原因,它说无法解析符号,有人知道为什么吗?
我已经尝试过以下方法:
  1. Importing

    import android.webkit.WebView;
    import android.webkit.WebViewClient; (dont need this one but still got it)
    
  2. Changing capitalization of words in webView (WebView, Id instead of id etc)

编辑:添加布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="g24.guidedproject.MainActivity">

    <WebView android:layout_height="match_parent"
        android:layout_width="match_parent" />
</RelativeLayout>

有人能帮忙吗?

如果找不到R.id.webView,那么问题就出在这里了。我们能看一下你定义webView的XML布局吗? - Stefan
我已经将WebView添加到activity_main的组件树中。实际上,您可以在此视频https://www.youtube.com/watch?v=eJa8xie9WZs中看到我所做的一切。但是我现在卡住了。 - Uros Tadic
你能展示一下XML中声明Web View的部分吗?这个视频是5月12日发布的。 - Stefan
https://snag.gy/A0D2tG.jpg - Uros Tadic
靠,我甚至没有注意到那个。我会更新我的第一篇帖子。 - Uros Tadic
@cricket_007 我已经更新了第一篇帖子。 - Uros Tadic
4个回答

11

由于您没有发布XML布局,我只能猜测,但很可能是由于您的预编译R.java类没有引用该ID,因此在您执行以下操作之前无法解决。

  1. 在这种情况下,始终在Android Studio中使用Build -> Rebuild Project。作为最后的手段,请使用File -> Invalidate Caches / Restart

  2. 如果以上方法不能解决问题,请确保正确声明您的资源ID

<WebView android:id="@+id/webView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></WebView
请注意@id之间的+。这会确保ID被创建并放入R.java文件中。确保重新编译后在继续操作。
更多信息请参阅Android的以下API指南:https://developer.android.com/guide/topics/ui/declaring-layout.html

@UrosTadic 请确保在您的xml布局中添加android:id部分。我昨晚忘记在这里粘贴代码片段了。请查看我的更新。 - anthonymonori
非常感谢。不知道原帖的作者,但这对我很有帮助。(+1) - user7969920
清理和重新构建并没有帮助我,但是 使缓存失效 / 重新启动 帮助了我。 - danyapd

3

您的WebView xml标签中缺少id属性。请添加android:id="@+id/webView"

这样看起来就像:

<WebView android:id="@+id/webView"
        android:layout_height="match_parent"
        android:layout_width="match_parent" />

1
您可以使用以下解决方案:
WebView view=(WebView) this.findViewById(R.id.webView);
view.getSettings().setJavaScriptEnabled(true);

0
        <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.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">


<LinearLayout
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:orientation="vertical"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <TextView
        android:id="@+id/textView8"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="layoutxml"
        android:textSize="30sp" />

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <EditText
                android:id="@+id/editText4"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:ems="10"
                android:inputType="textPersonName"
                android:text="https://www.google.com/" />

            <ImageView
                android:id="@+id/imageView6"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                app:srcCompat="@android:drawable/ic_menu_search" />

            <ImageView
                android:id="@+id/imageView7"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                app:srcCompat="@android:drawable/presence_audio_online" />

            <ImageView
                android:id="@+id/imageView8"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                app:srcCompat="@android:drawable/stat_sys_download_done" />

        </LinearLayout>
    </HorizontalScrollView>

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <Button
                android:id="@+id/button18"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:backgroundTint="@color/bacrondtexttime"
                android:text="ALL" />

            <Button
                android:id="@+id/button19"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="books" />

            <Button
                android:id="@+id/button21"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="photo" />

            <Button
                android:id="@+id/button17"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="video" />

            <Button
                android:id="@+id/button20"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="More " />
        </LinearLayout>
    </HorizontalScrollView>

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

   </androidx.constraintlayout.widget.ConstraintLayout>

Ref https://layoutxml.blogspot.com/


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接