App crashes when i try to open a certain activity - android

Hi All I'm really new to Android Studio. I'm trying to build an app but it keeps crashing when i try to open a certain Activity the other activities open ok just the forth one is the problem.
public class ForthActivity extends AppCompatActivity {
private EditText email;
private Button Save;
DatabaseReference databaseReference;
#Override
protected void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_forth);
email = (EditText) findViewById(R.id.email);
Save =(Button) findViewById(R.id.btnSave);
databaseReference = FirebaseDatabase.getInstance().getReference().child("Users");
Save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AddData();
}
});
}
public void AddData(){
String Email = email.getText().toString().trim();
SaveData saveData = new SaveData(Email);
databaseReference.push().setValue(saveData);
}
}

Related

Why aren't users registering to my Realtime Firebase database?

I have been following a tutorial to create an app like Tinder for my project and I am learning databasing. I have set up the code as the tutorial said within the version of Android Studio I have, yet when I register users, they don't appear in the database despite the profiles passing authentication. I have set the rules to true and it still hasn't solved my problem. The only thing I can possibly think of is that my database is set to Belgium West Europe over the USA as all tutorials for Firebase show USA as the database location. If anyone has any suggestions please drop them in, thank you.
Ps: Here is the tutorial link https://www.youtube.com/watch?v=_thTn8SDjC8&list=PLxabZQCAe5fio9dm1Vd0peIY6HLfo5MCf&index=5
public class RegistrationActivity extends AppCompatActivity {
private Button mRegister;
private EditText mEmail, mPassword, mName;
private RadioGroup mRadioGroup;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener firebaseAuthStateListener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration);
mAuth = FirebaseAuth.getInstance();
firebaseAuthStateListener = new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user !=null) {
Intent intent = new Intent(RegistrationActivity.this, MainActivity.class);
startActivity(intent);
finish();
return;
}
}
};
mRegister = (Button) findViewById(R.id.register);
mEmail = (EditText) findViewById(R.id.email);
mPassword = (EditText) findViewById(R.id.password);
mName = (EditText) findViewById(R.id.name);
mRadioGroup = (RadioGroup) findViewById(R.id.radioGroup);
mRegister.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int selectId = mRadioGroup.getCheckedRadioButtonId();
final RadioButton radioButton = (RadioButton) findViewById(selectId);
if(radioButton.getText() == null){
return;
}
final String email = mEmail.getText().toString();
final String password = mPassword.getText().toString();
final String name = mName.getText().toString();
mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(RegistrationActivity.this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (!task.isSuccessful()){
Toast.makeText(RegistrationActivity.this, "Error Signing In", Toast.LENGTH_SHORT).show();
}else{
String userId = mAuth.getCurrentUser().getUid();
DatabaseReference currentUserDb = FirebaseDatabase.getInstance().getReference().child(radioButton.getText().toString()).child(userId).child("name");
currentUserDb.setValue(name);
}
}
});
}
});
}
#Override
protected void onStart() {
super.onStart();
mAuth.addAuthStateListener(firebaseAuthStateListener);
}
#Override
protected void onStop() {
super.onStop();
mAuth.removeAuthStateListener(firebaseAuthStateListener);
}
}
I found out through experimentation that the database MUST be set to USA and not Belgium. It's still unclear as to exactly why this is but starting a new database with the USA location seemed to have worked with registering users to the database

Print a toast if specific parameter is correct

I'm trying to print a toast msg if the username and password is equal to admin i try this code but the toast msg says it's error and the logcat doesn't show anything
code i tried
public class login extends AppCompatActivity {
DatabaseHelper db;
EditText username,password;
Button login;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
db = new DatabaseHelper(this);
username = findViewById(R.id.username);
password = findViewById(R.id.password);
login = findViewById(R.id.login);
adminLogin();
}
public void toast(Context context, String string){
Toast.makeText(this,string, Toast.LENGTH_SHORT).show();
}
public void adminLogin(){
login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(username.getText().toString() == "admin" || password.getText().toString() == "admin"){
toast(login.this,"Done");
}
else{
toast(login.this,"Error");
}
}
});
}
}
You should not use == to compare String in java, rather you should use .equals method on String. So your logic should be as follow:
if(username.getText().toString().equals("admin") || password.getText().toString().equals("admin")){
toast(login.this,"Done");
}

android with firebase RealTime Database

how to fix this issue "savedInstance null" when using firebase realtimeDataBase
public class MainActivity extends AppCompatActivity {
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn =(Button)findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FirebaseDatabase database = FirebaseDatabase.getInstance( );
DatabaseReference myRef = database.getReference();
myRef.child("test").push().setValue("Nikmal");
Toast.makeText(getApplicationContext(),"succesfully", Toast.LENGTH_SHORT).show();
}
});
}
}
Try this code, it should works if you use a DatabaseReference
Button btn;
DatabaseReference myRef;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myRef = FirebaseDatabase.getInstance().getReference();
btn =(Button)findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myRef.child("test").push().setValue("Nikmal");
Toast.makeText(getApplicationContext(),"succesfully", Toast.LENGTH_SHORT).show();
}
});
}

How to retrieve POJO in Firebase Android

public class MainActivity extends AppCompatActivity {
private Firebase mRef;
private EditText mName,mEmail,mAge;
private Button mAddStud;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mName = (EditText) findViewById(R.id.editTextName);
mEmail = (EditText) findViewById(R.id.editTextEmail);
mAge = (EditText) findViewById(R.id.editTextAge);
mAddStud = (Button) findViewById(R.id.addStudents);
mRef = new Firebase(Constants.FIREBASE_URL);
mAddStud.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final Student student = new Student(mName.getText().toString(),mEmail.getText().toString(), Integer.parseInt(mAge.getText().toString()));
mRef.child("newStudent").setValue(student);
}
});
}
}
Display
public class Display extends AppCompatActivity {
private Firebase mRef;
private TextView mDisplay;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
mDisplay = (TextView) findViewById(R.id.textViewDisplay);
mRef = new Firebase("https://firecrestapp.firebaseio.com/newStudent");
mRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Student value = dataSnapshot.getValue(Student.class);
mDisplay.setText(value.getName()+" "+value.getEmailId()+" "+value.getAge() );
}
#Override
public void onCancelled(FirebaseError firebaseError) {
}
});
}
}
Im getting this error
com.firebase.client.FirebaseException: Failed to bounce to type
How am I supposed to retrieve the data?

Button is clickable but doesn't work [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
So I have this code but when I'm trying it on my phone doesn't do anything..
I tried the version on firebase website too but it doesn't work. I would really appreciate if you can help me guys!
I think that this is the problem
Can someone help me with it?
public class LoginActivity extends AppCompatActivity {
public Firebase mFirebase;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Firebase.setAndroidContext(this);
mFirebase = new Firebase("https://kip.firebaseio.com");
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_login);
}
public void onClickLogin(View v){
final String email = ((EditText) findViewById(R.id.email)).getText().toString();
final String password =((EditText) findViewById(R.id.password)).getText().toString();
mFirebase.createUser(email, password, new Firebase.ResultHandler() {
#Override
public void onSuccess() {
mFirebase.authWithPassword(email, password, null);
System.out.println("Successfully created user account!");
}
#Override
public void onError(FirebaseError firebaseError) {
mFirebase.authWithPassword(email, password, null);
System.out.println("There was an error connecting!");
}
});
}
}
Here is my XML
<Button
android:layout_width="300dp"
android:layout_height="wrap_content"
android:background="#drawable/button_login"
android:text="#string/login"
android:textColor="#android:color/white"
android:textSize="16sp"
android:id="#+id/button"
android:layout_below="#+id/password"
android:layout_centerHorizontal="true"
android:layout_marginTop="75dp"
android:onClick="onClickLogin"
android:clickable="true" />
Try with:
public class LoginActivity extends AppCompatActivity {
public Firebase mFirebase;
Button button = (Button) findViewById(R.id.button);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Firebase.setAndroidContext(this);
mFirebase = new Firebase("https://kip.firebaseio.com");
requestWindowFeature(Window.FEATURE_NO_TITLE);
button.setOnClickListener( new OnClickListener() {
#Override
public void onClick(View v) {
final String email = ((EditText) findViewById(R.id.email)).getText().toString();
final String password =((EditText) findViewById(R.id.password)).getText().toString();
mFirebase.createUser(email, password, new Firebase.ResultHandler() {
#Override
public void onSuccess() {
mFirebase.authWithPassword(email, password, null);
System.out.println("Successfully created user account!");
}
#Override
public void onError(FirebaseError firebaseError) {
mFirebase.authWithPassword(email, password, null);
System.out.println("There was an error connecting!");
}
});
}
});
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_login);
}
}
This is the final code and it works!!
protected EditText mEmail;
protected EditText mPassword;
protected Button mLogin;
protected Button mRegister;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Firebase.setAndroidContext(this);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_login);
mEmail = (EditText) findViewById(R.id.email);
mPassword = (EditText) findViewById(R.id.password);
mLogin = (Button) findViewById(R.id.Login);
mRegister = (Button) findViewById(R.id.Register);
mLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Firebase ref = new Firebase("https://kip.firebaseio.com");
String email = mEmail.getText().toString().trim();
String password = mPassword.getText().toString().trim();
ref.authWithPassword(email, password, new Firebase.AuthResultHandler() {
#Override
public void onAuthenticated(AuthData authData) {
System.out.println("User ID: " + authData.getUid() + ", Provider: " + authData.getProvider());
Intent activity = new Intent(LoginActivity.this, HomeActivity.class);
startActivity(activity);
}
#Override
public void onAuthenticationError(FirebaseError firebaseError) {
// there was an error
}
});
}
});
mRegister.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Firebase ref = new Firebase("https://kip.firebaseio.com");
String email = mEmail.getText().toString().trim();
String password = mPassword.getText().toString().trim();
ref.createUser(email, password, new Firebase.ValueResultHandler<Map<String, Object>>() {
#Override
public void onSuccess(Map<String, Object> result) {
System.out.println("Successfully created user account with uid: " + result.get("uid"));
}
#Override
public void onError(FirebaseError firebaseError) {
// there was an error
}
});
}
});
}
}

Categories

Resources