Android应用程序崩溃,闪烁的屏幕

时间:2018-01-10 05:05:28

标签: android xml firebase

我是android开发的新手,我正在学习使用firebase进行身份验证,我创建了三个活动MainActivity,LoginActivity和FeedActivity。发生了什么事情我正在加载Android 7.1.1加载的小米redmi手机上的应用程序,它打开并看起来工作正常,但我尝试注册一个新帐户,而不是带我到FeedActivity,它使整个屏幕闪烁白色,一切都停止工作,唯一的方法是停止它是杀死应用程序并转到主屏幕。

MainActivity

package com.example.ahmed.myapplication;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button buttonRegister;
    private EditText editTextEmail;
    private EditText editTextPassword;
    private TextView textViewLogin;

    private ProgressDialog progressDialog;
    private FirebaseAuth firebaseAuth;

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

        firebaseAuth = FirebaseAuth.getInstance();
        if (firebaseAuth.getCurrentUser()!= null){
            finish();
            startActivity(new Intent(getApplicationContext(), FeedActivity.class));
        }
        progressDialog = new ProgressDialog(this);

        buttonRegister = (Button) findViewById(R.id.buttonRegister);

        editTextEmail = (EditText) findViewById(R.id.editTextEmail);
        editTextPassword = (EditText) findViewById(R.id.editTextPassword);

        textViewLogin = (TextView) findViewById(R.id.textViewLogin);

        buttonRegister.setOnClickListener(this);
        textViewLogin.setOnClickListener(this);
    }

    private void registerUser() {
        String email = editTextEmail.getText().toString().trim();
        String password = editTextPassword.getText().toString().trim();

        if (TextUtils.isEmpty(email)) {
            //email is empty
            Toast.makeText(this, "Opps! Please Enter Email", Toast.LENGTH_SHORT).show();
            //stop the function

            return;
        }

        if (TextUtils.isEmpty(password)) {
            //password is empty
            Toast.makeText(this, "Opps! Please Enter Password", Toast.LENGTH_SHORT).show();
            //stop the function

            return;
        }

        progressDialog.setMessage("Getting you in...");
        progressDialog.show();

        firebaseAuth.createUserWithEmailAndPassword(email,password)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {

                        if (task.isSuccessful()) {
                            firebaseAuth = FirebaseAuth.getInstance();
                                finish();
                                startActivity(new Intent(getApplicationContext(), FeedActivity.class));

                            Toast.makeText(MainActivity.this, "Welcome, to My App.", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(MainActivity.this, "Opps, Something went wrong! Try Again!!", Toast.LENGTH_SHORT).show();
                        }
                        progressDialog.dismiss();

                    }
                });
    }

    @Override
    public void onClick(View view) {
        if (view == buttonRegister) {
            registerUser();
        }

        if (view == textViewLogin) {
            startActivity(new Intent(this, LoginActivity.class));
        }

    }
}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="16dp"
        android:paddingTop="16dp"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        tools:context="com.example.ahmed.myapplication.MainActivity">


    <LinearLayout
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="Sign Up and Inspire!"
            android:textAppearance="@style/TextAppearance.AppCompat.Large" />



        <EditText
            android:layout_margin="15dp"
            android:inputType="textEmailAddress"
            android:hint="Enter Your Email"
            android:id="@+id/editTextEmail"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />


        <EditText
            android:layout_margin="15dp"
            android:inputType="textPassword"
            android:hint="Enter Your Password"
            android:id="@+id/editTextPassword"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <Button
            android:layout_margin="15dp"
            android:id="@+id/buttonRegister"
            android:text="Sign Up"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:textAlignment="center"
            android:text="Already On signed up? Sign In Here"
            android:id="@+id/textViewLogin"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />


    </LinearLayout>

    </RelativeLayout>

LoginActivity

package com.example.ahmed.myapplication;

import android.app.ProgressDialog;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private Button buttonLogin;
    private EditText editTextEmail;
    private EditText editTextPassword;
    private TextView textViewSignup;

    private FirebaseAuth firebaseAuth;
    private ProgressDialog progressDialog;

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


        firebaseAuth = FirebaseAuth.getInstance();
        if (firebaseAuth.getCurrentUser()!= null){
            finish();
            startActivity(new Intent(getApplicationContext(), FeedActivity.class));
        }
        editTextEmail = (EditText) findViewById(R.id.editTextEmail);
        editTextPassword = (EditText) findViewById(R.id.editTextPassword);
        buttonLogin = (Button) findViewById(R.id.buttonLogin);
        textViewSignup = (TextView) findViewById(R.id.textViewSignup);

        progressDialog = new ProgressDialog(this);


        buttonLogin.setOnClickListener(this);
        textViewSignup.setOnClickListener(this);

    }

    private void userLogin() {
        String email = editTextEmail.getText().toString().trim();
        String password = editTextPassword.getText().toString().trim();

        if (TextUtils.isEmpty(email)) {
            //email is empty
            Toast.makeText(this, "Opps! Please Enter Email", Toast.LENGTH_SHORT).show();
            //stop the function

            return;
        }

        if (TextUtils.isEmpty(password)) {
            //password is empty
            Toast.makeText(this, "Opps! Please Enter Password", Toast.LENGTH_SHORT).show();
            //stop the function

            return;
        }

        progressDialog.setMessage("Finding Your Account...");
        progressDialog.show();

        firebaseAuth.signInWithEmailAndPassword(email,password)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        progressDialog.dismiss();
                        if (task.isSuccessful()){
                            finish();
                            startActivity(new Intent(getApplicationContext(), FeedActivity.class));
                        }

                    }
                });
    }

    @Override
    public void onClick(View view) {

        if (view == buttonLogin){
            userLogin();
        }

        if (view == textViewSignup){
            finish();
            startActivity(new Intent(this, MainActivity.class));
        }
    }
}

activity_login.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.ahmed.myapplication.LoginActivity">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:orientation="vertical"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="79dp">


        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="Welcome Back Buddy!"
            android:textAppearance="@style/TextAppearance.AppCompat.Large" />


        <EditText
            android:id="@+id/editTextEmail"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="15dp"
            android:hint="Enter Your Email"
            android:inputType="textEmailAddress" />


        <EditText
            android:id="@+id/editTextPassword"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="15dp"
            android:hint="Enter Your Password"
            android:inputType="textPassword" />

        <Button
            android:id="@+id/buttonLogin"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="15dp"
            android:text="Log In" />

        <TextView
            android:id="@+id/textViewSignup"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="New To My App? Sign Up Now"
            android:textAlignment="center" />


    </LinearLayout>

</android.support.constraint.ConstraintLayout>

FeedActivity

package com.example.ahmed.myapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

public class FeedActivity extends AppCompatActivity implements View.OnClickListener {

    private FirebaseAuth firebaseAuth;
    private TextView textViewUserEmail;
    private Button buttonLogout;

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


        firebaseAuth = FirebaseAuth.getInstance();
        if (firebaseAuth.getCurrentUser()!= null){
            finish();
            startActivity(new Intent(getApplicationContext(), LoginActivity.class));
        }

        FirebaseUser user = firebaseAuth.getCurrentUser();

        textViewUserEmail = (TextView) findViewById(R.id.textViewUserEmail);
        textViewUserEmail.setText("Welcome friend"+user.getEmail());
        buttonLogout = (Button) findViewById(R.id.buttonLogout);

        buttonLogout.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {

        if (view == buttonLogout){
            firebaseAuth.signOut();
            finish();
            startActivity(new Intent(this, LoginActivity.class));
        }
    }
}

感谢您的帮助

activity_feed.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.ahmed.myapplication.FeedActivity">

    <LinearLayout
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <TextView
            android:id="@+id/textViewUserEmail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="Ander Agaya te!"
            android:textAppearance="@style/TextAppearance.AppCompat.Large" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Sign Out"
            android:id="@+id/buttonLogout"
            android:layout_gravity="center_horizontal" />
    </LinearLayout>
</android.support.constraint.ConstraintLayout>

2 个答案:

答案 0 :(得分:0)

改变这一点 - :

if (task.isSuccessful()) {
                            firebaseAuth = FirebaseAuth.getInstance();
                                finish();
                                startActivity(new Intent(getApplicationContext(), FeedActivity.class));

                            Toast.makeText(MainActivity.this, "Welcome, to My App.", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(MainActivity.this, "Opps, Something went wrong! Try Again!!", Toast.LENGTH_SHORT).show();
                        }

要 - :

if (task.isSuccessful()) {
                            firebaseAuth = FirebaseAuth.getInstance();

                                startActivity(new Intent(getApplicationContext(), FeedActivity.class));

finish();

                            Toast.makeText(MainActivity.this, "Welcome, to My App.", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(MainActivity.this, "Opps, Something went wrong! Try Again!!", Toast.LENGTH_SHORT).show();
                        }

在该行之后致电完成...

答案 1 :(得分:0)

在FeedActivity.class中添加if条件

 if (firebaseAuth.getCurrentUser()!= null){
            finish();
            startActivity(new Intent(getApplicationContext(), LoginActivity.class));
        }

 if (firebaseAuth.getCurrentUser()= null){

            startActivity(new Intent(getApplicationContext(), LoginActivity.class));
 finish();
            }