I am developing a user login application in Android Studio, and I am not able to figure out why my on complete function is not working. It is not adding users to Firebase authentication. Anyone know it?
public void signuphere(View view) {
String email = t3.getEditText().getText().toString();
String password = t4.getEditText().getText().toString();
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(Register.this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "createUserWithEmail:success");
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "createUserWithEmail:failure", task.getException());
Toast.makeText(Register.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
}
});
Related
This question already has answers here:
Unfortunately MyApp has stopped. How can I solve this?
(23 answers)
Closed 11 months ago.
I'm begginer in coding and I don't know how to solve this bug.
I'm creating an app and in the login class the app stop working when fields are wmpty and pressing login button. Please help me with this
This is the code:
binding.submitBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String email, pass;
email = binding.emailBox.getText().toString();
pass = binding.passwordBox.getText().toString();
dialog.show();
auth.signInWithEmailAndPassword(email, pass).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
dialog.dismiss();
if(task.isSuccessful()){
startActivity(new Intent(LoginActivity.this, MainActivity.class));
finish();
} else {
Toast.makeText(LoginActivity.this, task.getException().getLocalizedMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}
});
You should make operation to check if field empty or not
if (binding.emailBox.getText().toString().isEmpty() || binding.passwordBox.getText().toString().isEmpty()) {
// Do something, erro dialog or whatever
} else {
login();
}
Try to check logcat and read the message
BtnReg = findViewById(R.id.btnRegister);
Button btnLogin = findViewById(R.id.btnLogin);
etUsername =findViewById(R.id.etUsername);
etPass = findViewById(R.id.etPass);
dataBase = Room.databaseBuilder(this, AppDataBase.class, "user-database.db")
.allowMainThreadQueries()
.build();
UserDao db = dataBase.userDao();
btnLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String username = etUsername.getText().toString();
String password = etPass.getText().toString();
User user= userDao.getUser(username,password);
Attempt to invoke interface method 'com.example.worksheet3.Data.User
com.example.worksheet3.Data.UserDao.getUser(java.lang.String,
java.lang.String)' on a null object reference
if ( user !=null) {
Intent i = new Intent(MainActivity.this, HomeActivity.class);
startActivity(i);
}else{
Toast.makeText(MainActivity.this, "Unregistered user, or incorrect", Toast.LENGTH_SHORT).show();
}
}
});
You need to have a method() to search for the user(username and password) in your database helper. Then you can come back and call your database helper method in your if statement to login. If you don't know how to code the needed method please Shoutš£
I want to use Facebook Audience Network for my Android app, I have integrated interstitialAd in-app and it works fine but it appears as Activity launch. I want to show ad after 30 seconds. this is my code
interstitialAd = new InterstitialAd(this, "1555910157949688_1556058954601475");
interstitialAd.setAdListener(new InterstitialAdListener() {
#Override
public void onAdLoaded(Ad ad) {
// Interstitial ad is loaded and ready to be displayed
Log.d(TAG, "Interstitial ad is loaded and ready to be displayed!");
// Show the ad
interstitialAd.show();
}
});
interstitialAd.loadAd();
and I have tried this to schedule my ad but it did not work.
private void showAdWithDelay() {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
// Check if interstitialAd has been loaded successfully
if(interstitialAd == null || !interstitialAd.isAdLoaded()) {
return;
}
// Check if the ad is already expired or invalidated, and do not show ad if that is the case. You will not get paid to show an invalidated ad.
if(interstitialAd.isAdInvalidated()) {
return;
}
// Show the ad
interstitialAd.show();
}
}, 3000);
}
I prefer to do this:
Handler handler = new Handler();
interstitialAd = new InterstitialAd(this, "XXX");
interstitialAd.setAdListener(new InterstitialAdListener() {
#Override
public void onAdLoaded(Ad ad) {
handler.postDelayed(new Runnable() {
#Override
public void run() {
interstitialAd.show();
}
}, YOUR_TIME);
}
});
interstitialAd.loadAd();
And if you want to be more precise, do it:
final long lastTime = System.currentTimeMillis();
Handler handler = new Handler();
interstitialAd = new InterstitialAd(this, "XXX");
interstitialAd.setAdListener(new InterstitialAdListener() {
#Override
public void onAdLoaded(Ad ad) {
long now = System.currentTimeMillis();
long timeWait;
if (now - lastTime >= YOUR_TIME){
timeWait = 0;
}else {
timeWait = YOUR_TIME-(now-lastTime);
}
handler.postDelayed(new Runnable() {
#Override
public void run() {
interstitialAd.show();
}
}, timeWait);
}
});
interstitialAd.loadAd();
I think your implementation is breaking AdMob policies and could get you some trouble.
Check this page for help on right/wrong Interstitial implementations (Specially the "Interstitials that unexpectedly launch" point): https://support.google.com/admob/answer/6201362?hl=en&ref_topic=2936214
To comply with AdMob policies I suggest you only show Interstitials in activity/app natural transitions, different implementations could lead you to get banned from Admob.
To do so, you could change your code to first just load the Ad and keep track of its loaded status like this:
boolean isAdLoaded = false;
interstitialAd = new InterstitialAd(this, "1555910157949688_1556058954601475");
interstitialAd.setAdListener(new InterstitialAdListener()
{
#Override
public void onAdLoaded(Ad ad)
{
// Interstitial ad is loaded and ready to be displayed
Log.d(TAG, "Interstitial ad is loaded and ready to be displayed!");
// The ad has been loaded
isAdLoaded = true;
}
});
interstitialAd.loadAd();
And then show it in a natural transition of the app, for example when the user taps a button to go to a different activity:
shareButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
//Show interstitial if it is loaded
if(isAdLoaded)
interstitialAd.show();
//Open Activity
startActivity(new Intent(this,OtherActivity.class));
}
});
I am trying to make a login logic using post request with retrofit, here is my code
Button btnLogin = (Button) findViewById(R.id.btn_login);
btnLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showProgress(true,"Sedang login... ");
String username = inputUsername.getText().toString();
String password = inputPassword.getText().toString();
apiService.login(new LoginParam(username,password)).enqueue(new Callback<AuthResponse>() {
#Override
public void onResponse(Call<AuthResponse> call, Response<AuthResponse> response) {
if(response.isSuccessful()){
showProgress(false,null);
Editor ed = sp.edit();
ed.putString("token",response.body().getToken());
Boolean login = response.body().getLogin();
Log.d("Login",response.body().getLogin().toString());
ed.putBoolean("login",response.body().getLogin().booleanValue());
ed.putString("username",response.body().getUser().getUsername());
ed.commit();
//startActivity(new Intent(getBaseContext(), MainActivity.class));
if(!login){
TextView infologin = (TextView) findViewById(R.id.loginInfo);
infologin.setText("Username dan password salah, coba lagi");
infologin.setVisibility(View.VISIBLE);
return;
}
else{
Intent i = new Intent(getBaseContext(),MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_CLEAR_TASK);
i.putExtra("route","afterlogin");
startActivity(i);
finish();
}
}
}
#Override
public void onFailure(Call<AuthResponse> call, Throwable t) {
LoginActivity.this.showProgress(false, null);
Toast.makeText(LoginActivity.this, "Gagal koneksi ke server, periksa jaringan internet anda error: " + t.toString(), Toast.LENGTH_LONG).show();
}
});
}
});
it was working when username and password are correct, but when it was not, it only shows a loading animation, the block that handle if login is false remains inexecuted. what's going wrong?
I am trying to create an admin only login on android studio using storm path. I am able to login any account right now, but I want to limit the people who can login based on who is in the Admin group in strompath.
StormpathCallback<Void> loginCallback = new StormpathCallback<Void>() {
#Override
public void onSuccess(Void aVoid) {
Stormpath.getAccessToken();
finish();
startActivity(new Intent(LoginActivity.this, (Class) redirectActivity));
}
#Override
public void onFailure(StormpathError error) {
showAlert();
// finish();
//startActivity(new Intent(LoginActivity.this, (Class) redirectActivity));
}
};