我想从Firebase到手机上进行OTP以进行验证,我已经按照文档所述完成了所有工作,但是最后我没有得到OTP,请帮助我,我知道我的代码有问题,任何人都可以纠正我? 我的应用已连接并且正在使用Firebase,这是我的活动Java代码:
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
}
}
这是logcat
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
}
}
Applicationclass
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'
}
答案 0 :(得分:1)
确保您不在模拟器上,然后确保已将有效的SHA-1密钥添加到Firebase控制台。
尝试此操作以检查您遇到的错误
@Override
public void onVerificationFailed(FirebaseException e) {
e.printStackTrace();
}
答案 1 :(得分: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>