Pregunta

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;
    }
}
¿Fue útil?

Solución

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.

Otros consejos

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?

<uses-permission android:name="android.permission.INTERNET" /> 
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top