0

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;
    }
}
JVE999
  • 3,327
  • 10
  • 54
  • 89
  • 1
    This has nothing to the with the bug you mention (in their case the loadUrl is executed but crashes while in your case the loadUrl won't happen because the view is null). Could you post the xml layout (activity_main.xml)? – Emanuel Moecklin May 15 '14 at 19:45
  • Can you post a logcat with a crash as well – Timothy Frisch May 15 '14 at 19:46
  • I actually used `fragment_main.xml`. Should I move it to `activity_main.xml`? The logcat reports `05-15 15:56:49.880: E/AndroidRuntime(821): Caused by: java.lang.NullPointerException` – JVE999 May 15 '14 at 19:58
  • Why did I get two negative votes? This is a real problem and well documented. I can add more documentation if wanted. – JVE999 May 15 '14 at 20:17
  • 1
    It must be a joke or something as someone else downvoted it. – JVE999 May 15 '14 at 20:20
  • 1
    Call findViewById on the rootView. – laalto May 15 '14 at 20:45
  • I don't know if I can write this comment here, but no other question with the same general topic could answer this, but this did, so I don't think it's a duplicate. – JVE999 May 15 '14 at 21:28
  • Even I am getting such error in few devices. I am not sure what is causing `WebView` to be null. I will add some screenies if my company allows it – Jimit Patel Nov 07 '16 at 06:10

4 Answers4

3

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.

To find the WebView do the following:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main, container, false);

    WebView siteViewer = (WebView) rootView.findViewById(R.id.webView);
    if(siteViewer != null)
        siteViewer.loadUrl("http://www.website.com");

    return rootView;
}

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.

Emanuel Moecklin
  • 28,488
  • 11
  • 69
  • 85
  • `PlaceHolderFragment` is `static`. Should I change it? I can't use `findViewById` in a static function. – JVE999 May 15 '14 at 20:19
  • 1) there are no functions in Java but methods, 2) PlaceHolderFragment is a class and the static means it's a static nested class meaning it's part of the Activity's code but still a class of its own (it can "live" outside the Activity as well unlike inner classes <- without the static). All non-static methods of PlaceHolderFragment are normal methods and you can call findViewById from there. – Emanuel Moecklin May 15 '14 at 20:25
  • I put it in the method `public View onCreateView` (not static), which is a method of the `PlaceholderFragment` (static) class. It tells me `Cannot make a static reference to the non-static method findViewById(int) from the type Activity` – JVE999 May 15 '14 at 20:31
  • 1
    Then you're doing something wrong but since you didn't post the Fragment code I can't tell you what is wrong. If you follow the question mentioned above (http://stackoverflow.com/q/23653778/534471) it will work. – Emanuel Moecklin May 15 '14 at 20:34
  • I updated it. I'm new to JAVA, but have experience in php and c++ among others. I'm looking at that question now. – JVE999 May 15 '14 at 20:39
  • 1
    I changed my answer to reflect your problem with retrieving the WebView in the fragment. – Emanuel Moecklin May 15 '14 at 20:46
1

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.

nikis
  • 11,166
  • 2
  • 35
  • 45
1

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

Ahmad
  • 437
  • 1
  • 4
  • 12
0

did you set the permission to access the internet in manifest file?

<uses-permission android:name="android.permission.INTERNET" />