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();
}
});
}
Related
I am still learning to use Android Studio, I have difficulty to set the status of the togglebutton in accordance with the realtimedatabase in Firebase, when exiting the application and entering the button again is off when the database is on,
so what I want to ask, when the database is activated, then togglebutton on Android is active and vice versa,
Thank you.
public class MainActivity extends AppCompatActivity {
TextView teks;
DatabaseReference dref;
String status;
ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
teks=(TextView)findViewById(R.id.text1);
Typeface customfont=Typeface.createFromAsset(getAssets(),"font/jd_led3.ttf");
teks.setTypeface(customfont);
teks=(TextView)findViewById(R.id.text1);
dref = FirebaseDatabase.getInstance().getReference();
dref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
status=dataSnapshot.child("Suhu").getValue().toString();
teks.setText(status);
}
#Override
public void onCancelled(DatabaseError error) {
}
});
final ToggleButton button = (ToggleButton) findViewById(R.id.toggleButton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FirebaseDatabase database = FirebaseDatabase.getInstance();
button.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP);
DatabaseReference myRef = database.getReference("RELAY1_STATUS");
if(button.isChecked()){
myRef.setValue("ON");
}else{
myRef.setValue("OFF");
}
}
});
final ToggleButton button1 = (ToggleButton) findViewById(R.id.toggleButton1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FirebaseDatabase database = FirebaseDatabase.getInstance();
button1.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP);
DatabaseReference myRef = database.getReference("RELAY2_STATUS");
if(button1.isChecked()){
myRef.setValue("ON");
}else{
myRef.setValue("OFF");
}
}
});
final ToggleButton button2 = (ToggleButton) findViewById(R.id.toggleButton2);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FirebaseDatabase database = FirebaseDatabase.getInstance();
button2.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP);
DatabaseReference myRef = database.getReference("RELAY3_STATUS");
if(button2.isChecked()){
myRef.setValue("ON");
}else{
myRef.setValue("OFF");
}
}
});
}
}
sorry if my post is a mess I am also the first time using stackoverflow
ON ONCREATE You will need need to add value event listener like
MyRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
status=dataSnapshot.child("RELAY2_STATUS").getValue().toString();
if(status.equals("ON"){
button1.isChecked(true)
}
#Override
public void onCancelled(DatabaseError error) {
}
});
dref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d(TAG, "onDataChange: " + status);
status = dataSnapshot.child("RELAY1_STATUS").getValue().toString();
if (status.equals("ON")) {
a.setChecked(true);
} else {
a.setChecked(false);
}
}
#Override
public void onCancelled(DatabaseError error) {
Log.w(TAG, "onCancelled: ",error.toException() );
}
});
public class MainActivity extends AppCompatActivity {
//points to the root node of the database
DatabaseReference mRootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference mConditionRef = mRootRef.child("condition");
TextView content;
Button sunny, foggy;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
content = (TextView) findViewById(R.id.condition);
sunny = (Button) findViewById(R.id.sunny);
foggy = (Button) findViewById(R.id.foggy);
}
#Override
protected void onStart() {
super.onStart();
mConditionRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String text = dataSnapshot.getValue(String.class);
content.setText(text);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
sunny.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mConditionRef.setValue("Sunny");
mRootRef.child("condition").push().setValue("Abhijeet Jain2");
}
});
foggy.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mConditionRef.setValue("Foggy");
}
});
}
}
The string gets added in the realtime database with a automatic made id but the app crashes as soon as I press the button "sunny".
What could be the reason and the solution?
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);
}
}
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?
I am trying to play around with Firebase, I have this piece of code but the user is not getting created.
I've enabled email and password auth on Firebase. The Firebase url is correct.
public class RegisterActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
Firebase.setAndroidContext(this);
Button BCreate;
BCreate=(Button)findViewById(R.id.Createbutton);
BCreate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.e("URL", "Before firebase url");
go();
}
});
}
private void go() {
final Firebase ref=new Firebase("https://testingbasee.firebaseio.com");
Log.e("GOOOOOO!", "gooooooo!");
ref.createUser("aa#bb.com", "12345", new Firebase.ResultHandler() {
#Override
public void onSuccess() {
Log.e("GOOOOOO!","gooooooo!");
}
#Override
public void onError(FirebaseError firebaseError) {
Log.e("NOPOOOOOOOO!","noooo!!1!");
}
});
}
}