I have an app which displays a webView and every time a url loads in the webView i try to show a custom toast message which says "Please wait".. so i inflate this custom toast message in the shouldOverrideUrlLoading(WebView view, String url) method.. The app works fine if i start the app and wait for the url to load..and the message also appears while loading the url..
The problem comes when i start the application and then suddenly exit it.. without waiting for the url to load.. the app crashes giving me a null pointer exception at --
LayoutInflater iflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
I'm not able to figure out a way to prevent this exception and the app from crashing-
Here is my Code-
public boolean shouldOverrideUrlLoading(WebView view, String url) {
        Log.i(TAG, "About to load:" + url);
        view.loadUrl(url);
        LayoutInflater nflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = nflater.inflate(R.layout.toast2,
                (ViewGroup) getActivity().findViewById(R.id.tl1));
         TextView textv = (TextView) layout.findViewById(R.id.text);
         textv.setText(R.string.loading);
         final Toast ltoast = new Toast(getActivity());
         ltoast.setView(layout);
         ltoast.show();
         new CountDownTimer(9000, 1000)
         {
             public void onTick(long millisUntilFinished) {ltoast.show();}
             public void onFinish() {ltoast.show();}
         }.start();
        return true;
    }
Here is the error in my Logcat-
07-30 15:26:25.429: E/AndroidRuntime(8701): FATAL EXCEPTION: main
07-30 15:26:25.429: E/AndroidRuntime(8701): java.lang.NullPointerException
07-30 15:26:25.429: E/AndroidRuntime(8701):     at        com.example.itslive.web1Activity$HelloWebViewClient.shouldOverrideUrlLoading(web1Activity.java:673)
 07-30 15:26:25.429: E/AndroidRuntime(8701):    at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:384)
07-30 15:26:25.429: E/AndroidRuntime(8701):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-30 15:26:25.429: E/AndroidRuntime(8701):     at android.os.Looper.loop(Looper.java:137)
07-30 15:26:25.429: E/AndroidRuntime(8701):     at android.app.ActivityThread.main(ActivityThread.java:4921)
07-30 15:26:25.429: E/AndroidRuntime(8701):     at java.lang.reflect.Method.invokeNative(Native Method)
07-30 15:26:25.429: E/AndroidRuntime(8701):     at java.lang.reflect.Method.invoke(Method.java:511)
07-30 15:26:25.429: E/AndroidRuntime(8701):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
07-30 15:26:25.429: E/AndroidRuntime(8701):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
07-30 15:26:25.429: E/AndroidRuntime(8701):     at dalvik.system.NativeStart.main(Native Method)
This is my toast2.xml file...
 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/tl1"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="vertical" >
  <TextView android:id="@+id/text"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:textColor="@color/blue"
          android:paddingLeft="30dp"
          android:paddingRight="30dp"
          />
  </LinearLayout>