我开发了一个有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)
更改您的方法
从
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();
}
任何项目中使用的 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();
}
}
传入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();
}
我认为这就是问题所在,
(Activity) context
您尚未将上下文对象传递给此方法,并且您正在尝试引用一些可以全局声明的上下文对象。
因此,此时如果您的上下文对象为空,您将获得 NullPointer。尝试在showToastMessage()的参数中传递当前活动的conetxt
我研究过自定义吐司,所以请按照以下过程进行操作,您将多次使用常用方法。
我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);