签出firebase android studio

时间:2018-05-28 15:40:51

标签: android firebase logout

我退出菜单活动后代码崩溃,它会打开徽标活动,但它不起作用并且在

之后立即停止工作
else if (id == R.id.nav_sair) {

                AlertDialog.Builder builder=new AlertDialog.Builder(RealMenuActivity.this); //Home is name of the activity
                builder.setMessage("Deseja sair?");
                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {

                        finish();
                        Intent i=new Intent(RealMenuActivity.this, LogoActivity.class);
                        i.putExtra("finish", true);
                        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // To clean up all activities
                        //startActivity(i);
                        startActivity( i );
                        FirebaseAuth.getInstance().signOut();



                        finish();
                        return;




                    }
                });

这是徽标活动,我认为错误就在这里,但我不确定

private FirebaseAuth mAuth;
    private FirebaseAuth.AuthStateListener firebaseAuthListener;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_logo );


        mAuth = FirebaseAuth.getInstance();

        firebaseAuthListener = new FirebaseAuth.AuthStateListener() {



            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
                if (user != null) {

                    Intent it = new Intent( LogoActivity.this, RealMenuActivity.class );
                    startActivity( it );
                    finish();
                    return;
                }

            }
        };

        mEmail = (EditText) findViewById( R.id.Email );
        mPassword = (EditText) findViewById( R.id.Password );
        mbtnLogin = (ImageButton) findViewById( R.id.loginButton );

        mRegistro = (Button) findViewById( R.id.Registro );

        mRegistro.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(LogoActivity.this, RegistroActivity.class);
                startActivity(it);
            }
        } );

        mbtnLogin.setOnClickListener( new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                final String Email = mEmail.getText().toString();
                final String Password = mPassword.getText().toString();
                mAuth.signInWithEmailAndPassword( Email, Password).addOnCompleteListener( LogoActivity.this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (!task.isSuccessful()){
                            Toast.makeText( LogoActivity.this, "Erro ao Entrar. Tente novamente", Toast.LENGTH_SHORT ).show();
                        }

                    }
                } );
            }
        } );
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    protected void onStart() {
        super.onStart();
        mAuth.addAuthStateListener( firebaseAuthListener );
    }

    @Override
    protected void onStop() {
        super.onStop();

        if (firebaseAuthListener != null) {
            mAuth.removeAuthStateListener(firebaseAuthListener);
            FirebaseAuth.getInstance().signOut();

        }
    }
}

它打开徽标活动后立即崩溃!很快我会添加logcat! 某事可以帮助我吗?先谢谢!

logcat的

java.lang.RuntimeException:无法停止活动{com.example.norio.amosou / com.example.norio.amosou.RealMenuActivity}:java.lang.NullPointerException:尝试调用虚方法'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()'在空对象引用上         在android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4331)         在android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:4393)         在android.app.ActivityThread.-wrap6(ActivityThread.java)         在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1618)         在android.os.Handler.dispatchMessage(Handler.java:110)         在android.os.Looper.loop(Looper.java:203)         在android.app.ActivityThread.main(ActivityThread.java:6339)         at java.lang.reflect.Method.invoke(Native Method)         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1084)         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:945)      引发者:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()'         在com.example.norio.amosou.RealMenuActivity.onStop(RealMenuActivity.java:472)         在android.app.Instrumentation.callActivityOnStop(Instrumentation.java:1311)         在android.app.Activity.performStop(Activity.java:6864)

2 个答案:

答案 0 :(得分:0)

原因是你在onclick

中做了2次结束()
@Override
                    public void onClick(DialogInterface dialog, int id) {

                        finish();
                        Intent i=new Intent(RealMenuActivity.this, LogoActivity.class);
                        i.putExtra("finish", true);
                        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // To clean up all activities
                        //startActivity(i);
                        startActivity( i );
                        FirebaseAuth.getInstance().signOut();



                        finish();

只需移除第一个,因为您在访问意图和注销之前就已杀死了您的活动

@Override
                    public void onClick(DialogInterface dialog, int id) {


                        Intent i=new Intent(RealMenuActivity.this, LogoActivity.class);
                        i.putExtra("finish", true);
                        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // To clean up all activities
                        //startActivity(i);
                        startActivity( i );
                        FirebaseAuth.getInstance().signOut();



                        finish();

答案 1 :(得分:0)

您的错误实际上是在firebaseAuthListener中尝试执行此FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();。 您没有在onAuthStateChange

中使用firebaseAuth
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {

}

您应该做的是这个FirebaseUser user = fireBaseAuth.getCurrecntUser()而不是这个FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();