Kostas St
Kostas St

Reputation: 3

cannot find symbol method setContentView(int)

I 'm trying to build an android app but I'm stuck in the same error. Android Studio can't find symbol method setContentView(int) and findViewById(int). Here is the code:

public class NewsFragment extends Fragment {


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_news);

    WebView mainWebView = (WebView) findViewById(R.id.mainWebView);

    WebSettings webSettings = mainWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    mainWebView.setWebViewClient(new MyCustomWebViewClient());
    mainWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

    mainWebView.loadUrl("http://www.google.com");
}

private class MyCustomWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}

any ideas?

Upvotes: 0

Views: 4891

Answers (2)

Blackbelt
Blackbelt

Reputation: 157467

you are extending Fragment and setContentView is a method of Activity not of Fragment. You can either change your class to extend Activity or keep using Fragment and override onCreateView to inflate and return an instance of fragment_news, and use it to retrive the widgets it contains

Upvotes: 1

starkshang
starkshang

Reputation: 8558

You are using Fragment,not Activity.If you want to use activity,please make your class implements Activity instead of Fragment.

If you want to use Fragment,you should set view in onCreateView,like this:

private WebView mainWebView;
protected View onCreateView(LayoutInflater inflater, ViewGroup vg, Bundle data){
    View view = inflate.inflate(R.layout.fragment_news,vg,false);
    mainWebView = (WebView) view.findViewById(R.id.mainWebView);
    return view;
}

Upvotes: 3

Related Questions