我为自定义吐司创建了以下方法。
public void customToastMessage(String message){
LayoutInflater inf = (LayoutInflater)con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inf.inflate(R.layout.custom_toast_layout,(ViewGroup)findViewById(R.id.myCustomToast));
TextView toastMessage = layout.findViewById(R.id.myCustomToastText);
toastMessage.setText(message);
Toast warningMessage = Toast.makeText(con, message, Toast.LENGTH_LONG);
warningMessage.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 10);
warningMessage.setView(layout);
warningMessage.show();
}
只要这个方法存在于 MainActivity 中,它就可以正常工作,但是当我将它移到一个单独的类时,我得到:
“java.lang.IllegalStateException:无法在 android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:389) 处执行 android:onClick 的方法”。
我在下面的课程中需要改变什么?
public class MyCustomUI extends AppCompatActivity {
private static Context con;
public MyCustomUI(Context con){
this.con = con;
}
public void customToastMessage(String message){
LayoutInflater inf = (LayoutInflater)con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inf.inflate(R.layout.custom_toast_layout,(ViewGroup)findViewById(R.id.myCustomToast));
TextView toastMessage = layout.findViewById(R.id.myCustomToastText);
toastMessage.setText(message);
Toast warningMessage = Toast.makeText(con, message, Toast.LENGTH_LONG);
warningMessage.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 10);
warningMessage.setView(layout);
warningMessage.show();
}
}
阿晨1998
相关分类