0

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>
  • If you get NPE at inflate code, the reason seem to because getActivity() is null. So why is it null? It's usually relate to fragment's lifecycle. Please check it. If you still don't know why, reveal more code around when you load url and fragment lifecycle code – justHooman Jul 31 '15 at 01:28
  • @Minhtdh please check the 3 line para under the answer by '@Niv'.. That worked for me.. using the FragmentActivity fragActivity = getActivity(); – Vidur Singh Aug 01 '15 at 08:54

1 Answers1

0

Try toast like this and look.

public void showCustomAlert()
{

    Context context = getActivity();
    // Create layout inflator object to inflate toast.xml file
    LayoutInflater inflater = getActivity().getLayoutInflater();

    // Call toast.xml file for toast layout
    View toastRoot = inflater.inflate(R.layout.list_adapter, null);

    Toast toast = new Toast(context);

    // Set layout to toast
    toast.setView(toastRoot);
    toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL,
            0, 0);
    toast.setDuration(Toast.LENGTH_LONG);
    toast.show();

}

Then you have to do 1 thing . make an object of FragmentActivity as onCreateView FragmentActivity fragActivity=getActivity(); then you have to call LayoutInflater inflater = fragActivity.getLayoutInflater(); in your shouldOverrideUrlLoading Method

Nivedh
  • 971
  • 1
  • 8
  • 19
  • How is this different from what i've written in my code..? I am getting the null pointer exception at - LayoutInflater nflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); – Vidur Singh Jul 30 '15 at 11:31
  • Look the edited code and let me know whether it works for you. in my case it got worked – Nivedh Jul 30 '15 at 12:02
  • I tried your code and the thing is.. I'm only getting nullpointerexception at the line - LayoutInflater inflater = getActivity().getLayoutInflater(); ... Similar to my code..the call to LayoutInflater is what is giving the NPE.. the toast in itself is fine..The layoutinflater is the problem.... – Vidur Singh Jul 30 '15 at 14:13
  • I edited my question and have added my toast2.xml file which i'm trying to inflate.. theres just a textview in it.. so its really the LayoutInflater.. The thing is.. I only get this NPE if i instantaneously exit the application after starting the application.. without waiting for the url to load.. – Vidur Singh Jul 30 '15 at 14:29
  • Then you have to do 1 thing . make an object of FragmentActivity as onCreateView FragmentActivity fragActivity=getActivity(); then you have to call LayoutInflater inflater = fragActivity.getLayoutInflater(); in your shouldOverrideUrlLoading Method – Nivedh Jul 31 '15 at 04:45
  • It worked! Thanks alot man! you can edit your answer and i'll accept it.. ! How did you know that this might work? – Vidur Singh Jul 31 '15 at 10:13
  • I've accepted the answer.. but can't upvote it.. haven't earned that many reputations.. Do you have any suggestions on how to reduce the Actionbar height? I can't figure out a way to do that.. – Vidur Singh Aug 01 '15 at 08:51