0
Toast.makeText(net.asdqwe.activities.Signup.this, configurationz.ERROR_MESSAGES_SIGNUP_PASSWORDS_DO_NOT_MATCH, Toast.LENGTH_SHORT).setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL, 0, 0);

Toast.show();

此代码不起作用。Toast.show() 被标记为红色,错误如下:

Cannot make a static reference to the non-static method show() from the type Toast

Toast.makeText(net.asdqwe.activities.Signup.this, 
        configurationz.ERROR_MESSAGES_SIGNUP_PASSWORDS_DO_NOT_MATCH,
        Toast.LENGTH_SHORT)
            .setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL, 0, 0)
            .show();

这也不起作用,给我以下错误:

Cannot invoke show() on the primitive type void
4

2 回答 2

2

Show 不是静态方法,因此您需要使用通过 makeText 方法创建的实例。你可以这样做:

Toast myToast = Toast.makeText(net.asdqwe.activities.Signup.this, configurationz.ERROR_MESSAGES_SIGNUP_PASSWORDS_DO_NOT_MATCH, Toast.LENGTH_SHORT);
myToast.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL, 0, 0);
myToast.show();
于 2013-09-26T19:23:02.820 回答
1

show() 方法不是静态方法,所以不能调用 Toast.show();

相反,您应该使用以下。

Toast toast=Toast.makeText(net.asdqwe.activities.Signup.this, configurationz.ERROR_MESSAGES_SIGNUP_PASSWORDS_DO_NOT_MATCH, Toast.LENGTH_SHORT);

toast.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL, 0, 0);

toast.show();
于 2013-09-26T19:23:43.747 回答