【问题标题】:OTP is not getting from firebase code is properOTP 没有从 firebase 代码中获取是正确的
【发布时间】:2019-03-08 13:07:34
【问题描述】:

我想从 Firebase 获取 OTP 到我的手机以进行验证,我已按照文档所述完成所有操作,但最后我没有得到 OTP 请帮助我,我知道我的代码有问题任何人都可以纠正我? 我的应用程序已连接并使用 firebase 这是我的活动 java 代码:

still am getting error

public class LoginActivity extends AppCompatActivity {

String phone_number;
FirebaseAuth auth;
PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;

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

    mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks(){
        @Override
        public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {

        }

        @Override
        public void onVerificationFailed(FirebaseException e) {

        }
    };

    CardView card_view = findViewById(R.id.cardView);
    card_view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            EditText phonetextview = findViewById(R.id.phonetextview);
            phone_number = PhoneNumberUtils.formatNumber(phonetextview.getText().toString());

            send();

            Toast toast = Toast.makeText(getApplicationContext(), phone_number,
                    Toast.LENGTH_SHORT);
            toast.show();
        }
    });
}

private void send() {

    PhoneAuthProvider.getInstance().verifyPhoneNumber(
            phone_number,        // Phone number to verify
            60,                 // Timeout duration
            TimeUnit.SECONDS,   // Unit of timeout
            this,               // Activity (for callback binding)
            mCallbacks);             // ForceResendingToken from callbacks
    }
}

这里是日志猫

2019-03-08 15:29:36.095 22108-22108/com.example.meenvandi E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.meenvandi, PID: 22108
java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.example.meenvandi. Make sure to call FirebaseApp.initializeApp(Context) first.
    at com.google.firebase.FirebaseApp.getInstance(com.google.firebase:firebase-common@@16.0.1:219)
    at com.google.firebase.auth.PhoneAuthProvider.getInstance(Unknown Source:5)
    at com.example.meenvandi.LoginActivity.send(LoginActivity.java:96)
    at com.example.meenvandi.LoginActivity.access$000(LoginActivity.java:28)
    at com.example.meenvandi.LoginActivity$2.onClick(LoginActivity.java:77)
    at android.view.View.performClick(View.java:6330)
    at android.view.View$PerformClick.run(View.java:25136)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:192)
    at android.app.ActivityThread.main(ActivityThread.java:6778)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:875)

2019-03-08 15:29:36.108 22108-22108/com.example.meenvandi I/Process:发送信号。 PID:22108 SIG:9

i am getting this error after creating class

<?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.meenvandi"
tools:ignore="ExtraText">


<uses-permission 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=".Otp"></activity>
    <activity
        android:name=".LoginActivity"
        android:theme="@style/AppTheme.NoActionBar" />
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

更新代码

public class LoginActivity extends AppCompatActivity {

String phone_number;
 FirebaseAuth auth;
 PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;


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




    mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() 
 {

        @Override
        public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {

        }

        @Override
        public void onVerificationFailed(FirebaseException e) {
            {
                e.printStackTrace();

                Toast toast = Toast.makeText(getApplicationContext(), (CharSequence) e,
                        Toast.LENGTH_SHORT);
                toast.show();

            }

        }
    };


    CardView card_view = findViewById(R.id.cardView);

    card_view.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {




            EditText phonetextview = findViewById(R.id.phonetextview);
            phone_number = PhoneNumberUtils.formatNumber(phonetextview.getText().toString());

            send();



            Toast toast = Toast.makeText(getApplicationContext(), phone_number,
                    Toast.LENGTH_SHORT);
            toast.show();



        }


    });

}

private void send() {
    FirebaseApp.initializeApp(this);
    PhoneAuthProvider.getInstance().verifyPhoneNumber(
            phone_number,        // Phone number to verify
            60,                 // Timeout duration
            TimeUnit.SECONDS,   // Unit of timeout
            this,               // Activity (for callback binding)
            mCallbacks);             // ForceResendingToken from callbacks


}

}

应用类

package com.example.meenvandi;

import android.app.Application;

import com.google.firebase.FirebaseApp;

public class Applicationclass extends Application {
@Override
public void onCreate() {
    super.onCreate();
    FirebaseApp.initializeApp(this);//here we are intializing firebase
}
}

更新清单

<?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.meenvandi"
tools:ignore="ExtraText">


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


<application
    android:name="com.example.meenvandi.Applicationclass"
    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=".Otp"></activity>
    <activity
        android:name=".LoginActivity"
        android:theme="@style/AppTheme.NoActionBar" />
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

</manifest>

分级 1

// Top-level build file where you can add configuration options common to 
all sub-projects/modules.

buildscript {
repositories {
    google()
    jcenter()

}
dependencies {
    classpath 'com.android.tools.build:gradle:3.3.1'
    classpath 'com.google.gms:google-services:4.1.0'

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}
}

 allprojects {
  repositories {
    google()
    jcenter()

}
}

 task clean(type: Delete) {
  delete rootProject.buildDir
 }

毕业2

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

android {
 compileSdkVersion 28
 defaultConfig {
    applicationId "com.example.meenvandi"
    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:design:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.google.firebase:firebase-core:16.0.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'
 }

【问题讨论】:

  • 你遇到了什么错误?
  • 尝试@Override public void onVerificationFailed(FirebaseException e) { e.printStackTrace(); }
  • 代码正确吗?
  • 是的,它看起来不错。
  • 那为什么会这样呢?

标签: java android firebase firebase-authentication


【解决方案1】:

您需要在使用它之前intialize firebase。为此创建一个Application 类。即:

public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        FirebaseApp.initializeApp(this);//here we are intializing firebase
    }
}

别忘了把这个添加到你的AndroidManifest.xml

<application
        android:name="yourPackage.MyApplication"
        android:allowBackup="true"
        android:icon="@mipmap/route"
        android:theme="@style/AppTheme">
 <!--your activity classes-->
  <activity
            android:name=".YourActivity"
            >
 </application>

【讨论】:

  • 查看来自quiestion的截图
  • 它应该扩展 Application 而不是 LoginActivity ,请正确查看我的代码!
  • 我应该用你的代码创建新的 java 类,对吗?我是新手
  • android:name=".YourActivity" 这里我应该添加什么? android:name=".LoginActivity" ?
  • 是的,您的所有activity classes 都在您的项目中。
【解决方案2】:

确保您不在模拟器上,然后确保您已将有效的 SHA-1 密钥添加到 Firebase 控制台。

试试这个来检查你遇到了什么错误

    @Override 
     public void onVerificationFailed(FirebaseException e) { 
           e.printStackTrace(); 
     } 

【讨论】:

  • fire-base 52:4e:0c:e9:a0:7a:af:89:23:29:1f:74:31:06:95:2c 中有一个 SHA-1 指纹:f5:99:e0:cb SHA-1
  • 我添加了 logcat
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-20
  • 2021-09-01
  • 2021-07-23
  • 2020-12-08
相关资源
最近更新 更多