Toast不工作 ,当要显示toast时App停止

问题描述 投票:-4回答:3

我创建了一个小应用程序来检查用户输入的密码是否有效。它能够检查,但它没有显示吐司,一旦我点击按钮,它显示“不幸的是,你的应用程序已停止工作”。我正在使用我的设备进行部署。请帮我看看,为什么烤面包不起作用。我使用了一个命令,它在编辑文本字段中设置变量a,b,c的值,以检查它是否正确。是的,这是正确的。所以问题在于我认为的吐司。

public class second extends AppCompatActivity {

    public EditText fname ;
    public EditText lname ;
    public EditText email ;
    public EditText pass ;
    public EditText blood;
    public EditText cpass;
    public EditText add ;
    public EditText mob ;
    public Toast t ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        fname = (EditText) findViewById(R.id.fname);
        lname = (EditText) findViewById(R.id.lname);
        email = (EditText) findViewById(R.id.email);
        pass = (EditText) findViewById(R.id.pass);
        add = (EditText) findViewById(R.id.add);
        cpass = (EditText) findViewById(R.id.cpass);
        mob = (EditText) findViewById(R.id.mob);
        blood = (EditText) findViewById(R.id.blood);
        Button sign = (Button) findViewById(R.id.sign);

        sign.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String sfname = fname.getText().toString();
                String spass = pass.getText().toString();
                String scpass = cpass.getText().toString();
                validate(spass, scpass);
            }
        });
    }
        public void validate(String spass ,String scpass){
        int a =0;
        int b =0;
        int c =0;
          t = new Toast(this);
        int len = spass.length();
        for(int i =0;i<len;i++){
            char d = spass.charAt(i);
            if(d>=48 && d<=57){
                a++;
            }
            if(d>=65 && d<=90){
                b++;
            }
            if(d>=33 && d<=47){
                c++;
            }
        }
        email.setText(a+" "+b+" "+c);

        if(a==0 || b==0 || c==0){
            t.makeText(this, "Password should contain atleast one special character , one capital letter and one number", Toast.LENGTH_LONG);
            t.show();

        } else {
            if(spass.equals(scpass)){
                t.makeText(this,"login succesful",Toast.LENGTH_SHORT);
                t.show();
            } else {
                t.makeText(this,"passwords dont match",Toast.LENGTH_SHORT).show();

            }
        }

    }
    }
android toast
3个回答
1
投票

试试这个

Toast toast = Toast.makeText(context, text, duration);
toast.show()

要么

Toast.makeText(context, text, duration).show();

0
投票

它说,来自Docs

Toast(Context context)构造一个空的Toast对象。您必须先调用setView(View)才能调用show()。

因此,当您从其构造函数创建Toast对象时,您认为您正在尝试创建Custom Toast

如果您没有创建任何,请使用如下:

public Toast t;    // Global variable

现在你的validate方法内:

t = Toast.makeText(this, "Password should contain atleast one special character , one capital letter and one number", Toast.LENGTH_LONG);
t.show();

0
投票

我对你的方法做了一些改动,尝试使用这个。你可以在https://stackoverflow.com/a/21963343找到更多细节

public void validate(String spass ,String scpass){
    int a =0;
    int b =0;
    int c =0;
//        Toast t = new Toast(this);
    int len = spass.length();
    for(int i =0;i<len;i++){
        char d = spass.charAt(i);
        if(d>=48 && d<=57){
            a++;
        }
        if(d>=65 && d<=90){
            b++;
        }
        if(d>=33 && d<=47){
            c++;
        }
    }
    email.setText(a+" "+b+" "+c);

    if(a==0 || b==0 || c==0){

        /*
        * updated
        * */
        Toast.makeText(this, "Password should contain atleast one special character , one capital letter and one number", Toast.LENGTH_LONG).show();


    } else {
        if(spass.equals(scpass)){
            /*Updated*/
            Toast.makeText(this,"login succesful",Toast.LENGTH_SHORT).show();
        } else {
            /*Updated*/
            Toast.makeText(this,"passwords dont match",Toast.LENGTH_SHORT).show();

        }
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.