Yellow highlight while using custom Toast in android -
i trying implement custom toast , below code wrote , hooked onclickelistner asusual
button customtoastbutton = (button) this.findviewbyid(r.id.add_to_cart); customtoastbutton.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { //get layoutinflater , inflate custom_toast layout layoutinflater inflater = getlayoutinflater(); view layout = inflater.inflate(r.layout.cart_toast, (viewgroup) findviewbyid(r.id.toast_layout_root)); //get textview custom_toast layout textview text = (textview) layout.findviewbyid(r.id.toasttext); text.settext("item been added cart"); //create toast object, set display duration, //set view layout that's inflated above , call show() toast t = new toast(getapplicationcontext()); t.setduration(toast.length_short); t.setview(layout); t.show(); } });
and code used highlighted in yellow showing below error.well it's not showing error in logcat , code works fine.the highlightend part says ' may produce 'java.lang.nullpointerexception'
method invocation 'customtoastbutton.setonclicklistener(new view.onclicklistener() { public void onclick(vi...' may produce 'java.lang.nullpointerexception' less... (ctrl+f1) inspection analyzes method control , data flow report possible conditions true or false, expressions value statically proven constant, , situations can lead nullability contract violations. variables, method parameters , return values marked @nullable or @notnull treated nullable (or not-null, respectively) , used during analysis check nullability contracts, e.g. report possible nullpointerexception errors. more complex contracts can defined using @contract annotation, example: @contract("_, null -> null") — method returns null if second argument null @contract("_, null -> null; _, !null -> !null") — method returns null if second argument null , not-null otherwise @contract("true -> fail") — typical assertfalse method throws exception if true passed inspection can configured use custom @nullable @notnull annotations (by default ones annotations.jar used)
i don't understand reason causing error,i kindly request have @ it. thankyou in advance
you check if customtoastbutton
not null before setting onclicklistener
if(customtoastbutton!=null){ customtoastbutton.setonclicklistener(...) ... }
the warning shown, because if button
not exist , continue working nullpointerexception
thrown.
Comments
Post a Comment