I have this code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView siteViewer = (WebView) findViewById(R.id.webView);
if(siteViewer != null)
siteViewer.loadUrl("http://www.website.com");
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
It's the default android app code, except:
WebView siteViewer = (WebView) findViewById(R.id.webView);
if(siteViewer != null)
siteViewer.loadUrl("http://www.website.com");
siteViewer
is always null
. If I don't use the if()
statement, the app will crash immediately upon opening, otherwise it just skips loadURL
.
I found a similar bug here:
https://code.google.com/p/android/issues/detail?id=11533
Apparently some OS's don't allow it? I'm running it on 4.4.2 in an emulator. I also tried it on a Motorola Razr with 4.1.2 and I get the same result
With the line if(siteViewer != null)
commented out, this is the logcat:
05-15 16:05:35.112: D/AndroidRuntime(1201): Shutting down VM
05-15 16:05:35.112: W/dalvikvm(1201): threadid=1: thread exiting with uncaught exception (group=0xada41ba8)
05-15 16:05:35.142: E/AndroidRuntime(1201): FATAL EXCEPTION: main
05-15 16:05:35.142: E/AndroidRuntime(1201): Process: com.example.webviewtest, PID: 1201
05-15 16:05:35.142: E/AndroidRuntime(1201): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.webviewtest/com.example.webviewtest.MainActivity}: java.lang.NullPointerException
05-15 16:05:35.142: E/AndroidRuntime(1201): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-15 16:05:35.142: E/AndroidRuntime(1201): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-15 16:05:35.142: E/AndroidRuntime(1201): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-15 16:05:35.142: E/AndroidRuntime(1201): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-15 16:05:35.142: E/AndroidRuntime(1201): at android.os.Handler.dispatchMessage(Handler.java:102)
05-15 16:05:35.142: E/AndroidRuntime(1201): at android.os.Looper.loop(Looper.java:136)
05-15 16:05:35.142: E/AndroidRuntime(1201): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-15 16:05:35.142: E/AndroidRuntime(1201): at java.lang.reflect.Method.invokeNative(Native Method)
05-15 16:05:35.142: E/AndroidRuntime(1201): at java.lang.reflect.Method.invoke(Method.java:515)
05-15 16:05:35.142: E/AndroidRuntime(1201): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-15 16:05:35.142: E/AndroidRuntime(1201): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-15 16:05:35.142: E/AndroidRuntime(1201): at dalvik.system.NativeStart.main(Native Method)
05-15 16:05:35.142: E/AndroidRuntime(1201): Caused by: java.lang.NullPointerException
05-15 16:05:35.142: E/AndroidRuntime(1201): at com.example.webviewtest.MainActivity.onCreate(MainActivity.java:25)
05-15 16:05:35.142: E/AndroidRuntime(1201): at android.app.Activity.performCreate(Activity.java:5231)
05-15 16:05:35.142: E/AndroidRuntime(1201): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-15 16:05:35.142: E/AndroidRuntime(1201): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-15 16:05:35.142: E/AndroidRuntime(1201): ... 11 more
Here is activity_main.xml
:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.webviewtest.MainActivity"
tools:ignore="MergeRootFrame" />
Here is fragment_main.xml
:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.webviewtest.MainActivity$PlaceholderFragment" >
<WebView
android:id="@+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
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;
}
}