![](/img/trans.png)
[英]Flutter Firebase Works On Real Device Not On Android Emulator
[英]Firebase Auth gives ViewPostIme pointer 1 While running on a physical device but work on Android Emulator ( Firebase works only on emulator )
更新:任何使用 firebase 的項目都只能在模擬器上運行,我試過了。
該應用程序僅在模擬器中按預期運行
當我在物理設備上測試應用程序時,我得到以下信息。 [LogCat output 截圖][1]
這是 LogCat output evertime 我點擊我的登錄按鈕
這是我的MainActivity.java文件
package com.abg.logintest;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.android.material.textfield.TextInputEditText;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private FirebaseAuth mAuth;
TextInputEditText email,pass;
String emailText,password;
Button login;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAuth = FirebaseAuth.getInstance();
email = findViewById(R.id.email);
pass = findViewById(R.id.pass);
login = findViewById(R.id.button);
login.setOnClickListener(this);
}
@Override
public void onStart() {
super.onStart();
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser currentUser = mAuth.getCurrentUser();
updateUI(currentUser);
}
private void loginUser(String email, String password){
mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d("TAG", "signInWithEmail:success");
FirebaseUser user = mAuth.getCurrentUser();
updateUI(user);
} else {
// If sign in fails, display a message to the user.
Log.w("TAG", "signInWithEmail:failure", task.getException());
Toast.makeText(MainActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
updateUI(null);
}
// ...
}
});
}
private void updateUI(FirebaseUser currentUser) {
if(currentUser!=null)
Log.d("login",currentUser.getEmail());
}
@Override
public void onClick(View v) {
emailText = email.getText().toString();
password = pass.getText().toString();
loginUser(emailText,password);
}
}
這是主要活動 XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="email"
android:padding="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.265">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/email">
</com.google.android.material.textfield.TextInputEditText>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="password"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/pass"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</com.google.android.material.textfield.TextInputEditText>
</com.google.android.material.textfield.TextInputLayout>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textInputLayout" />
</androidx.constraintlayout.widget.ConstraintLayout>
我已經包含了運行 firebase 所需的所有依賴項
[我的模塊級依賴:APP][2]
[我在清單文件中提到的權限][3]
任何幫助將不勝感激,謝謝。 [1]: https://i.stack.imgur.com/u8XY8.png [2]: https://i.stack.imgur.com/lIm6a.png [3]: https://i.stack. imgur.com/5kBVy.png
所以在翻遍了整個代碼后,我發現“new OnCompleteListener()”是灰色的,IDE 建議“Anonymous new OnCompleteListener() can be replaced with lambda”,
所以我用 lambda 代替了它
令人驚訝的是,該應用程序現在也可以在實際設備上運行。
在 LAMBDA 之前
mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {....
mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(this, task -> {...
盡管我仍然對為什么在一個項目中“new onCompleteListener...”沒有變灰並且工作正常感到困惑,但在另一個項目中它需要用 lambda function 替換。
現在的問題是,為什么無論 lambda 函數是否存在,該應用程序在模擬器上都能正常運行,並且它需要 lambda 才能在實際設備上運行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.