Uros Tadic
Uros Tadic

Reputation: 65

Cannot resolve symbol webView on Android Studio

I am trying to make an application using HTML/CSS/Javascript and i have this running in the 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");
    }
}

The problem I'm getting is with the R.id.webView in the second to last line. It's saying it cannot resolve symbol for some reason, does anyone know why?

Things I've tried already:

  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)

EDIT: ADDING LAYOUT

<?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>

Can anyone help??

Upvotes: 4

Views: 29816

Answers (4)

eeeee
eeeee

Reputation: 1

        <?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/

Upvotes: 0

حمزة العبار
حمزة العبار

Reputation: 11

You can use the following solution :

WebView view=(WebView) this.findViewById(R.id.webView);
view.getSettings().setJavaScriptEnabled(true);

Upvotes: 1

hifromantal
hifromantal

Reputation: 1764

As you haven't posted your XML layout I can only just guess, but it has to do most likely that your pre-compiled R.java class does not hold reference to that ID, hence it cannot resolve until you do either one of the following.

  1. In scenarios like this, always use Build -> Rebuild Project in Android Studio. As last resort, use File -> Invalidate Caches / Restart.

  2. If the above does not solve it, make sure your resource id is properly declared:

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

Please note the + in-between @ and id. That makes sure the ID is created and put into the R.java file. Make sure you re-compile afterwards.

For more info: I suggest you reading the following API Guide from the Android: https://developer.android.com/guide/topics/ui/declaring-layout.html

Upvotes: 11

Myth
Myth

Reputation: 1258

There is no id attribute in your WebView xml tag. Add android:id="@+id/webView" to it.

This will make it look like:

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

Upvotes: 3

Related Questions