Castor Troy
Castor Troy

Reputation: 51

NPE while loading webview

I'm having a NullPointerException having this code. I guess the problem is with the getView() but I don't know another way to do it.

public class Seccion1 extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.seccion3, container, false);

    return rootView;
}

@Override
public void onResume() {
    super.onResume();

    WebView mWebView;

    mWebView = (WebView) getView().findViewById(R.id.lectura_webView);

    //Archivo de ejemplo. Cambiar por el adecuado.
    --> THIS IS THE LINE THAT GIVES THE ERROR mWebView.loadUrl("file:///android_asset/productividad/ejemplo1.html");

    mWebView.getSettings().setBuiltInZoomControls(true);
    mWebView.getSettings().setDisplayZoomControls(false);

    mWebView.setLongClickable(true);
    mWebView.setOnLongClickListener(new View.OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            return true;
        }
    });

}

}

The log:

    java.lang.RuntimeException: Unable to resume activity {com.ejemplo/ejemplo.ejemplo.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2774)
            at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2803)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2238)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at   com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.ejemplo.Seccion1.onResume(Seccion1.java:33)
            at android.app.Fragment.performResume(Fragment.java:1743)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:924)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1044)
            at android.app.FragmentManagerImpl.dispatchResume(FragmentManager.java:1863)
            at android.app.Activity.performResume(Activity.java:5320)
            at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2764)
            at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2803)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2238)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)

Thank you!

P.D: I'm adding this text because it says that my post is mostly code. I think the question is clear and direct and I'm providing the data needed to identify properly the problem. Thank you again!

Edit 1

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ejemplo/com.ejemplo1.MainActivity}: java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
    at android.app.ActivityThread.access$800(ActivityThread.java:135)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5001)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
    at dalvik.system.NativeStart.main(Native Method)


Caused by: java.lang.NullPointerException
        at com.ejemplo1.Seccion1.onCreateView(Seccion1.java:29)
        at android.app.Fragment.performCreateView(Fragment.java:1700)
        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
        at android.app.BackStackRecord.run(BackStackRecord.java:684)
        at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
        at android.app.Activity.performStart(Activity.java:5240)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2157)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)

Edit 2:

Code Updated that returns the upper log:

public class Seccion1 extends Fragment {

WebView mWebView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.seccion3, container, false);


    mWebView = (WebView) rootView.findViewById(R.id.lectura_webView);

    //Archivo de ejemplo. Cambiar por el adecuado.
    mWebView.loadUrl("file:///android_asset/productividad/ejemplo1.html");

    mWebView.getSettings().setBuiltInZoomControls(true);
    mWebView.getSettings().setDisplayZoomControls(false);

    mWebView.setLongClickable(true);
    mWebView.setOnLongClickListener(new View.OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            return true;
        }
    });

    return rootView;

}

Upvotes: 1

Views: 225

Answers (3)

Naveen Tamrakar
Naveen Tamrakar

Reputation: 3339

public class Seccion1 extends Fragment 
{
 WebView mWebView;

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
 View rootView = inflater.inflate(R.layout.seccion3, container, false);
 mWebView = (WebView)rootView.findViewById(R.id.lectura_webView);
 //Archivo de ejemplo. Cambiar por el adecuado.
  --> THIS IS THE LINE THAT GIVES THE ERROR   
 mWebView.loadUrl("file:///android_asset/productividad/ejemplo1.html");

 mWebView.getSettings().setBuiltInZoomControls(true);
 mWebView.getSettings().setDisplayZoomControls(false);

 mWebView.setLongClickable(true);
 mWebView.setOnLongClickListener(new View.OnLongClickListener() {

    @Override
    public boolean onLongClick(View v) {
        return true;
    }
 });
 return rootView;
}
}

Upvotes: 0

Blaze Tama
Blaze Tama

Reputation: 10948

Delete this code from onResume :

 mWebView = (WebView) getView().findViewById(R.id.lectura_webView);

And add this code in onCreateView :

mWebView = (WebView) rootView.findViewById(R.id.lectura_webView);

You need to do this because mWebView is in the inflated layout (rootView).

Upvotes: 0

Giru Bhai
Giru Bhai

Reputation: 14398

declare WebView mWebView; as class variable and initialize in onCreateView as

mWebView = (WebView) rootView.findViewById(R.id.lectura_webView);

and remove below lines from onResume

WebView mWebView;

mWebView = (WebView) getView().findViewById(R.id.lectura_webView);

Upvotes: 1

Related Questions