My Homepage has a problem, the app failed to start when i run it. but, when i comment out the code on .Homepage
in the AndroidManifest
the app can run.
Error at Logcat: 2022-05-31 13:28:30.991 5606-5606/com.example.limbangfoodhuntfortourism E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.limbangfoodhuntfortourism, PID: 5606 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.limbangfoodhuntfortourism/com.example.limbangfoodhuntfortourism.Homepage}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2951) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3016) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1716) at android.os.Handler.dispatchMessage(Handler.java:110) at android.os.Looper.loop(Looper.java:232) at android.app.ActivityThread.main(ActivityThread.java:6806) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1103) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference at com.example.limbangfoodhuntfortourism.Homepage.onCreate(Homepage.java:24) at android.app.Activity.performCreate(Activity.java:6974) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2904) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3016) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1716) at android.os.Handler.dispatchMessage(Handler.java:110) at android.os.Looper.loop(Looper.java:232) at android.app.ActivityThread.main(ActivityThread.java:6806) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1103) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)
this is the error: E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.limbangfoodhuntfortourism, PID: 27159 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.limbangfoodhuntfortourism/com.example.limbangfoodhuntfortourism.Homepage}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference
This is my Log in code:
package com.example.limbangfoodhuntfortourism;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
public class Login extends AppCompatActivity {
EditText mEmail, mPassword;
Button mLoginButton;
TextView mCreateLinkSignUp, mForgotPassword;
FirebaseAuth fAuth;
ProgressBar progressBar;
ImageView mLogo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mEmail = findViewById(R.id.email);
mPassword = findViewById(R.id.password);
progressBar = findViewById(R.id.progressBarLogin);
mLoginButton = findViewById(R.id.loginButton);
mCreateLinkSignUp = findViewById(R.id.signUpNow);
mForgotPassword = findViewById(R.id.forgotPassword);
fAuth = FirebaseAuth.getInstance();
mLoginButton.setOnClickListener((view) -> {
String email = mEmail.getText().toString().trim();
String password = mPassword.getText().toString().trim();
if(TextUtils.isEmpty(email)) {
mEmail.setError("Email is Required.");
return;
}
if(TextUtils.isEmpty(password)) {
mPassword.setError("Password is Required.");
return;
}
if(password.length() < 6) {
mPassword.setError("Password must be >= 6 characters");
return;
}
progressBar.setVisibility(View.VISIBLE);
// authenticate the user
fAuth.signInWithEmailAndPassword(email,password).addOnCompleteListener((task) -> {
if(task.isSuccessful()) {
Toast.makeText(Login.this, "Logged in Successfully!", Toast.LENGTH_SHORT).show();
startActivity(new Intent(getApplicationContext(),Homepage.class));
} else {
Toast.makeText(Login.this, "Error!" + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
}
});
});
mCreateLinkSignUp.setOnClickListener((view) -> {
startActivity(new Intent(getApplicationContext(), Register.class));
});
mLogo = findViewById(R.id.imageView);
mLogo.setImageResource(R.drawable.main_logo);
mForgotPassword.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final EditText resetMail = new EditText(view.getContext());
final AlertDialog.Builder passwordResetDialog = new AlertDialog.Builder(view.getContext());
passwordResetDialog.setTitle("Reset Password?");
passwordResetDialog.setMessage("Enter Your Email To Received Reset Link.");
passwordResetDialog.setView(resetMail);
passwordResetDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// extract the mail and send reset link
String mail = resetMail.getText().toString();
fAuth.sendPasswordResetEmail(mail).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void unused) {
Toast.makeText(Login.this, "Reset Link Sent To Your Email.", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(Login.this, "Error! Reset Link is Not Sent." + e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
});
passwordResetDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// close the dialog
}
});
passwordResetDialog.create().show();
}
});
}
}
this is my Homepage:
package com.example.limbangfoodhuntfortourism;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import com.google.firebase.auth.FirebaseAuth;
public class Homepage extends AppCompatActivity {
ImageView mLogo;
TextView mLogoutLink;
FirebaseAuth fAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_homepage);
mLogoutLink = findViewById(R.id.Logout);
mLogo = findViewById(R.id.imageView);
mLogo.setImageResource(R.drawable.main_logo);
fAuth = FirebaseAuth.getInstance();
mLogoutLink.setOnClickListener((view) -> {
startActivity(new Intent(getApplicationContext(), Logout.class));
});
}
}
This is my AndroidManifest code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.limbangfoodhuntfortourism">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.LimbangFoodHuntForTourism"
tools:targetApi="31">
<activity
android:name=".Logout"
android:exported="true" />
<activity
android:name=".RestaurantDetails"
android:exported="true" />
<activity
android:name=".Homepage"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Login"
android:exported="true" />
<activity
android:name=".Register"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:exported="true" />
</application>
</manifest>