An error occurred java.lang.RuntimeException: Unable to start activity ComponentInfo.
Code Error:
java.lang.RuntimeException: Unable to start activity
ComponentInfo{firebase.app.ph/firebase.app.ph.LoginActivity}:
java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2757)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2818)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1557)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:163)
at android.app.ActivityThread.main(ActivityThread.java:6393)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:933)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at firebase.app.ph.LoginActivity.onCreate(LoginActivity.java:57)
at android.app.Activity.performCreate(Activity.java:6858)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2710)
... 9 more
Code app: StartActivity:
public class StartActivity extends AppCompatActivity {
Button login , register;
FirebaseUser firebaseUser;
@Override
protected void onStart() {
super.onStart();
firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
if(firebaseUser!=null){
startActivity(new Intent(StartActivity.this,MainActivity.class));
finish();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
login = findViewById(R.id.login);
register = findViewById(R.id.register);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(StartActivity.this,LoginActivity.class));
}
});
register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(StartActivity.this,RegisterActivity.class));
}
});
}
}
LoginActivity:
public class LoginActivity extends AppCompatActivity {
EditText email, pass;
Button login;
TextView txtsignup;
FirebaseAuth auth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
email = findViewById(R.id.email);
login = findViewById(R.id.login);
pass = findViewById(R.id.pass);
txt_signup = findViewById(R.id.txtsignup);
auth = FirebaseAuth.getInstance();
txt_signup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
}
});
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final ProgressDialog pd = new ProgressDialog(LoginActivity.this);
pd.setMessage("Please wait...");
pd.show();
String str_email = email.getText().toString();
String str_pass = pass.getText().toString();
if (TextUtils.isEmpty(str_email) || TextUtils.isEmpty(str_pass)) {
Toast.makeText(LoginActivity.this, "All Filed are required!", Toast.LENGTH_SHORT).show();
} else {
auth.signInWithEmailAndPassword(str_email, str_pass)
.addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("Users").child(auth.getCurrentUser().getUid());
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
pd.dismiss();
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
pd.dismiss();
}
});
} else {
pd.dismiss();
Toast.makeText(LoginActivity.this, "!", Toast.LENGTH_SHORT).show();
}
}
});
}
}
});
}
}
activity_login.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="10dp"
android:background="@drawable/b228"
tools:context=".LoginActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="150dp"
android:src="@drawable/image"></ImageView>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/edittext_background"
android:hint="Email"
android:layout_marginTop="15dp"
android:id="@+id/email"
android:inputType="textEmailAddress"
android:padding="10dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/edittext_background"
android:hint="Password"
android:layout_marginTop="15dp"
android:id="@+id/pass"
android:inputType="textPass"
android:padding="10dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/login"
android:text="Log in"
android:textColor="@color/colorPrimary"
android:background="@drawable/button_background"
android:layout_marginTop="30dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="!"
android:id="@+id/txt_signup"
android:textColor="@color/colorWhite"
android:layout_marginTop="15dp"/>
I also have activity_register.xml and RegisterActivity, but they work great unlike Login. I just started programming in java and on Android Studio, so I will be glad to hear your answers!