在所有屏幕自定义敬酒的消息?自定义、屏幕、消息

2023-09-07 10:36:11 作者:鬼〆柏。

我有prepared 1 application.My应用程序有15个屏幕。现在,我想在所有的屏幕上显示自定义的烤面包的消息我已经prepared一种布局和inflated.It正在一screen.I希望与单一的方法所有的屏幕为显示敬酒消息我已经prepared在Java class.when一种方法,我想显示敬酒消息只是我叫method.but我得到nullpointerexecption.My code是,

I have prepared one application.My application have 15 screen .Now i want display custom toast message in all screens for that i have prepared one layout and inflated.It is working for one screen.I want display toast message in all screens with single method for that i have prepared one method in java class.when i want display toast message just i call method.but i got nullpointerexecption.My code is,

public static void showToastMessage(String message){

               LayoutInflater inflater = ((Activity) context).getLayoutInflater();

                View layout = inflater.inflate(R.layout.custom_toast,
                  (ViewGroup) ((Activity) context).findViewById(R.id.customToast));
            // set a message
                TextView text = (TextView) layout.findViewById(R.id.text);
                text.setText(message);

                // Toast...
                Toast toast = new Toast(context);
                toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                toast.setDuration(Toast.LENGTH_LONG);
                toast.setView(layout);
                toast.show();
           }

日志是

java.lang.NullPointerException

    at com.guayama.utilities.CommonMethods.showToastMessage(CommonMethods.java:474)

    at android.view.View.performClick(View.java:3511)

    at android.view.View$PerformClick.run(View.java:14105)

    at android.os.Handler.handleCallback(Handler.java:605)

    at android.os.Handler.dispatchMessage(Handler.java:92)

    at android.os.Looper.loop(Looper.java:137)

    at android.app.ActivityThread.main(ActivityThread.java:4424)

    at java.lang.reflect.Method.invokeNative(Native Method)

觉得我做了错误的一些地方,请帮助。

think i done mistake some where please help.

推荐答案

改变你的方法

showToastMessage(String message)

showToastMessage(Context context,String message);

似乎方面的问题我

it seems context problem to me

你的函数看起来像这样

public static void showToastMessage(Context context,String message){

               LayoutInflater inflater = context.getLayoutInflater();

                View layout = inflater.inflate(R.layout.custom_toast,
                  (ViewGroup) ((Activity) context).findViewById(R.id.customToast));
            // set a message
                TextView text = (TextView) layout.findViewById(R.id.text);
                text.setText(message);

                // Toast...
                Toast toast = new Toast(context);
                toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                toast.setDuration(Toast.LENGTH_LONG);
                toast.setView(layout);
                toast.show();
           }