Toasty icon indicating copy to clipboard operation
Toasty copied to clipboard

Toasty.custom

Open angel888808888 opened this issue 7 years ago • 7 comments

用Toasty.info显示toast报NullPointerException

Attempt to invoke interface method 'java.lang.String java.lang.CharSequence.toString()' on a null object reference at android.widget.ToastInjector.addAppName(ToastInjector.java:50) at android.widget.Toast.makeText(Toast.java:273) at es.dmoral.toasty.Toasty.custom(Toasty.java:171)

angel888808888 avatar May 15 '18 11:05 angel888808888

Hi, this doesn't provide enough information. Please, show where are you calling Toasty.

By the way, are you using version 1.3.0?

GrenderG avatar May 15 '18 21:05 GrenderG

yes,I use 1.3.0,exception: public static void showToast(Context context,int type,String msg,boolean isWithIcon){ switch (type) { case 1: Toasty.info(context,msg, Toast.LENGTH_SHORT, isWithIcon).show(); break; case 2: Toasty.success(context,msg, Toast.LENGTH_SHORT, isWithIcon).show(); break; case 3: Toasty.warning(context,msg, Toast.LENGTH_SHORT, isWithIcon).show(); break; case 4: Toasty.error(context,msg, Toast.LENGTH_SHORT, isWithIcon).show(); break; } }

error : 05-17 09:40:42.975 15142-15142/com.creatoo.checkticket W/System.err: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.String java.lang.CharSequence.toString()' on a null object reference at android.widget.ToastInjector.addAppName(ToastInjector.java:50) at android.widget.Toast.makeText(Toast.java:273) at es.dmoral.toasty.Toasty.custom(Toasty.java:171) at es.dmoral.toasty.Toasty.warning(Toasty.java:100) at com.force.librarybase.utils.ToastUtils.showToast(ToastUtils.java:69) at com.force.librarybase.network.net.common.DefaultObserver.onException(DefaultObserver.java:136) at com.force.librarybase.network.net.common.DefaultObserver.onError(DefaultObserver.java:73) at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.checkTerminated(ObservableObserveOn.java:276) at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.drainNormal(ObservableObserveOn.java:172) at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.run(ObservableObserveOn.java:252) at io.reactivex.android.schedulers.HandlerScheduler$ScheduledRunnable.run(HandlerScheduler.java:109) at android.os.Handler.handleCallback(Handler.java:742)

I find that Toasy.custom having NullPointerException

angel888808888 avatar May 17 '18 01:05 angel888808888

Check if your msg parameter is null.

GrenderG avatar May 18 '18 21:05 GrenderG

it is not null

angel888808888 avatar May 21 '18 11:05 angel888808888

What device are you testing this on?

GrenderG avatar May 21 '18 21:05 GrenderG

Redmi 4A

angel888808888 avatar May 26 '18 09:05 angel888808888

Are you call toasty from background thread? If yes, try this

private static void toast(final Context context, final String text, final int duration, final TOAST_TYPE type){
    switch (type) {
        case ERROR:
            Toasty.error(context, text, duration, false).show();
            break;
        case SUCCESS:
            Toasty.success(context, text, duration, false).show();
            break;
        case INFO:
            Toasty.info(context, text, duration, false).show();
            break;
        case WARNING:
            Toasty.warning(context, text, duration, false).show();
            break;
        case NORMAL:
            Toasty.normal(context, text, duration).show();
            break;
    }
}

public static void showToast(final Context context, final String text, final int duration, final TOAST_TYPE type) {
    if(Looper.myLooper() == Looper.getMainLooper()){
        // call from foreground thread
        toast(context, text, duration, type);
    }
    else {
        // call from background thread
        new Handler(context.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                toast(context, text, duration, type);
            }
        });
    }
}

mbsysde99 avatar Sep 22 '18 12:09 mbsysde99