1

我开发了一个有 15 个屏幕的应用程序。现在我想在所有这 15 个屏幕中显示自定义 toast 消息。为此,我夸大了一种布局。但它只能在一个屏幕上工作。因此,我编写了一个方法来Toast在所有屏幕上显示自定义。每当我想显示 toast 消息时,我都会调用该方法。但我得到了java.lang.NullPointerException。如何解决这个问题?以下是我的代码,

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

5 回答 5

8

改变你的方法

showToastMessage(String message)

showToastMessage(Context context,String message);

对我来说似乎是上下文问题

你的功能看起来像这样

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();
           }
于 2012-07-05T07:53:31.707 回答
5

Toast 的自定义类实现,可用于任何项目。

public class ToastMessage {
        private Context context;
        private static ToastMessage instance;

    /**
     * @para
   m context
     */
    private ToastMessage(Context context) {
        this.context = context;
    }

    /**
     * @param context
     * @return
     */
    public synchronized static ToastMessage getInstance(Context context) {
        if (instance == null) {
            instance = new ToastMessage(context);
        }
        return instance;
    }

    /**
     * @param message
     */
    public void showLongMessage(String message) {
        Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
    }

    /**
     * @param message
     */
    public void showSmallMessage(String message) {
        Toast.makeText(context, message, Toast.LENGTH_LONG).show();
    }

    /**
     * The Toast displayed via this method will display it for short period of time
     *
     * @param message
     */
    public void showLongCustomToast(String message) {

        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        View layout = inflater.inflate(R.layout.layout_custom_toast, (ViewGroup) ((Activity) context).findViewById(R.id.ll_toast));
        TextView msgTv = (TextView) layout.findViewById(R.id.tv_msg);
        msgTv.setText(message);
        Toast toast = new Toast(context);
        toast.setGravity(Gravity.FILL_HORIZONTAL | Gravity.BOTTOM, 0, 0);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(layout);
        toast.show();

    }

    /**
     * The toast displayed by this class will display it for long period of time
     *
     * @param message
     */
    public void showSmallCustomToast(String message) {

        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        View layout = inflater.inflate(R.layout.layout_custom_toast, (ViewGroup) ((Activity) context).findViewById(R.id.ll_toast));
        TextView msgTv = (TextView) layout.findViewById(R.id.tv_msg);
        msgTv.setText(message);
        Toast toast = new Toast(context);
        toast.setGravity(Gravity.FILL_HORIZONTAL | Gravity.BOTTOM, 0, 0);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setView(layout);
        toast.show();
    }

}
于 2016-10-18T08:38:27.277 回答
2

传入Context并将其用作showToastMessage(String message,Context context)

因此:

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();
}
于 2012-07-05T07:53:52.467 回答
1

我认为这是问题所在,

(Activity) context

您尚未将上下文对象传递给此方法,并且您正在尝试引用一些您可以全局声明的上下文对象。

因此,此时如果您的 Context Object 为 null,您将获得 NullPointer。尝试在 showToastMessage() 的参数中传递当前活动的 conetxt

于 2012-07-05T07:52:39.280 回答
1

我一直在定制吐司,所以请按照以下过程,您将多次使用常用方法。

我的 custom_toast.xml 是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/custom_toast_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/white">

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        card_view:cardBackgroundColor="@color/grey"
        card_view:cardCornerRadius="6dp"
        card_view:cardElevation="6dp"
        card_view:contentPadding="25dp"
       >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center">

            <ImageView
                android:id="@+id/custom_toast_image"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:gravity="center_vertical"
                android:src="@drawable/ic_launcher"/>

            <TextView
                android:id="@+id/custom_toast_message"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:textSize="16dp"/>

        </LinearLayout>

    </android.support.v7.widget.CardView>
</LinearLayout>

第二件事是创建一个 java 类:比如 CustomToast.java

public class CustomToast {
Context context;



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

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

    View layout = inflater.inflate(R.layout.customtoast,
            (ViewGroup) ((Activity) context).findViewById(R.id.custom_toast_layout));
    // set a message
    TextView text = (TextView) layout.findViewById(R.id.custom_toast_message);
    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();
}

}

第三步在您的活动中创建 CustomToast.java 类的对象,并通过传递上下文和消息来调用该方法。

 CustomToast customToast=new CustomToast();
  customToast.showToastMessage(ctx,message);
于 2018-01-06T10:12:05.213 回答