Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

438
Vistas
How can i add webview in my class? [findViewById(int) in fragment]

I have an activity and i want it to display a website using webview. But i am not able to do it with my existing code.

(EDIT: I made the necessary changes as told by Nirmal Raj but now i am getting an error that "Cannot resolve method findViewById(int)" i have edited my code please look at it and identify the problem.)

BlogAcitivity.java

package thenerdimite.nuttybuddies;

import android.app.Fragment;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class BlogActivity extends Fragment {

    View myView;
    WebView webView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        myView = inflater.inflate(R.layout.activity_blog, container, false);
        return myView;

        webView = (WebView)myView.findViewById(R.id.blogview);

        webView = (WebView)findViewById(R.id.blogview);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient());
        webView.loadUrl("http://www.google.com");
    }

}

The error "Cannot resolve method findViewById(int)" coming with a red color. What changes should i do in this code so that the webview can work. Thanks!

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

I am not sure what you want but I will give you a sample of how to implement a webview.

Layout:

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

Add this to the layout in which you want to show the WebView.

Java Code:

        webView = (WebView)findViewById(R.id.webs);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient());
        webView.loadUrl("http://www.google.com");

Since you are using fragments the only difference will be in findViewById() function. You have to call myView.findViewbyId().

Also don't forget to add this to the manifest

<uses-permission android:name="android.permission.INTERNET"/>
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda