简体   繁体   English

连接到 Firebase 的问题

[英]problems connecting to firebase

I have made an app with a short splash screen to display the logo image followed with a login page following tutorials online.我制作了一个带有简短启动画面的应用程序,用于显示徽标图像,然后是在线教程后的登录页面。 I have connected firebase where I have saved one email address and password which can be used.我已经连接了 firebase,在那里我保存了一个可以使用的电子邮件地址和密码。 However I have recently added functionality for the login to work correctly and it now gets stuck at the splash screen saying it has recently stopped working.但是,我最近添加了登录功能以使其正常工作,现在它卡在初始屏幕上,说它最近停止工作。

I understand that I have probably used the wrong version of something but have no idea how to find where I have used the wrong version to amend it我知道我可能使用了错误版本的某些内容,但不知道如何找到使用错误版本的地方来修改它

All I want from this app so far is for the splash screen to work correctly take you to the login page and then to be able to use the email address I have setup to use.到目前为止,我想要从这个应用程序中获得的只是启动画面能够正常工作,将您带到登录页面,然后能够使用我设置使用的电子邮件地址。

this error is shown on this screen however I am not sure where I have used wrong version此错误显示在此屏幕上,但是我不确定在哪里使用了错误的版本

All com.android.support libraries must use the exact same version specification (mixing versions can lead to runtime crashes).所有 com.android.support 库必须使用完全相同的版本规范(混合版本可能导致运行时崩溃)。 Found versions 28.0.0, 26.1.0.找到版本 28.0.0、26.1.0。 Examples include com.android.support:animated-vector-drawable:28.0.0 and com.android.support:support-media-compat:26.1.0 less... (Ctrl+F1) Inspection info:There are some combinations of libraries, or tools and libraries, that are incompatible, or can lead to bugs.示例包括 com.android.support:animated-vector-drawable:28.0.0 和 com.android.support:support-media-compat:26.1.0 less... (Ctrl+F1) 检查信息:有一些组合不兼容或可能导致错误的库或工具和库。 One such incompatibility is compiling with a version of the Android support libraries that is not the latest version (or in particular, a version lower than your targetSdkVersion).一种此类不兼容性是使用非最新版本的 Android 支持库版本(或者特别是低于您的 targetSdkVersion 的版本)进行编译。 Issue id: GradleCompatible"问题 ID:GradleCompatible”

Here is login activity这是登录活动

public class LoginActivity extends AppCompatActivity {
//variables
    private EditText loginEmailText;
    private EditText loginPassText;
    private Button loginBtn;
     private Button loginRegBtn;

     private FirebaseAuth nAuth;

     private ProgressBar loginProgress;

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

        nAuth = FirebaseAuth.getInstance();

        loginEmailText = (EditText) findViewById(R.id.login_email);
        loginPassText = (EditText) findViewById(R.id.login_password);
        loginBtn = (Button) findViewById(R.id.login_btn);
        loginRegBtn = (Button) findViewById(R.id.login_reg_btn);
        loginProgress = (ProgressBar) findViewById(R.id.login_progress);


        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                loginProgress.setVisibility(View.VISIBLE);
                String loginEmail = loginEmailText.getText().toString();
                String loginPass = loginPassText.getText().toString();

                if (TextUtils.isEmpty(loginEmail) && !TextUtils.isEmpty(loginPass)) {
                    loginProgress.setVisibility(View.VISIBLE);

                    nAuth.signInWithEmailAndPassword(loginEmail, loginPass).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            if (task.isSuccessful()) {

                                sendToMain();
                            } else {
                                String errorMessage = task.getException().getMessage();
                                Toast.makeText(LoginActivity.this, "Error:" + errorMessage, Toast.LENGTH_SHORT).show();

                            }
                            loginProgress.setVisibility(View.INVISIBLE);
                        }
                    });


                }

            }
        });
    }



    @Override
    protected void onStart() {
        super.onStart();
//this is to see if user logged in or not
        FirebaseUser currentUser = nAuth.getCurrentUser();

        if(currentUser!=null){
            sendToMain();
        }


    }

    private void sendToMain() {
        Intent mainIntent = new Intent(LoginActivity.this,MainActivity.class);
        startActivity(mainIntent);
        finish();
    }
}

Here is login activity xml这是登录活动xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:background="@color/colorPrimary"
    tools:context=".LoginActivity">

    <Button
        android:id="@+id/login_reg_btn"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:text="@string/login_reg_btn_text"
        android:textAllCaps="false"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/login_btn" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="0dp"
        android:layout_height="452dp"
        android:contentDescription="@string/App_logo"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/logo"

        tools:layout_editor_absoluteX="0dp" />

    <EditText
        android:id="@+id/login_email"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:autofillHints=""
        android:ems="10"
        android:hint="@string/Email_hint"
        android:inputType="textEmailAddress"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView2" />

    <EditText
        android:id="@+id/login_password"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="24dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:ems="10"
        android:hint="@string/Pass_hint"
        android:importantForAutofill="no"
        android:inputType="textPassword"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/login_email" />

    <Button
        android:id="@+id/login_btn"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:text="@string/Login_btn_text"
        android:textAllCaps="false"
        android:textColorLink="@color/colorPrimaryDark"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/login_password" />

    <ProgressBar
        android:id="@+id/login_progress"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:indeterminate="true"
        android:visibility="invisible"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>


Here is main activity这是主要活动

package com.example.splashscreentest;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent intent = new Intent(MainActivity.this, LoginActivity.class);
        startActivity(intent);
    }

    @Override
    protected void onStart() {
        super.onStart();
        FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
        if (currentUser == null){
            Intent loginIntent = new Intent(MainActivity.this,LoginActivity.class);
            startActivity(loginIntent);
            finish();
        }
    }
}

here is main activity xml这是主要活动xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="401dp"
        android:layout_height="36dp"
        android:text="TextView"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="285dp" />

    <Button
        android:id="@+id/button2"
        android:layout_width="407dp"
        android:layout_height="47dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:text="Sign Up"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.666"
        app:layout_constraintStart_toStartOf="parent"
        tools:layout_editor_absoluteY="442dp" />

    <Button
        android:id="@+id/button"
        android:layout_width="407dp"
        android:layout_height="47dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:text="Login"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:layout_editor_absoluteY="381dp" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="401dp"
        android:layout_height="36dp"
        android:text="TextView"
        tools:layout_editor_absoluteX="2dp"
        tools:layout_editor_absoluteY="230dp" />
</android.support.constraint.ConstraintLayout>

Here is Splash screen这是启动画面

package com.example.splashscreentest;

import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class SplashScreen extends AppCompatActivity {



         static int SPLASH_TIME_OUT = 3000;

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

            new Handler().postDelayed(new Runnable() {


                @Override
                public void run() {
                    // This method will be executed once the timer is over
                    // Start your app main activity
                    Intent i = new Intent(SplashScreen.this, MainActivity.class);
                    startActivity(i);

                    // close this activity
                    finish();
                }
            }, SPLASH_TIME_OUT);
        }
    }

Here is splash screen xml这是闪屏xml

<RelativeLayout 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"
    android:background="@android:color/white" >

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/app_name"
        app:srcCompat="@drawable/logo" />





</RelativeLayout>



here is android manifest这是安卓清单


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

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

    <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=".SplashScreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".LoginActivity"
            android:label="@string/app_name"
            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
            </intent-filter>
        </activity>
    </application>

</manifest>

this is one of the gradle files with an error#这是有错误的 gradle 文件之一#

apply plugin: 'com.google.gms.google-services'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.splashscreentest"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {

    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.google.firebase:firebase-auth:16.0.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

Have you downloaded and the Google-Services json file to your app folder?您是否已将 Google-Services json 文件下载到您的应用程序文件夹?

You'll also need to apply it to your gradle file at the bottom, I can't see it in yours:您还需要将其应用于底部的 gradle 文件,我在您的文件中看不到它:

apply plugin: 'com.google.gms.google-services'

As what @cricket_007 said you need to add implementation 'com.google.firebase:firebase-auth:16.1.0' to your gradle file as well.正如@cricket_007 所说,您还需要将implementation 'com.google.firebase:firebase-auth:16.1.0'到您的 gradle 文件中。

Tools-Firebase-cloud messaging , set up Firebase Cloud Messaging , steps will work smoothly if you do one by one. Tools-Firebase-cloud Messaging ,设置Firebase Cloud Messaging ,一步一步来,步骤会很顺利。

https://firebase.google.com/docs/android/setup#available_libraries https://firebase.google.com/docs/android/setup#available_libraries

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM