登出Firebase Android Studio

问题描述 投票:0回答:1

我退出菜单活动后,代码崩溃了,它打开了徽标活动,但它不起作用,并在此之后立即停止工作

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! sb可以帮助我吗?谢谢!

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)在NULL。 android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1618)上的.ActivityThread.wrap6(ActivityThread.java)android.os.Looper.loop上的android.os.Handler.dispatchMessage(Handler.java:110) (Looper.java:203)在android.app.ActivityThread.main(ActivityThread.java:6339)在java.lang.reflect.Method.invoke(本机方法)在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:1084)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:945)原因:java.lang.NullPointerException:尝试调用虚拟方法 android.app.Instrumentation上com.example.norio.amosou.RealMenuActivity.onStop(RealMenuActivity.java:472)上的空对象引用上的'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' .callActivityOnStop(Instrumentation.java:1311)在android.app.Activity.performStop(Activity.java:6864)

android firebase logout
1个回答
0
投票

原因是您在onclick中执行了2 finish()

@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();
© www.soinside.com 2019 - 2024. All rights reserved.