GrenderG / Toasty

The usual Toast, but with steroids 💪
GNU Lesser General Public License v3.0
6.57k stars 809 forks source link

Toasty.custom #70

Open angel888808888 opened 6 years ago

angel888808888 commented 6 years ago

用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)

GrenderG commented 6 years ago

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

By the way, are you using version 1.3.0?

angel888808888 commented 6 years ago

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

GrenderG commented 6 years ago

Check if your msg parameter is null.

angel888808888 commented 6 years ago

it is not null

GrenderG commented 6 years ago

What device are you testing this on?

angel888808888 commented 6 years ago

Redmi 4A

mbsysde99 commented 6 years ago

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);
            }
        });
    }
}