I have two buttons that send to two different activities (they are used to fill a form and submit).
I am using the code for both activities (with some changes), but one works yet the other CRASHES. This is the code for the working one
public class InstaForm extends AppCompatActivity {
//
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener firebaseAuthListener;
//
private EditText mUserId, mPassword;
private Button mAdd, mModify;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_insta_form);
//
mAuth = FirebaseAuth.getInstance();
//
mUserId = (EditText) findViewById(R.id.userIdInsta);
mPassword = (EditText) findViewById(R.id.passwordInsta);
mAdd = (Button) findViewById(R.id.btnInsta);
mModify = (Button) findViewById(R.id.btnModifyInsta);
FirebaseDatabase database = FirebaseDatabase.getInstance();
final DatabaseReference mRef = database.getReference();
mAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String userId = mUserId.getText().toString();
String password = mPassword.getText().toString();
String user_id_auth = mAuth.getUid();
DatabaseReference current_user_db = FirebaseDatabase.getInstance().getReference().child("Users").child(user_id_auth).child("Insta");
Map newPost = new HashMap();
newPost.put("IdOfUser", userId);
newPost.put("Password", password);
current_user_db.setValue(newPost).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (!task.isSuccessful()) {
Toast.makeText(InstaForm.this, "Error! Please try again.", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(InstaForm.this, "SUCCESSFULLY REGISTERED!", Toast.LENGTH_LONG).show();
Intent intent = new Intent(InstaForm.this, SocialOptionsActivity.class);
startActivity(intent);
finish();
return;
}
}
});
}
});
mModify.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
}
and this one is crashing
public class TwitterForm extends AppCompatActivity {
//
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener firebaseAuthListener;
//
private EditText mUserId, mPassword;
private Button mAdd, mModify;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_insta_form);
//
mAuth = FirebaseAuth.getInstance();
//
mUserId = (EditText) findViewById(R.id.userIdTwitter);
mPassword = (EditText) findViewById(R.id.passwordTwitter);
mAdd = (Button) findViewById(R.id.btnTwitter);
mModify = (Button) findViewById(R.id.btnModifyTwitter);
FirebaseDatabase database = FirebaseDatabase.getInstance();
final DatabaseReference mRef = database.getReference();
mAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String userId = mUserId.getText().toString();
String password = mPassword.getText().toString();
String user_id_auth = mAuth.getUid();
DatabaseReference current_user_db = FirebaseDatabase.getInstance().getReference().child("Users").child(user_id_auth).child("Twitter");
Map newPost = new HashMap();
newPost.put("IdOfUser", userId);
newPost.put("Password", password);
current_user_db.setValue(newPost).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (!task.isSuccessful()) {
Toast.makeText(TwitterForm.this, "Error! Please try again.", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(TwitterForm.this, "SUCCESSFULLY REGISTERED!", Toast.LENGTH_LONG).show();
Intent intent = new Intent(TwitterForm.this, SocialOptionsActivity.class);
startActivity(intent);
finish();
return;
}
}
});
}
});
mModify.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
}
My logcat is this
11-29 12:16:02.193 4588-4588/com.example.android.snapgrowth7 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.snapgrowth7, PID: 4588
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.snapgrowth7/com.example.android.snapgrowth7.TwitterForm}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2659)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.android.snapgrowth7.TwitterForm.onCreate(TwitterForm.java:48)
at android.app.Activity.performCreate(Activity.java:6672)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2612)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
The first line after the spaced part in the logcat, says the error is in the line 48 of the second code, but I just can not figure out what I did wrong.
Can anyone suggest? Thanks