正如标题所说,我在尝试使用Firebase数据库时遇到了这个错误。我尝试过在网上找到的众多解决方案,但似乎没有什么对我有用。下面你可以找到我的代码,还有一个简短的信息,关于我在发布到stackOverflow之前到目前为止所尝试的内容。
代码如下:
package com.example.vlad.restaurantorder;
import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.vlad.restaurantorder.Model.User;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.rengwuxian.materialedittext.MaterialEditText;
public class LogIn extends AppCompatActivity {
EditText editPhone, editPassword;
Button btnLogIn;
FirebaseDatabase database;
DatabaseReference tableUser;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_log_in);
btnLogIn = (Button)findViewById(R.id.btnLogInScreenLogIn);
editPhone = (MaterialEditText)findViewById(R.id.edtPhone);
editPassword = (MaterialEditText)findViewById(R.id.edtPassword);
//database
database = FirebaseDatabase.getInstance();
tableUser = database.getReference("User");
btnLogIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final ProgressDialog progressDialog = new ProgressDialog(LogIn.this);
progressDialog.setMessage("Please wait...");
progressDialog.show();
tableUser.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.child(editPhone.getText().toString()).exists()){
progressDialog.dismiss();
User user = dataSnapshot.child(editPhone.getText().toString()).getValue(User.class);
if(user.getPassword().equals(editPassword.getText().toString())){
Toast.makeText(getApplicationContext(), "You are logged in!", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getApplicationContext(), "Log in failed!", Toast.LENGTH_SHORT).show();
}
}
else {
progressDialog.dismiss();
Toast.makeText(getApplicationContext(), "User does not exists!", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
}
}
到目前为止我在此处发布的内容:禁用proGuard并在debug {}中的build.gradle中将minifyEnable置为false;重新启动android studio,因为我读过其他人也发现了遇到问题的帖子说这对他们有用;在我使用tableUser = database.getReference("User");
的位置,我也尝试使用tableUser = database.getReference().child("User");
非常感谢任何帮助。非常感谢你
稍后编辑:添加LogIn活动xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="@drawable/login_screen_image"
tools:context="com.example.vlad.restaurantorder.LogIn">
<LinearLayout
android:orientation="vertical"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.rengwuxian.materialedittext.MaterialEditText
android:id="@+id/edtPhone"
android:hint="Phone Number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="@android:color/white"
android:text="0293292442"
android:textColor="@android:color/white"
android:textSize="34sp"
android:inputType="phone"
app:met_floatingLabel="highlight"
app:met_baseColor="@android:color/white"
app:met_maxCharacters="11"
app:met_primaryColor="@android:color/white"
app:met_singleLineEllipsis="true"
/>
<com.rengwuxian.materialedittext.MaterialEditText
android:id="@+id/edtPassword"
android:hint="Password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="@android:color/white"
android:text="5678"
android:textColor="@android:color/white"
android:textSize="34sp"
android:inputType="textPassword"
app:met_floatingLabel="highlight"
app:met_baseColor="@android:color/white"
app:met_maxCharacters="11"
app:met_primaryColor="@android:color/white"
app:met_singleLineEllipsis="true"
/>
</LinearLayout>
<info.hoang8f.widget.FButton
android:id="@+id/btnLogInScreenLogIn"
android:text="@string/LogIn"
android:textColor="@android:color/white"
android:layout_marginRight="8dp"
android:layout_marginLeft="8dp"
android:layout_alignParentBottom="true"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
app:buttonColor="@color/btnLogIn"
app:shadowColor="@android:color/black"
app:shadowEnabled="true"
app:shadowHeight="5dp"
app:cornerRadius="4dp"
/>
稍后编辑:LogCat图片:
答案 0 :(得分:1)
如果没有其他警告/错误显示,您的代码似乎应该正常工作。丢失的实例错误/消息可能只是因为从Firebase读取数据需要一些时间。
添加ValueEventListener
后,该应用需要一些时间来“收听”或从Firebase中读取数据。因此,如下所示向每个if/else
块添加一些日志消息,重新编译并启动应用程序,在Android Studio中打开Logcat视图,然后单击应用程序屏幕上的按钮btnLogIn
。然后等待(确保设备/模拟器可以访问互联网并连接Firebase数据库)。
在Logcat窗口中,您应该看到日志消息显示,具体取决于onDataChange中输入的if/else
块。例如..... D/ANY_TAG: entered onDataChange
带调试行的代码:
tableUser.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d("ANY_TAG", "entered onDataChange");
if(dataSnapshot.child(editPhone.getText().toString()).exists()){
Log.d("ANY_TAG", "entered onDataChange/childExists");
progressDialog.dismiss();
User user = dataSnapshot.child(editPhone.getText().toString()).getValue(User.class);
if(user.getPassword().equals(editPassword.getText().toString())){
Log.d("ANY_TAG", "entered onDataChange/childExists/equalPassword");
Toast.makeText(getApplicationContext(), "You are logged in!", Toast.LENGTH_SHORT).show();
}
else{
Log.d("ANY_TAG", "entered onDataChange/childExists/unEqualPasswords");
Toast.makeText(getApplicationContext(), "Log in failed!", Toast.LENGTH_SHORT).show();
}
}
else {
Log.d("ANY_TAG", "entered onDataChange/childDoesNotExist");
progressDialog.dismiss();
Toast.makeText(getApplicationContext(), "User does not exists!", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}