我有这样的静态方法:findViewById一个静态方法
public static void displayLevelUp(int level, Context context) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_level_coast,
(ViewGroup) findViewById(R.id.toast_layout_root)); // this row
TextView text = (TextView) layout.findViewById(R.id.toastText);
text.setText("This is a custom toast");
Toast toast = new Toast(context);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
Toast.makeText(context, String.valueOf(level), Toast.LENGTH_SHORT)
.show();
}
但是,我无法弄清楚如何获得第一findViewById
为它说,这是一个非静态方法发挥好本。我明白为什么会这样说,但必须有一种解决方法?我将context
传入此方法,但无法一起工作。
您不需要传递上下文对象,而是传递视图对象。一个持有你的意见 – edoardotognoni 2013-05-02 15:33:59
为什么不只是使视图类宽变量,并访问它的方式 – tyczj 2013-05-02 15:34:44
@Wamasa这是正确的答案..信息它 – Pragnani 2013-05-02 15:37:40