2017-10-23 16 views
0

Firebaseデータベースで作業しようとしたときに、そのエラーが発生しました。私はオンラインで見つかった多くのソリューションを試しましたが、何も私のために働いたようです。下に私のコードを見つけることができ、また、stackOverflowに投稿する前に私が試みたことに関する簡単な情報があります。Firebase「そのようなインスタンスフィールドがありません」エラー

コードは以下の通りです:私はここに投稿する前に、これまでに試してみました

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の無効化とデバッグにbuild.gradleで偽にminifyEnableを置きます{};アンドロイドスタジオを再起動すると、問題に遭遇した他の人の投稿を読んだので、これが彼らのために働いたと言いました。私が使用したラインで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の画像:

enter image description here

+0

、ワーキングあなたのコードですか?例外はありますか? –

+0

後で何が起こるのか分からないので、うまくいくかどうかはわかりません。問題は "btnLogIn.setOnClickListener(new View.OnClickListener(){"はこの関数の中に入力しなくても(私はデバッグしようとしました)、 "そのようなインスタンスフィールドはありません" – Vlad

+0

Logcatを追加できますか? –

答えて

1

あなたのコードだけで動作するはずと思われます他の警告/エラーが表示されない場合は問題ありません。欠落しているインスタンスのエラー/メッセージは、Firebaseからデータを読み取るのに時間がかかるためです。

ValueEventListenerを追加した後、アプリはFirebaseのデータを「聞く」または読み込むのに時間がかかります。したがって、以下のようにif/elseブロックにいくつかのログメッセージを追加し、再コンパイルして起動し、Androidスタジオで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) { 

       } 
      }); 
} 
関連する問題