Firebase身份验证后启动MainActivity

时间:2017-12-27 09:42:48

标签: java android firebase login firebase-authentication

我已经研究过如何在登录后启动新活动,但每当我尝试实施新活动时,我的应用程序都会崩溃。

我的启动器类登录活动提示通过用户名-pw / Facebook / Google进行身份验证。

现在我正在尝试在onActivityResult方法中启动新活动,但一直在崩溃。尝试了不同的方式来启动它,但现在我迷失了。

我设法在点击按钮后开始一个新片段,或者开始一个新活动,但是登录给我带来了麻烦。

感谢您的帮助 - 非常感谢!

public class LoginActivity extends AppCompatActivity implements View.OnClickListener{
    private static final int RC_SIGN_IN = 0;
    private FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();

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

        //everything for firebase
        firebaseAuth = FirebaseAuth.getInstance();
        if (firebaseAuth.getCurrentUser() != null) {
            //user already signed in
            Log.d("AUTH", firebaseAuth.getCurrentUser().getEmail());

        } else {
            startActivityForResult(AuthUI.getInstance()
                            .createSignInIntentBuilder()
                            .setAvailableProviders(
                                    Arrays.asList(new AuthUI.IdpConfig.Builder(AuthUI.EMAIL_PROVIDER).build(),
                                            new AuthUI.IdpConfig.Builder(AuthUI.GOOGLE_PROVIDER).build(),
                                            new AuthUI.IdpConfig.Builder(AuthUI.FACEBOOK_PROVIDER).build()))
                            .build(),
                    RC_SIGN_IN);
        }
        findViewById(R.id.log_out_button).setOnClickListener(this);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RC_SIGN_IN) {
            if (resultCode == RESULT_OK) {
                //user logged in
                Log.d("AUTH", firebaseAuth.getCurrentUser().getEmail());

                Uri contentUri = data.getData();
                startActivity(new Intent(this, MainActivity.class)
                        .setData(contentUri));

               // Intent myIntent = new Intent(LoginActivity.this, MainActivity.class);
                // LoginActivity.this.startActivity(myIntent);

                /* TEST: START RANDOM FRAGMENT FROM THE ACTIVITY
                android.support.v4.app.FragmentManager fragmentManager = LoginActivity.this.getSupportFragmentManager();
                android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                TrainingFragment trainingFragment = new TrainingFragment();
                fragmentTransaction.add(R.id.fragment_container, trainingFragment);
                fragmentTransaction.commit();*/

            }
            else {
                //user not authenticated
                Log.d("AUTH", "NOT AUTHENTICATED");
            }
        }
    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.log_out_button) {
            AuthUI.getInstance()
                    .signOut(this)
                    .addOnCompleteListener(new OnCompleteListener<Void>() {
                        @Override
                        public void onComplete(@NonNull Task<Void> task) {
                                Log.d("AUTH", "USER LOGGED OUT!");
                            //Fragment newFragment = new HomeFragment(); try to start new activity?
                            //switch to another fragment saying bye or some back next time or sth and then add a button with a "close app" function
                           /* android.support.v4.app.FragmentManager fragmentManager = LoginActivity.this.getSupportFragmentManager();
                            android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                            TrainingFragment trainingFragment = new TrainingFragment();
                            fragmentTransaction.add(R.id.fragment_container, trainingFragment);
                            fragmentTransaction.commit(); */
                           finish(); //finish ends not only the activity, but the application, without it, the switch from activity to fragment functions :)


                        }
                    });
        }
    }
}

错误日志

D / AndroidRuntime:关闭VM E / AndroidRuntime:致命异常:主要                   过程:com.example.vreeni.firebaseauthentiction,PID:28367                   java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.vreeni.firebaseauthentiction / com.example.vreeni.firebaseauthentication.MainActivity}:java.lang.IllegalStateException:此活动已有窗口装饰提供的操作栏。不要在主题中请求Window.FEATURE_SUPPORT_ACTION_BAR并将windowActionBar设置为false以使用工具栏。                       在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2822)                       在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2897)                       在android.app.ActivityThread.-wrap11(未知来源:0)                       在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1598)                       在android.os.Handler.dispatchMessage(Handler.java:105)                       在android.os.Looper.loop(Looper.java:251)                       在android.app.ActivityThread.main(ActivityThread.java:6563)                       at java.lang.reflect.Method.invoke(Native Method)                       在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)                       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)                    引起:java.lang.IllegalStateException:此Activity已经有一个由窗口装饰提供的操作栏。不要在主题中请求Window.FEATURE_SUPPORT_ACTION_BAR并将windowActionBar设置为false以使用工具栏。                       在android.support.v7.app.AppCompatDelegateImplV9.setSupportActionBar(AppCompatDelegateImplV9.java:201)                       在android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:129)                       在com.example.vreeni.firebaseauthentication.MainActivity.onCreate(MainActivity.java:26)                       在android.app.Activity.performCreate(Activity.java:6975)                       在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)                       在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2775)                       在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2897)                       在android.app.ActivityThread.-wrap11(未知来源:0)                       在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1598)                       在android.os.Handler.dispatchMessage(Handler.java:105)                       在android.os.Looper.loop(Looper.java:251)                       在android.app.ActivityThread.main(ActivityThread.java:6563)                       at java.lang.reflect.Method.invoke(Native Method)                       在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)                       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

1 个答案:

答案 0 :(得分:0)

好吧,显然它已经混淆了,因为我最初有2个单独的项目,我合并在一起。 第一个活动和以下活动的样式文件中的主题包含有关actionBar的不同信息。 另外,我还没有在Manifest中初始化第二个活动。 只是一直盯着代码太长时间没注意到这一点。 问题解决了:))