2012-07-05 26 views
1

我已经开发出了拥有15个屏幕一个应用程序。现在我想在所有这15个屏幕中显示自定义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) 
+1

和你的logcat错误报告? –

+0

哪条线是474线? –

+0

更新logcat的......看看 –

回答

6

改变你的方法

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

thanku so mach it working –

+0

@Chiru欢迎您。 – Akram

1

,我认为这是问题,

(Activity) context 

您还没有传递的上下文对象以这种方法,你正在尝试引用您可能已在全局声明的某个上下文对象。

所以在这一点,如果你的语境对象为空,你会得到空指针。试图通过你当前活动的conetxt在showToastMessage()的参数

+0

thanku so mach it working –

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

thanku so mach它工作正常 –

2

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

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

} 
0

我工作过定制烤面包,所以请按照以下流程操作,您将使用常用方法多次。

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