Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can not resolve method 'findViewById(int)'

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);    } } 
like image 843
Ferhat Avatar asked Jun 02 '15 14:06

Ferhat


People also ask

What is findViewById method in Android?

FindViewById(Int32)Finds a view that was identified by the android:id XML attribute that was processed in #onCreate .

Is findViewById a method?

findViewById is the method that finds the View by the ID it is given. So findViewById(R. id. myName) finds the View with name 'myName'.

What does findViewById return?

findViewById returns an instance of View , which is then cast to the target class. All good so far. To setup the view, findViewById constructs an AttributeSet from the parameters in the associated XML declaration which it passes to the constructor of View . We then cast the View instance to Button .


2 Answers

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;   } 
like image 180
Kristy Welsh Avatar answered Oct 03 '22 03:10

Kristy Welsh


Fragment doesn't provide thefindViewById() method. This is provided in Activity or View. When implementing a Fragment you don't inflate your views in onCreate() (like you normally do in an Activity.) Instead, you do it in onCreateView() and you need to use the inflated root View to find the ID within the layout you inflated.

like image 25
Larry Schiefer Avatar answered Oct 03 '22 02:10

Larry Schiefer