WebView in a Fragment?

2

I have this error:

  

Second_Fragment can not be converted to Fragment

Code:

public class Second_fragment extends Fragment {

    String direccion_web = "https://www.google.es/";

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.second_tab,container,false);

        WebView appWeb = (WebView) getActivity().findViewById(R.id.webView);
        //Habilitamos el javaScript y el zoom
        appWeb.getSettings().setJavaScriptEnabled(true);
        appWeb.getSettings().setBuiltInZoomControls(true);
        //Cargamos el enlace definido
        appWeb.loadUrl(direccion_web);
        //Este método es para que el navegador se quede en nuestra aplicación
        appWeb.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }
        });
        return v;
    }

second_tab.xml : 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">


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

</LinearLayout>

What is wrong? Can not you implement this in a Fragment?

public class MainActivity extends AppCompatActivity {

    BottomBar mBottomBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mBottomBar = BottomBar.attach(this, savedInstanceState);
        mBottomBar.setItemsFromMenu(R.menu.menu_main, new OnMenuTabSelectedListener() {

            @Override
            public void onMenuItemSelected(@IdRes int i) {

                if (i == R.id.one) {

                    First_fragment f1 = new First_fragment();
                    getSupportFragmentManager().beginTransaction().replace(R.id.frame,f1).commit();

                }

                if (i == R.id.two) {

                    Second_fragment f2 = new Second_fragment();
                    getSupportFragmentManager().beginTransaction().replace(R.id.frame, f2).commit();

                }

                if (i == R.id.three) {

                    Three_fragment f3 = new Three_fragment();
                    getSupportFragmentManager().beginTransaction().replace(R.id.frame, f3).commit();

                }
                if (i == R.id.four) {

                    Four_fragment f4 = new Four_fragment();
                    getSupportFragmentManager().beginTransaction().replace(R.id.frame, f4).commit();
                }
                if (i == R.id.five) {

                    Five_fragment f5 = new Five_fragment();
                    getSupportFragmentManager().beginTransaction().replace(R.id.frame, f5).commit();

                }
            }
        });
        mBottomBar.mapColorForTab(0, "#c92029");
        mBottomBar.mapColorForTab(1, "#c92029");
        mBottomBar.mapColorForTab(2, "#c92029");
        mBottomBar.mapColorForTab(3, "#c92029");
        mBottomBar.mapColorForTab(4, "#c92029");
    }
}
    
asked by Rf Mvs 27.10.2016 в 23:31
source

1 answer

1

You are not recognizing your class as a fragment.

When you inflate the layout of the Fragment, you must obtain the references of the element within the Fragment, in this case the WebView, not within the Activity.

 WebView appWeb = (WebView) getActivity().findViewById(R.id.webView); //* INCORRECTO

Ensure that your WebView is within second_tab.xml

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.second_tab,container,false);

    //* INCORRECTO: WebView appWeb = (WebView) getActivity().findViewById(R.id.webView);
     WebView appWeb = (WebView) v.findViewById(R.id.webView); //CORRECTO.

    //Habilitamos el javaScript y el zoom
    appWeb.getSettings().setJavaScriptEnabled(true);
    appWeb.getSettings().setBuiltInZoomControls(true);
    //Cargamos el enlace definido
    appWeb.loadUrl(direccion_web);
    //Este método es para que el navegador se quede en nuestra aplicación
    appWeb.setWebViewClient(new WebViewClient(){
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return false;
        }
    });


    return v;
}
    
answered by 28.10.2016 / 01:05
source