-1

For some time, I have been been having issues with getting through/loading one of my activities which is the login screen. First activity which launches has two buttons "Sign In" and "Sign Up" with both identical onClick functions defined. When I select "Sign Up", the "Register" activity launches, however, when I select the "Sign In" button, nothing happens ("Login" activity's supposed to launch), the app freezes then I have to manually shut it down because it crashes.

In order to test it in a different matter, I set the "Login" activity as MAIN and LAUNCHER in AndroidManifest.xml to see if it can launch first without any help from other activities, however, only a blank white screen comes up, and once again, nothing else happens. It has worked previously on many occasions, and I've no idea what could be causing this to be happening. Any help will be much appreciated!

My AndroidManifest.xml code:

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

<uses-permission android:name="android.permission.INTERNET" />

<application
    android:name=".TodayApp"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity android:name=".OpeningActivity"/>

    <activity
        android:name="com.facebook.FacebookActivity"
        android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Translucent.NoTitleBar" />

    <meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/facebook_application_id" />

    <activity android:name=".LoginActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <activity android:name=".RegisterActivity"/>

    <activity android:name=".MainActivity"/>

    <!--
 ATTENTION: This was auto-generated to add Google Play services to your     project for
 App Indexing.  See https://g.co/AppIndexing/AndroidStudio for more information.
    -->
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

</application>

<activity
    android:name=".SettingsActivity"
    android:label="@string/title_activity_settings"
    android:parentActivityName=".MainActivity">
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.example.michael.whatsupldn.MainActivity" />
</activity>

</manifest>

You can view the emulator output here.

EDIT:

My XML code for LoginActivity:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#E0E0E0"
    tools:context="com.example.michael.whatsupldn.LoginActivity">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:src="@drawable/darkest_london_city_blur"
        android:layout_alignParentTop="true"
        android:id="@+id/imageView"
        android:contentDescription="@string/city_of_london" />

    <LinearLayout
        android:id="@+id/db1_root"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/txtWelcome"
            android:text="@string/welcome"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:paddingTop="50dp"
            android:textColor="#FAFAFA"
            android:textSize="55sp"/>

        <TextView
            android:id="@+id/txtSignInBelow"
            android:text="@string/please_sign_in_below"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_marginTop="8dp"
            android:textColor="#FAFAFA"
            android:gravity="center" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical"
            android:weightSum="1">

            <EditText
                android:id="@+id/editTextEmail"
                android:hint="@string/email"
                android:background="@drawable/shape"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:layout_marginTop="16dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:layout_marginBottom="8dp"
                android:inputType="textEmailAddress"
                android:textColor="#FAFAFA"
                android:textColorHint="#FAFAFA" />

            <EditText
                android:id="@+id/editTextPassword"
                android:hint="@string/password"
                android:background="@drawable/shape"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:layout_marginTop="8dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:layout_marginBottom="16dp"
                android:textColor="#FAFAFA"
                android:textColorHint="#FAFAFA"/>
            <!-- android:inputType="textPassword"-->

            <Button
                android:id="@+id/signinButton"
                android:text="@string/sign_in"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:textAllCaps="false"
                android:background="@drawable/shape_button"
                android:textSize="16sp"
                android:onClick="signinButton_onClick"/>

            <TextView
                android:id="@+id/Or"
                android:text="@string/or"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="20sp"
                android:textColor="#FAFAFA"
                android:layout_margin="8dp"
                android:textAllCaps="false"/>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <com.facebook.login.widget.LoginButton
                    android:id="@+id/facebookButton"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:paddingTop="12dp"
                    android:paddingBottom="12dp"
                    android:layout_weight="1"
                    android:layout_marginStart="16dp"
                    android:layout_marginEnd="4dp"/>

                <com.google.android.gms.common.SignInButton
                    android:id="@+id/googleButton"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_weight="1"
                    android:layout_marginStart="4dp"
                    android:layout_marginEnd="16dp"/>

            </LinearLayout>

        </LinearLayout>

    </LinearLayout>

</RelativeLayout>

You can view my LoginActivity here.

My Login Java code:

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;
import com.google.android.gms.auth.api.Auth;

import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.auth.api.signin.GoogleSignInResult;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.GoogleAuthProvider;

import java.util.Arrays;

public class LoginActivity extends AppCompatActivity {

    private LoginButton facebookButton;
    private CallbackManager callbackManager;

    private SignInButton mGoogleButton;

    private static final int RC_SIGN_IN = 1;

    private GoogleApiClient mGoogleApiClient;

    private FirebaseAuth mAuth;
    private FirebaseAuth.AuthStateListener mAuthListener;

    private static final String TAG = "LOGIN_ACTIVITY";

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

        mAuth = FirebaseAuth.getInstance();
        mAuthListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                if (firebaseAuth.getCurrentUser() != null ) {
                    startActivity(new Intent(LoginActivity.this, MainActivity.class));
                }
            }
        };

        mGoogleButton = (SignInButton) findViewById(R.id.googleButton);


        // Configure Google Sign In
        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(getString(R.string.default_web_client_id))
                .requestEmail()
                .build();

        mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext())
                .enableAutoManage(this, new GoogleApiClient.OnConnectionFailedListener() {
                    @Override
                    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
                        Toast.makeText(LoginActivity.this, "You have an error", Toast.LENGTH_LONG).show();
                    }
                })
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .build();

        mGoogleButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                signIn();
            }
        });



        callbackManager = CallbackManager.Factory.create();

        facebookButton = (LoginButton) findViewById(R.id.facebookButton);

        facebookButton.setReadPermissions(Arrays.asList("email"));

        facebookButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                goMainScreen();
            }

            @Override
            public void onCancel() {
                Toast.makeText(getApplicationContext(), R.string.com_facebook_loginview_cancel_action, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError(FacebookException error) {
                Toast.makeText(getApplicationContext(), R.string.com_facebook_internet_permission_error_message, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();

        mAuth.addAuthStateListener(mAuthListener);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(requestCode, resultCode, data);

        // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
        if (requestCode == RC_SIGN_IN) {
            GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
            if (result.isSuccess()) {
                // Google Sign In was successful, authenticate with Firebase
                GoogleSignInAccount account = result.getSignInAccount();
                firebaseAuthWithGoogle(account);
            } else {
                // Google Sign In failed, update UI appropriately
                // ...
            }
        }
    }

    private void firebaseAuthWithGoogle(GoogleSignInAccount account) {
        AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null);
        mAuth.signInWithCredential(credential)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());

                        // If sign in fails, display a message to the user. If sign in succeeds
                        // the auth state listener will be notified and logic to handle the
                        // signed in user can be handled in the listener.
                        if (!task.isSuccessful()) {
                            Log.w(TAG, "signInWithCredential", task.getException());
                            Toast.makeText(LoginActivity.this, "Authentication failed.",
                                    Toast.LENGTH_SHORT).show();
                        }
                        // ...
                    }
                });
    }

    private void signIn() {
        Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
        startActivityForResult(signInIntent, RC_SIGN_IN);
    }

    private void goMainScreen() {
        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }

    public void signinButton_onClick(View view) {
        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
        startActivity(intent);
    }
}
AndroidDevBro
  • 151
  • 1
  • 3
  • 13

1 Answers1

1

Place your Activity element inside the </Application> tag. Not outside it:

My AndroidManifest.xml code:

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

<uses-permission android:name="android.permission.INTERNET" />

<application
    android:name=".TodayApp"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity android:name=".OpeningActivity"/>

    <activity
        android:name="com.facebook.FacebookActivity"
        android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Translucent.NoTitleBar" />

    <meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/facebook_application_id" />

    <activity android:name=".LoginActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <activity android:name=".RegisterActivity"/>

    <activity android:name=".MainActivity"/>

      // changes made here
    <activity
    android:name=".SettingsActivity"
    android:label="@string/title_activity_settings"
    android:parentActivityName=".MainActivity">
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.example.michael.whatsupldn.MainActivity" />

    </activity>

    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

</application>

</manifest>

Although add your LoginActivity Code.

W4R10CK
  • 5,502
  • 2
  • 19
  • 30