android - NullPointerException while inflating custom toast message in shouldOverrideUrlLoading() method -


i have app displays webview , every time url loads in webview try show custom toast message says "please wait".. inflate custom toast message in shouldoverrideurlloading(webview view, string url) method.. app works fine if start app , wait url load..and message appears while loading url..

the problem comes when start application , exit it.. without waiting url load.. app crashes giving me null pointer exception @ --

layoutinflater iflater = (layoutinflater)getactivity().getsystemservice(context.layout_inflater_service);

i'm not able figure out way prevent exception , app crashing-

here code-

public boolean shouldoverrideurlloading(webview view, string url) {         log.i(tag, "about 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 error in 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):     @        com.example.itslive.web1activity$hellowebviewclient.shouldoverrideurlloading(web1activity.java:673)  07-30 15:26:25.429: e/androidruntime(8701):    @ android.webkit.callbackproxy.handlemessage(callbackproxy.java:384) 07-30 15:26:25.429: e/androidruntime(8701):     @ android.os.handler.dispatchmessage(handler.java:99) 07-30 15:26:25.429: e/androidruntime(8701):     @ android.os.looper.loop(looper.java:137) 07-30 15:26:25.429: e/androidruntime(8701):     @ android.app.activitythread.main(activitythread.java:4921) 07-30 15:26:25.429: e/androidruntime(8701):     @ java.lang.reflect.method.invokenative(native method) 07-30 15:26:25.429: e/androidruntime(8701):     @ java.lang.reflect.method.invoke(method.java:511) 07-30 15:26:25.429: e/androidruntime(8701):     @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1027) 07-30 15:26:25.429: e/androidruntime(8701):     @ com.android.internal.os.zygoteinit.main(zygoteinit.java:794) 07-30 15:26:25.429: e/androidruntime(8701):     @ dalvik.system.nativestart.main(native method) 

this 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> 

try toast , look.

public void showcustomalert() {      context context = getactivity();     // create layout inflator object inflate toast.xml file     layoutinflater inflater = getactivity().getlayoutinflater();      // call toast.xml file toast layout     view toastroot = inflater.inflate(r.layout.list_adapter, null);      toast toast = new toast(context);      // set layout toast     toast.setview(toastroot);     toast.setgravity(gravity.center_horizontal | gravity.center_vertical,             0, 0);     toast.setduration(toast.length_long);     toast.show();  } 

then have 1 thing . make object of fragmentactivity oncreateview fragmentactivity fragactivity=getactivity(); have call layoutinflater inflater = fragactivity.getlayoutinflater(); in shouldoverrideurlloading method


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -