Following the suggestion given, this is the PlaceHolderFragment code, with the WebView code included, that gives me the error cannot make a static reference to the non-static method findViewById(int) from the type Activity:
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
WebView siteViewer = (WebView) findViewById(R.id.webView);
if(siteViewer != null)
siteViewer.loadUrl("http://www.website.com");
return rootView;
}
}
Solução
In your Activity you are inflating main_layout which doesn't contain a WebView at all. Therefore the findViewById won't find anything and siteViewer is null.
Move your code to load the Url into your fragment.
Since you'r using a PlaceHolderFragment I guess it will be replaced by a real one later. That's the place to put your code. You'll probably inflate your fragment_main.xml in the fragments onCreateView and then you can load the Url in the fragment's onResume method.
Note the rootView.findViewById(R.id.webView) instead of the simple findViewById(R.id.webView). Fragments don't have a findViewById method but since you'r inflating the layout yourself you can use the View.findViewById.
Outras dicas
siteViewer is null, because there is no such element with id = webView found at the activity_main.xml. Please double check that you've typed it correctly.
I will answer from my knowledge since i didnt know the project heirarchy ...
I) .suppose you have the activity in package com.another.package and your project package which is declared in the manifest is com.main.package now if you will try to access the webview using R.id.webView in package com.another.package it wont be available hence you will get null pointer exception so to fix that import com.main.package.R.*; in the crashing activity.
II). suppose the crashing activity is in the same package com.main.package then plz cross check dat whether webview named webView is present in the activity_main.
III). may be u are casting other layout to webView like webView is id of other than webview ( but in this case you wont get null pointer exception just for info).
Hope it helps ... Thx
did you set the permission to access the internet in manifest file?