com.google.firebase.auth.FirebaseAuth.getCurrentUser () 'для пустой ссылки на объект - PullRequest
0 голосов
/ 19 апреля 2019
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 authantification

получение ошибки ** Причина: 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

1 Ответ

0 голосов
/ 20 апреля 2019

Вы присвоили mAuth в MainActivity, но не в LoginActivity.Если вы хотите использовать его в LoginActivity, вам также придется назначить его там.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...