首页 > 解决方案 > 应用程序继续停止再次打开在 LoginActivity.java 上发生错误

问题描述

我构建了我的应用程序并显示成功构建但是当我尝试在我的设备中运行时,它显示您的应用程序再次停止打开,如果有人帮助我解决这个问题,这只会在登录活动中发生,所以请帮助我

我的登录活动代码

package com.eassycars.www.licencespot;

import android.content.Intent;
import android.os.Build;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
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.ProgressBar;
import android.widget.RelativeLayout;
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 Activity_Login extends AppCompatActivity {


    private EditText inputEmail, inputPassword;
    private FirebaseAuth mAuth;
    private ProgressBar progressBar;
    private Button singups,forgotpass,logins;


    RelativeLayout really1, really2;

    Handler handler = new Handler();
    Runnable runnable = new Runnable() {

        @Override
        public void run() {
            really1.setVisibility(View.VISIBLE);
            really2.setVisibility(View.VISIBLE);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Get Firebase auth instance
        mAuth = FirebaseAuth.getInstance();

        if (mAuth.getCurrentUser() != null) {
            startActivity(new Intent(Activity_Login.this, MainActivity.class));
            finish();
        }

        // set the view now
        setContentView(R.layout.activity__login);


        inputEmail = (EditText) findViewById(R.id.email);
        inputPassword = (EditText) findViewById(R.id.password);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        singups = (Button) findViewById(R.id.singups);
        logins = (Button) findViewById(R.id.logins);
        forgotpass = (Button) findViewById(R.id.forgotpass);

        //Get Firebase auth instance
        mAuth = FirebaseAuth.getInstance();

        singups.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(Activity_Login.this, singup.class));
            }
        });

        forgotpass.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(Activity_Login.this, ForgotPassword.class));
            }
        });
        really1 = (RelativeLayout) findViewById(R.id.rellayl);
        really2 = (RelativeLayout) findViewById(R.id.really2);
        handler.postDelayed(runnable, 2000);

        logins.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String email = inputEmail.getText().toString();
                final String password = inputPassword.getText().toString();

                if (TextUtils.isEmpty(email)) {
                    Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (TextUtils.isEmpty(password)) {
                    Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show();
                    return;
                }

                progressBar.setVisibility(View.VISIBLE);

                //authenticate user
                mAuth.signInWithEmailAndPassword(email, password)
                        .addOnCompleteListener(Activity_Login.this, new OnCompleteListener<AuthResult>() {
                            @Override
                            public void onComplete(@NonNull Task<AuthResult> task) {
                                // If sign in fails, display a message to the user. If sign in succeeds
                                // the auth state listener will be notified and logic to handle the
                                // signed in user can be handled in the listener.
                                progressBar.setVisibility(View.GONE);
                                if (!task.isSuccessful()) {
                                    // there was an error
                                    if (password.length() < 6) {
                                        inputPassword.setError(getString(R.string.minimum_password));
                                    } else {
                                        Toast.makeText(Activity_Login.this, getString(R.string.auth_failed), Toast.LENGTH_LONG).show();
                                    }
                                } else {
                                    Intent intent = new Intent(Activity_Login.this, MainActivity.class);
                                    startActivity(intent);
                                    finish();

                                }
                            }
                        });
            }
        });
    }
}

这些是我的登录活动代码,无法找到错误,android studio 也显示您没有任何错误,但有一个错误是我的应用程序在我使用登录屏幕时不断停止。

我使用 Firebase 构建登录活动

标签: javaandroidandroid-studio

解决方案


请从 logcat 发布您的错误消息。

如果从 Thread 调用 Handler 导致 NullPointerException,

试着打电话给handleronCreate

public class LoginActivity extends Activity {

@Override
public void onCreate(Bundle bundle){
    super.onCreate(bundle);
    //move this HERE!!  
    new Handler().post(new Runnable() {
    @Override
      public void run() {
       // Code here will run in UI thread
      }
    });
  }
}

推荐阅读