我正在尝试创建一个Android程序,遇到一个错误,我不知道如何解决。 输入电子邮件后,我的应用程序崩溃了。我怀疑问题与Firebase有关。我尝试了解决此问题的不同方法,例如更改了Firebase实现的版本,但没有成功。
这是我的代码
MainActivity.java
package com.chat.mychatapp;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.firebase.ui.auth.AuthUI;
import com.firebase.ui.database.FirebaseListAdapter;
import com.github.library.bubbleview.BubbleTextView;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.FirebaseDatabase;
import android.text.format.DateFormat;
import java.util.Objects;
public class MainActivity extends AppCompatActivity {
public static int SIGN_IN_CODE = 1;
private RelativeLayout activity_main;
private FirebaseListAdapter<Message> adapter;
private FloatingActionButton sendBtn;
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == SIGN_IN_CODE){
if(resultCode == RESULT_OK){
Snackbar.make(activity_main, "You are authorized", Snackbar.LENGTH_LONG).show();
displayAllMessages();
}else{
Snackbar.make(activity_main, "You are NOT authorized", Snackbar.LENGTH_LONG).show();
finish();
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activity_main = findViewById(R.id.activity_main);
sendBtn = findViewById(R.id.sendBtn);
sendBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText textField = findViewById(R.id.messageField);
if(textField.getText().toString().equals(""))
return;
FirebaseDatabase.getInstance().getReference().push().setValue(
new Message(Objects.requireNonNull(FirebaseAuth.getInstance().getCurrentUser()).getEmail(),
textField.getText().toString()
)
);
textField.setText("");
}
});
//User not autorized
if(FirebaseAuth.getInstance().getCurrentUser() == null){
startActivityForResult(AuthUI.getInstance().createSignInIntentBuilder().build(), SIGN_IN_CODE);
}else{
Snackbar.make(activity_main, "You are authorized", Snackbar.LENGTH_LONG).show();
displayAllMessages();
}
}
private void displayAllMessages() {
ListView listOfMessages = findViewById(R.id.messageList);
adapter = new FirebaseListAdapter<Message>(this, Message.class, R.layout.list_item, FirebaseDatabase.getInstance().getReference()) {
@Override
protected void populateView(View v, Message model, int position) {
TextView m_user, m_time;
BubbleTextView m_text;
m_user = v.findViewById(R.id.messageUser);
m_time = v.findViewById(R.id.messageTime);
m_text = v.findViewById(R.id.messageText);
m_user.setText(model.getUserName());
m_text.setText(model.getMessage());
m_time.setText(DateFormat.format("dd-mm-yyyy HH:mm:ss",model.getTime()));
}
};
listOfMessages.setAdapter(adapter);
}
}
build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.chat.mychatapp"
minSdkVersion 19
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.google.firebase:firebase-analytics:17.2.1'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.google.android.gms:play-services-auth:17.0.0'
implementation 'com.google.firebase:firebase-auth:19.2.0'
implementation 'com.google.firebase:firebase-database:19.2.0'
implementation 'com.firebaseui:firebase-ui:0.6.2'
implementation 'com.github.lguipeng:BubbleView:1.0.1'
//implementation 'com.google.firebase:firebase-firestore:21.3.1'
}
apply plugin: 'com.google.gms.google-services'
错误
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.chat.mychatapp, PID: 18455
java.lang.NoSuchMethodError: No virtual method fetchProvidersForEmail(Ljava/lang/String;)Lcom/google/android/gms/tasks/Task; in class Lcom/google/firebase/auth/FirebaseAuth; or its super classes (declaration of 'com.google.firebase.auth.FirebaseAuth' appears in /data/app/com.chat.mychatapp-s_3u6mmiv0A6KE9ijznfqQ==/base.apk)
at com.firebase.ui.auth.ui.AcquireEmailHelper.checkAccountExists(AcquireEmailHelper.java:55)
at com.firebase.ui.auth.ui.email.SignInNoPasswordActivity.onClick(SignInNoPasswordActivity.java:73)
at android.view.View.performClick(View.java:7339)
at android.widget.TextView.performClick(TextView.java:14222)
at android.view.View.performClickInternal(View.java:7305)
at android.view.View.access$3200(View.java:846)
at android.view.View$PerformClick.run(View.java:27787)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7078)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)
答案 0 :(得分:1)
更新以下依赖项:
implementation 'com.firebaseui:firebase-ui:0.6.2'
对此:
implementation 'com.firebaseui:firebase-ui:6.2.0'
https://github.com/firebase/FirebaseUI-Android
来自docs:
从FirebaseAuth类以及相关的ProviderQueryResult类中删除了不推荐使用的
fetchProvidersForEmail(String)
方法。请改用fetchSignInMethodsForEmail(String)
。
因此,请更新firebaseUI
,而可以使用fetchSignInMethodsForEmail(String)
。