0

I have three layouts, activity_main, activity_login and activity_home. When in the LoginActivity or HomeActivity java file, I get the error "cannot resolve symbol activity_(home or login). I have tried deleting it and creating a new one by clicking "alt+enter" on the error but still did not work. I'm thinking I have to add something to the AndroidManifest but we never went over that in class. If anyone could help that'd be great. Thanks.

Here is my activity_main.xml (note: my activity_login and activity_main are pretty much the same exact code just different @+id's):

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <EditText
        android:id="@+id/emailEditText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="300dp"
        android:ems="10"
        android:hint="email"
        android:inputType="textEmailAddress"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/passwordEditText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="password"
        android:inputType="textPassword"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/emailEditText"
        app:layout_constraintVertical_bias="0.052" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="356dp"
        android:layout_height="235dp"
        android:cropToPadding="false"
        android:scaleType="fitStart"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.032"
        app:srcCompat="@drawable/guitar" />

    <Button
        android:id="@+id/signUpButton"
        android:layout_width="133dp"
        android:layout_height="73dp"
        android:text="Sign Up!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/passwordEditText"
        app:layout_constraintVertical_bias="0.54" />

    <TextView
        android:id="@+id/signInTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Already have an account? Sign in here!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/signUpButton" />

</androidx.constraintlayout.widget.ConstraintLayout>

Here is my LoginActivity.java:

package com.antonioocasio.finalproject;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

public class LoginActivity extends AppCompatActivity {

    public EditText emailIdEditText, passwordEditText;
    private FirebaseAuth.AuthStateListener firebaseAuthStateListener;
    Button signInBtn;
    TextView signUpTextView;
    FirebaseAuth firebaseAuth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        firebaseAuth = FirebaseAuth.getInstance();
        emailIdEditText = findViewById(R.id.emailEditText2);
        passwordEditText = findViewById(R.id.passwordEditText);
        signInBtn = findViewById(R.id.signInButton);
        signUpTextView = findViewById(R.id.signUpTextView);

        firebaseAuthStateListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
                if(firebaseUser != null){
                    Toast.makeText(LoginActivity.this, "You are logged in", Toast.LENGTH_LONG).show();
                    Intent i = new Intent(LoginActivity.this, HomeActivity.class);
                    startActivity(i);
                } else {
                    Toast.makeText(LoginActivity.this, "Please Login", Toast.LENGTH_LONG).show();

                }
            }
        };
        signInBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String email = emailIdEditText.getText().toString();
                String password = passwordEditText.getText().toString();

                if(email.isEmpty()){
                    Toast.makeText(getApplicationContext(), "Error: Email must be entered.", Toast.LENGTH_LONG).show();
                    emailIdEditText.requestFocus();

                } else if(password.isEmpty()){
                    Toast.makeText(getApplicationContext(), "Error: Password must be entered.", Toast.LENGTH_LONG).show();
                    passwordEditText.requestFocus();

                } else if(email.isEmpty() && password.isEmpty()){
                    Toast.makeText(getApplicationContext(), "Error:Both fields must be entered.", Toast.LENGTH_LONG).show();

                } else if(!(email.isEmpty() && password.isEmpty())){
                    firebaseAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            if(!task.isSuccessful()){
                                Toast.makeText(getApplicationContext(), "Login Error, Please try again.", Toast.LENGTH_LONG).show();

                            } else {
                                Intent intToHome = new Intent(LoginActivity.this, HomeActivity.class);
                                startActivity(intToHome);
                            }
                        }
                    });
                } else {
                    Toast.makeText(getApplicationContext(), "Error Occurred!", Toast.LENGTH_LONG).show();

                }
            }
        });

        signUpTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intSignUp = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intSignUp);
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        firebaseAuth.addAuthStateListener(firebaseAuthStateListener);
    }
}

Finally, here is my AndroidMainfiest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.antonioocasio.finalproject">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".HomeActivity"></activity>
        <activity android:name=".LoginActivity" />
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Again, I'm really not sure what the problem could be. I am at a complete loss.

EDIT: This error finally popped up. I really don't know where to start since there is so much to it:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.antonioocasio.finalproject, PID: 17363
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.antonioocasio.finalproject/com.antonioocasio.finalproject.MainActivity}: 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:3270)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     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.antonioocasio.finalproject.MainActivity.onCreate(MainActivity.java:39)
        at android.app.Activity.performCreate(Activity.java:7802)
        at android.app.Activity.performCreate(Activity.java:7791)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7356) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
  • In `MainActivity`'s `onCreate()` method, you're calling `setOnClickListener()` on a null `Button`. – Mike M. May 07 '20 at 04:27
  • whenever i had couldn't resolve error. i used to invalidate cache and restart. it would fix my error. hope it does yours too – SaWin0 May 07 '20 at 04:31
  • @MikeM. Okay thanks. I fixed that, I got the variable names mixed up. Do you have an idea on why I get the "cannot resolve symbol activity_(home or login)" error? I have both files created so I'm not sure what the error is caused from. – cprogramnewb May 07 '20 at 04:33
  • @SaWin0 alright I'll try that right now thanks for the tip. – cprogramnewb May 07 '20 at 04:33
  • 1
    thanks for your help you guys I appreciate it so much. Restarting did fix it and fixing the variable name did as well. – cprogramnewb May 07 '20 at 04:38

0 Answers0