Invalid GmsCore APK, remote loading disabledrequires the Google Play Store but it is missing Firebase object access showing null pointer exception

Viewed 94

I am writing a sign-in page but the problem here is that every time I click on signin button it doesn't show my homepage but the words "please waiting" keep showing up. I am a newbie to java and firebase, I also searched a lot of websites but didn't find a solution. Does anyone, please help me?

Realtime database : enter image description here

here is code

package com.example.eatit_new;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;

import com.example.eatit_new.Common.Common;
import com.example.eatit_new.Model.User;
import com.google.android.material.snackbar.Snackbar;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.example.eatit_new.databinding.ActivitySignInBinding;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.rengwuxian.materialedittext.MaterialEditText;

public class SignIn extends AppCompatActivity {
    EditText editPhone, editPassword;
    Button btnSignIn;
@Override
    protected void onCreate (Bundle saveInstanceState) {
        super.onCreate(saveInstanceState);
        setContentView(R.layout.activity_sign_in);

        editPassword=(MaterialEditText)findViewById(R.id.editPassword);

        editPhone= (MaterialEditText)findViewById(R.id.editPhone);
        btnSignIn = (Button) findViewById(R.id.btnSignIn);

    //init Database
    final FirebaseDatabase database= FirebaseDatabase.getInstance();
    final DatabaseReference table_user=database.getReference("User");

btnSignIn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        final ProgressDialog mDialog = new ProgressDialog(SignIn.this);
        mDialog.setMessage("Please waiting....");
        mDialog.show();

        table_user.addValueEventListener( new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                //Check if user not exist in database

                if (dataSnapshot.child(editPhone.getText().toString()).exists()) {

                    //Get User Information
                    mDialog.dismiss();
                    User user = dataSnapshot.child(editPhone.getText().toString()).getValue(User.class);
                    if (user.getPassword().equals(editPassword.getText().toString())) {

                        Intent homeIntent= new Intent(SignIn.this,Home.class);
                        Common.currentUser= user;
                        startActivity(homeIntent);
                        finish();
                    } else {
                        Toast.makeText(SignIn.this, "Wrong password!", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    mDialog.dismiss();
                    Toast.makeText(SignIn.this, "User not exists!", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }
});
}
}

in User class :

package com.example.eatit_new.Model;

public class User {
    String name;
    String password;

    public User() {
    }

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public User(String name, String password) {
    this.name = name;
    this.password = password;
}

}

enter image description here

enter image description here

and I am getting "nullpointerException"

enter image description here

1 Answers

When you create a reference that points to the User node and when you attach a listener to it:

DatabaseReference table_user=database.getReference("User");

It means that you reading (downloading) all the data beneath that node. This is considered an antipattern, since downloading the entire node of users is definitely a waste of resources and bandwidth. If you want to check some data against a specific user, then you should consider adding the typed phone number to the reference. This means that you'll always read a single user, rather than all users. Assuming that the user types in the editPhone EditText one of the numbers that exist in your screenshot, the code should look like this:

String phone = editPhone.getText().toString().trim();
String password = editPassword.getText().toString();
DatabaseReference db = FirebaseDatabase.getInstance().getReference();
DatabaseReference phoneRef = db.child("User").child(phone);
phoneRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DataSnapshot> task) {
        if (task.isSuccessful()) {
            DataSnapshot snapshot = task.getResult();
            User user = snapshot.getValue(User.class);
            if(user.getPassword().equals(password)) {
                //Go to next activity
            } else {
                Toast.makeText(SignIn.this, "Wrong password!", Toast.LENGTH_SHORT).show();
            }
        } else {
            Log.d("TAG", task.getException().getMessage()); //Never ignore potential errors!
        }
    }
});
Related