How to call a hosted id in the extended activity layout of Fragment?

1

Well, I'm developing an app, which has a navigation drawer and in this navigation I put it to open each item in a new activity, and in each acitivty will have a layout, so far so good. But now, I'm trying to call a webview with progress bar, everything is already done in the layout, but when I call them by id (findViewById) in Activity it presents errors, since I'm using extends Fragment and does not extend Acitivity. In this case, if I replace the Fragment with Activity, it simply gets null, and the screen goes white with no content. Can you help me call this Webview along with Fragment?

Main activity:

// Here is the part of the activity where it declares which fragment will be the main one, which will be the first screen:

    if (id == R.id.home) {
        //Aplicar fragment principal
        home fragment = new home();
        android.support.v4.app.FragmentTransaction fragmentTransaction =
                getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, fragment);
        fragmentTransaction.commit();           

And here is the activity home.java, as previously stated:

import android.os.Bundle; 
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


        public class home extends Fragment {

            public home() {
            }

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                     Bundle savedInstanceState) {
                // Chamando o layout nesta activity
                return inflater.inflate(R.layout.home, container, false);
            }

And here follows my home.xml layout

<ProgressBar
    android:id="@+id/progressBar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

<WebView
    android:id="@+id/web"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@id/progressBar"/>

What I want is to declare the webview and progressBar in the home.java activity. Many thanks!

    
asked by anonymous 20.02.2016 / 21:19

1 answer

3

First, I think you're confusing a few concepts. If your home extends Fragment is because it is a Fragment and not a Activity .

It is very easy to manipulate a WebView within your Fragment . Note that the onCreateView method returns a View inflated by LayouInflater . This View is nothing more than the entire layout of your Fragment . So you can change the home class to handle WebView as follows:

public class HomeFragment extends Fragment {
    private WebView webView;
    private ProgressBar progressBar;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_home, container, false);
        webView = (WebView) root.findViewById(R.id.web_view);
        progressBar = (ProgressBar) root.findViewById(R.id.progress_bar);

        return root;
    }
} 

With these defined attributes you can manipulate these views at will. I could also pass them to Activity which contains HomeFragment , as I think was your intention. However, I recommend that all operations relating to these elements be done in% w / o of which they belong.

    
20.02.2016 / 22:02