【发布时间】:2017-10-23 20:04:12
【问题描述】:
正如标题所说,我在尝试使用 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 并将 minifyEnable 设置为 false in build.gradle in debug{};重新启动android studio,因为我读过其他人的帖子,他们也遇到了这个问题,说这对他们有用;在我使用tableUser = database.getReference("User");的在线我也尝试使用tableUser = database.getReference().child("User");
非常感谢任何帮助。非常感谢
稍后编辑:添加登录活动 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 图像:
【问题讨论】:
-
如果您忽略来自调试器的“无此类实例字段”消息,您的代码是否正常工作?有什么例外吗?
-
我不能确定它是否会起作用,因为我不知道之后会发生什么。问题是在“btnLogIn.setOnClickListener(new View.OnClickListener() {”中甚至不会进入这个函数(我试过调试)并给我“没有这样的实例字段”
-
能否添加Logcat。
-
您的意思是登录活动 xml?我发布的是 LogIn 类。
-
没有 Logcat 是您看到错误的地方。它在 Android Studio 的左下角
标签: android database firebase firebase-realtime-database instance