android - Toast that only shows on debug variants -
i create simple helper class shows toast messages debug variants only.
used this:
toast.maketext(context, "debug toast message", toast.length_short).show();
toast.java
import android.annotation.suppresslint; import android.content.context; import android.support.annotation.nonnull; import android.widget.toast; import com.mypp.buildconfig; /** * toast shows debug build variants. */ @suppresslint("showtoast") public class toast extends toast { public toast(context context) { super(context); } @nonnull public static toast maketext(@nonnull context context, charsequence text, int duration) { return (toast) toast.maketext(context, text, duration); } @nonnull public static toast maketext(@nonnull context context, int resid, int duration) { return (toast) toast.maketext(context, resid, duration); } @override public void show() { if (buildconfig.debug) { super.show(); } } }
though cast failing in implementation:
caused by: java.lang.classcastexception: android.widget.toast cannot cast com.mypp.helpers.toast @ com.mypp.helpers.toast.maketext(toast.java:23)
you cannot typecast instance of toast
(a base class) derived class toast
, though other way round possible.
may suggest change implementation below:
import android.annotation.suppresslint; import android.content.context; import android.support.annotation.nonnull; import android.widget.toast; import com.mypp.buildconfig; /** * toast shows debug build variants. */ @suppresslint("showtoast") public class toast { private toast toast; public toast(toast toast) { this.toast = toast; } @nonnull public static toast maketext(@nonnull context context, charsequence text, int duration) { return new toast(toast.maketext(context, text, duration)); } @nonnull public static toast maketext(@nonnull context context, int resid, int duration) { return new toast(toast.maketext(context, resid, duration)); } public void show() { if (buildconfig.debug) { toast.show(); } } }
Comments
Post a Comment