package com.example.firebase_project;
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;
import com.google.firebase.auth.FirebaseUser;
public class LoginActivity extends AppCompatActivity {
private FirebaseUser currentuser;
private Button loginbutton,PhoneLoginButton;
private TextView forgetPasswordLink,NeedNewAccountLink;
private EditText UserEmail,UserPassword;
private FirebaseAuth mAuth;
private ProgressDialog loadingBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
InatlizeField();
currentuser=mAuth.getCurrentUser();
mAuth=FirebaseAuth.getInstance();
NeedNewAccountLink.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SendUserToRegisterActivity();
}
});
loginbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AllowUserToLogin();
}
});
}
private void AllowUserToLogin() {
String Email=UserEmail.getText().toString();
String Password=UserPassword.getText().toString();
if(TextUtils.isEmpty(Email))
{
Toast.makeText(this,"plz enter email",Toast.LENGTH_SHORT).show();
}
if(TextUtils.isEmpty(Password))
{
Toast.makeText(this,"plz enter password",Toast.LENGTH_SHORT).show();
}
else{
loadingBar.setTitle("Log in");
loadingBar.setMessage("plz wait");
loadingBar.setCanceledOnTouchOutside(true);
mAuth.signInWithEmailAndPassword(Email,Password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful())
{
sendtoMainActivity();
Toast.makeText(LoginActivity.this,"Login success ful",Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
}
else {
String massage = task.getException().toString();
Toast.makeText(LoginActivity.this, massage, Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
}
}
});
}
}
private void sendtoMainActivity() {
Intent MainIntent=new Intent(LoginActivity.this,MainActivity.class);
startActivity(MainIntent);
}
protected void InatlizeField()
{
loginbutton=(Button)findViewById(R.id.login_button);
PhoneLoginButton=(Button)findViewById(R.id.phone_login);
forgetPasswordLink=(TextView)findViewById(R.id.forget_password);
NeedNewAccountLink=(TextView)findViewById(R.id.create_account);
UserEmail=(EditText)findViewById(R.id.login_email);
UserPassword=(EditText)findViewById(R.id.login_password);
loadingBar=new ProgressDialog(this);
}
@Override
protected void onStart() {
super.onStart();
if(currentuser !=null)
{
SendUserToMainActivity();
}
}
protected void SendUserToMainActivity()
{
Intent MainActivityIntent=new Intent(LoginActivity.this,MainActivity.class);
startActivity(MainActivityIntent);
}
protected void SendUserToRegisterActivity()
{
Intent RegisterIntent=new Intent(LoginActivity.this,RegisterActivity.class);
startActivity(RegisterIntent);
}
}
MainActivity.java
package com.example.firebase_project;
import android.app.ProgressDialog;
import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
private ViewPager viewPager;
private TabLayout tabLayout;
private TabAcessAdaptor tabAcessAdaptor;
private FirebaseUser currentuser;
private FirebaseAuth mAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAuth = FirebaseAuth.getInstance();
currentuser=mAuth.getCurrentUser();
toolbar=(Toolbar)findViewById(R.id.main_page_toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Created By Sumit Kumar");
viewPager=(ViewPager)findViewById(R.id.main_tab_paper);
tabAcessAdaptor=new TabAcessAdaptor(getSupportFragmentManager());
viewPager.setAdapter(tabAcessAdaptor);
tabLayout=(TabLayout)findViewById(R.id.main_tabs);
tabLayout.setupWithViewPager(viewPager);
}
@Override
protected void onStart() {
super.onStart();
if(currentuser ==null)
{
SendUserToLoginPge();
}
}
private void SendUserToLoginPge()
{
Intent LoginIntent=new Intent(MainActivity.this,LoginActivity.class);
startActivity(LoginIntent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.option_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
super.onOptionsItemSelected(item);
if (item.getItemId()==R.id.main_find_friend_option)
{
}
if (item.getItemId()==R.id.main_setting_option)
{
}
if (item.getItemId()==R.id.main_logout_option)
{
mAuth.signOut();
sendUserToLoginPge();
}
return true;
}
private void sendUserToLoginPge() {
Intent intent =new Intent(MainActivity.this,LoginActivity.class);
startActivity(intent);
}
}
当我创建登录并使用Firebase身份验证进行注册
获取错误**原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'com.google.firebase.auth.FirebaseUser com.google.firebase.auth.FirebaseAuth.getCurrentUser()' com.example.firebase_project.LoginActivity.onCreate(LoginActivity.java:35)**
私有FirebaseUser currentuser; currentuser = mAuth.getCurrentUser();
我不知道它如何显示nullPointerexception
答案 0 :(得分:0)
您在MainActivity中分配了mAuth
,但没有在LoginActivity中分配。如果要在LoginActivity中使用它,则也必须在其中分配它。