【问题标题】:Firebase "no such instance field" errorFirebase“没有这样的实例字段”错误
【发布时间】: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


【解决方案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) {

                }
            });
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多