所有屏幕中的自定义Toast消息?

时间:2012-07-05 07:49:46

标签: android android-toast

我开发了一个有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)

5 个答案:

答案 0 :(得分:7)

更改方法

来自

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

答案 1 :(得分:3)

可以在任何项目中使用的Toast的自定义类实现。

Sent Events

答案 2 :(得分: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();
}

答案 3 :(得分:1)

我认为这是问题,

(Activity) context

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

因此,如果您的Context Object为null,您将获得NullPointer。尝试在showToastMessage()

的参数中传递Current Activity的conetxt

答案 4 :(得分: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);
相关问题