Android 我希望面包片吃完后能马上消失
我希望面包片吃完后能马上消失 有一节ToastUtils课Android 我希望面包片吃完后能马上消失,android,toast,Android,Toast,我希望面包片吃完后能马上消失 有一节ToastUtils课 import android.content.Context; import android.widget.Toast; public class ToastUtils{ private static Toast mToast; public static void showToast(Context context, int msg, int duration) { if (mToast == nul
import android.content.Context;
import android.widget.Toast;
public class ToastUtils{
private static Toast mToast;
public static void showToast(Context context, int msg, int duration) {
if (mToast == null) {
mToast = Toast.makeText(context, msg, duration);
} else {
mToast.setText(msg);
}
mToast.show();
}
public static void showToast(Context context, String msg, int duration) {
if (mToast == null) {
mToast = Toast.makeText(context, msg, duration);
} else {
mToast.setText(msg);
}
mToast.show();
}
public static void clearToast(){
mToast.cancel();
}
}
许多人说,在顶部覆盖可以解决这个问题
因此,我覆盖了片段中的函数onStop
@Override
public void onStop(){
super.onStop();
ToastUtils.clearToast();
}
但它给出了一个例外
08-07 14:48:50.070 11178-11178/? W/System.err﹕ java.lang.NullPointerException
08-07 14:48:50.075 11178-11178/? W/System.err﹕ at com.shijiebang.offlinemap.utils.ToastUtils.clearToast(ToastUtils.java:30)
08-07 14:48:50.075 11178-11178/? W/System.err﹕ at com.shijiebang.offlinemap.ui.Fragment.LoginFragment.onStop(LoginFragment.java:173)
08-07 14:48:50.075 11178-11178/? W/System.err﹕ at android.app.Fragment.performStop(Fragment.java:1888)
08-07 14:48:50.075 11178-11178/? W/System.err﹕ at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:948)
08-07 14:48:50.075 11178-11178/? W/System.err﹕ at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1071)
08-07 14:48:50.075 11178-11178/? W/System.err﹕ at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1053)
08-07 14:48:50.075 11178-11178/? W/System.err﹕ at android.app.FragmentManagerImpl.dispatchStop(FragmentManager.java:1880)
08-07 14:48:50.075 11178-11178/? W/System.err﹕ at android.app.Activity.performStop(Activity.java:5471)
08-07 14:48:50.075 11178-11178/? W/System.err﹕ at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3713)
08-07 14:48:50.075 11178-11178/? W/System.err﹕ at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3772)
08-07 14:48:50.075 11178-11178/? W/System.err﹕ at android.app.ActivityThread.access$1700(ActivityThread.java:139)
08-07 14:48:50.075 11178-11178/? W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
08-07 14:48:50.075 11178-11178/? W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:102)
08-07 14:48:50.075 11178-11178/? W/System.err﹕ at android.os.Looper.loop(Looper.java:136)
08-07 14:48:50.075 11178-11178/? W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5314)
08-07 14:48:50.075 11178-11178/? W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
08-07 14:48:50.075 11178-11178/? W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
08-07 14:48:50.075 11178-11178/? W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
08-07 14:48:50.075 11178-11178/? W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:680)
08-07 14:48:50.075 11178-11178/? W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
我不知道原因。请帮忙 我认为它应该不会出错
public static void clearToast(MyActivity activity){
activity.runOnUiThread(new Runnable() {
if(mToast != null){
if (mToast.getView().isShown())
{
mToast.cancel();
}
}
}
}
像这样调用这个函数。将MyActivity
名称更改为您的活动
clearToast(this);
尝试使用
hide()
方法而不是cancel()
异常何时发生?你能发布完整的stacktrace吗?在谷歌上快速搜索,最后发现与AVOS Cloud相关的AVUncaughtExceptionHandler
。对不起,我在片段中使用了toast。我该怎么做?你必须通过clearToast和RunNuithRead的活动。那就行了。这里有一些关于片段内getActivity的信息。我尝试了这个解决方案,但是它仍然给出了RuntimeExceptionCorry…我找不到这个函数