在 if 条件中使用异常

我使用它是Firebase Auth为了管理我的登录并注册该应用程序。


我想根据收到的异常添加 toast 消息。


例如,如果例外是FirebaseAuthInvalidCredentialsException我想祝酒一条消息。如果是从那时FirebaseAuthUserCollisionException起我想再用一片吐司。


我用类似的东西:


auth.createUserWithEmailAndPassword(email, password)

        .addOnCompleteListener(SignUpActivity.this, new OnCompleteListener<AuthResult>() {

            @Override

            public void onComplete(@NonNull Task<AuthResult> task) {

                if (task.isSuccessful()) {

                    LayoutInflater inflater = LayoutInflater.from( SignUpActivity.this );

                    View toastview = inflater.inflate( R.layout.toast_registered, null );

                    Toast toast = new Toast( SignUpActivity.this );

                    toast.setView( toastview );

                    toast.setGravity( Gravity.CENTER, 0, 3 );

                    toast.setDuration( Toast.LENGTH_LONG );

                    toast.show();

                }


                if (!task.isSuccessful()) {

                    Toast.makeText(SignUpActivity.this, "toast1." + task.getException(),

                            Toast.LENGTH_SHORT).show();

                }


                if (!task.isSuccessful()) {

                    Toast.makeText(SignUpActivity.this, "toast2." + task.getException(),

                            Toast.LENGTH_SHORT).show();

                } else {

                    startActivity(new Intent(SignUpActivity.this, MainActivity.class));

                    finish();

                }

            }

        });

所以基本上我需要在 if 条件中添加一些东西,但我不确定什么。


我看到了使用catch但我不认为是这种情况。


MM们
浏览 119回答 2
2回答

白猪掌柜的

这应该可以做到:auth.createUserWithEmailAndPassword(email, password)&nbsp; &nbsp; &nbsp; &nbsp; .addOnCompleteListener(SignUpActivity.this, new OnCompleteListener<AuthResult>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onComplete(@NonNull Task<AuthResult> task) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (task.isSuccessful()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LayoutInflater inflater = LayoutInflater.from( SignUpActivity.this );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; View toastview = inflater.inflate( R.layout.toast_registered, null );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast toast = new Toast( SignUpActivity.this );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; toast.setView( toastview );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; toast.setGravity( Gravity.CENTER, 0, 3 );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; toast.setDuration( Toast.LENGTH_LONG );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; toast.show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Toast.makeText(SignUpActivity.this, "toast1." + task.getException(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (task.getException() instanceof FirebaseAuthUserCollisionException) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Toast.makeText(SignUpActivity.this, "toast2." + task.getException(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startActivity(new Intent(SignUpActivity.this, MainActivity.class));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; finish();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });因此,您检查任务是否成功,如果没有成功,则在开始注册活动和完成此活动之前打开错误类型并显示相应的 toast。

当年话下

首先,您只需要一个条件,例如task.isSuccessful()。你可以这样做:if(task.isSuccessful()) {&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; throw task.getException();&nbsp; &nbsp; } catch(FirebaseAuthInvalidCredentialsException e) {&nbsp; &nbsp; &nbsp; &nbsp; /*Toast here*/&nbsp; &nbsp; } catch(FirebaseAuthUserCollisionException e) {&nbsp; &nbsp; &nbsp; &nbsp; /*Toast here*/&nbsp; &nbsp; } catch(...) ...}和...throw task.getException();...你将抛出特定的异常并使用} catch(FirebaseAuthInvalidCredentialsException e) {&nbsp; &nbsp; /*Toast here*/}您将捕获特定的异常来处理该异常并执行您想要的操作,在您的情况下,您将编写代码以在 catch 语句中显示 toast。请记住捕获所有异常,否则当引发未处理的异常时,您的应用程序将崩溃。您还可以处理通用异常:if(task.isSuccessful()) {&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; throw task.getException();&nbsp; &nbsp; } catch(FirebaseAuthInvalidCredentialsException e) {&nbsp; &nbsp; &nbsp; &nbsp; /*Toast here*/&nbsp; &nbsp; } catch(FirebaseAuthUserCollisionException e) {&nbsp; &nbsp; &nbsp; &nbsp; /*Toast here*/&nbsp; &nbsp; } catch(Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; /*Handle generic exception*/&nbsp; &nbsp; }}再见
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java