0

I have an activity with viewPager called WebActivity.The viewPager use instances of a webView Fragment(the fragment contains a webview inside it).I change the title of the WebActivity to "Loading.." from the fragments inside the viewPager to indicate the webView is loading.It does not cause any problem but when i move through the pages a lot i get a crash with logs as shown below:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.FragmentActivity.setTitle(java.lang.CharSequence)' on a null object reference
  at com.example.android.gametalks.Fragments.WebViewFragment$1.onPageFinished(WebViewFragment.java:91)
  at com.android.webview.chromium.WebViewContentsClientAdapter.onPageFinished(WebViewContentsClientAdapter.java:199)
  at org.chromium.android_webview.AwContentsClientCallbackHelper$MyHandler.handleMessage(AwContentsClientCallbackHelper.java:72)
  at android.os.Handler.dispatchMessage(Handler.java:102)
  at android.os.Looper.loop(Looper.java:168)
  at android.app.ActivityThread.main(ActivityThread.java:5885)
  at java.lang.reflect.Method.invoke(Native Method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)

Here is my fragment(Relevant part):

public class WebViewFragment extends Fragment {

    WebView mWebView;
    String currentUrl;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        currentUrl = getArguments().getString("currentUrl");
        super.onCreate(savedInstanceState);
    }

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


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        getActivity().setTitle("Web View");

        setRetainInstance(true);
        mWebView = (WebView) view.findViewById(R.id.webvieww);

        mWebView.setWebViewClient(new WebViewClient() {
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Toast.makeText(getActivity(), description, Toast.LENGTH_SHORT).show();


            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon)
            {
                getActivity().setTitle("Loading...");
            }


            @Override
            public void onPageFinished(WebView view, String url) {

                super.onPageFinished(view, url);
                getActivity().setTitle("Web View");
            }
        });


        if (savedInstanceState == null)
        {
            mWebView.loadUrl(currentUrl);
        }

    }
Juan Carlos Mendoza
  • 5,736
  • 7
  • 25
  • 50
Arjun Anil
  • 97
  • 12

1 Answers1

1

Short answer: check if getActivity() is returning null because accessing it.

if(getActivity() != null) {
    getActivity().setTitle("New title");
}

Long answer: it's best to use some sort of listener to notify the Activity of a change within the fragment rather than directly using getActivity(). The Google Developer docs have a pretty good walkthrough of how to accomplish this using an interface.

wchristiansen
  • 436
  • 7
  • 20