Home > OS >  Getting "Cannot find symbol WebView view" error in java android studio
Getting "Cannot find symbol WebView view" error in java android studio

Time:05-27

package com.example.appthree;

import androidx.appcompat.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);


        String Htmlurl = "file://android_assets/index.html";
        WebView view = (WebView) this.findViewById(R.id.webView);
        view.getSettings().setJavaScriptEnabled(true);
        view.loadUrl(Htmlurl);

    }
}

This is my MainActivity.java file. I want to rander html file on app which is inside the assets folder. But I'm getting this error -> error: cannot find symbol WebView view = (WebView) this.findViewById(R.id.webView); ^ symbol: variable webView location: class id

see screenshot (https://prnt.sc/yx65CzqLDBmk)

Updated____ this is my activity_main.xml file

<?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"
    tools:context=".MainActivity">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Welcome"
        android:textSize="48sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.421"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.043" />

    <Button
        android:id="@ id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World"
        tools:layout_editor_absoluteX="136dp"
        tools:layout_editor_absoluteY="193dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

CodePudding user response:

  1. You need to add internet permission in AndroidManifest.xml

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

  1. Make sure that your id of webView component into activity_main.xml is webView.
WebView
    android:id="@ id/webView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

CodePudding user response:

You need to add the WebView inside the ConstraintLayout as Halil Ozel's code. Something like this

<?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"
    tools:context=".MainActivity">

<WebView
    android:id="@ id/webView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Welcome"
    android:textSize="48sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.421"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.043" />

<Button
    android:id="@ id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World"
    tools:layout_editor_absoluteX="136dp"
    tools:layout_editor_absoluteY="193dp" />

</androidx.constraintlayout.widget.ConstraintLayout>
  • Related