Can not resolve method 'findViewById(int)'

Ferhat picture Ferhat · Jun 2, 2015 · Viewed 92.1k times · Source

I'm having a trouble with findViewByid but I can't find where the problem is.

Here's my FirstFragment class code:

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;

import java.util.ArrayList;
import java.util.List;


public class FirstFragment extends Fragment {
public static final String TAG = "first";

private WebView mWebView;
ProgressBar progressBar;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    mWebView = (WebView) findViewById(R.id.activity_main_webview);

    progressBar = (ProgressBar) findViewById(R.id.progressBar1);

    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    mWebView.loadUrl("http://www.google.com");
 }

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

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);


 }
}

Answer

Kristy Welsh picture Kristy Welsh · Jun 2, 2015

You need to do this in onCreateView:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle   savedInstanceState) {
   View view =  inflater.inflate(R.layout.secondefragment, container, false);
    mWebView = (WebView) view.findViewById(R.id.activity_main_webview);
   progressBar = (ProgressBar) view.findViewById(R.id.progressBar1);

   WebSettings webSettings = mWebView.getSettings();
   webSettings.setJavaScriptEnabled(true);
   mWebView.loadUrl("http://www.google.com");

   return view;


}