从firebase数据库获取数据时显示“进度”对话框

时间:2017-07-19 16:25:36

标签: android firebase firebase-realtime-database progressdialog

我想活动必须在从Firebase数据库中获取数据时显示进度对话框。它没有显示任何内容并且崩溃了。这是我的代码

public class profilemain扩展了AppCompatActivity {

private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;

 private ProgressDialog progressDialog;

DatabaseReference mref = 
FirebaseDatabase.getInstance().getReference("users");
ListView mlistview;
ArrayList<String> arrayList=new ArrayList<>();
ArrayAdapter<String> arrayAdapter;

private BottomNavigationView.OnNavigationItemSelectedListener 
 mOnNavigationItemSelectedListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.navigation_home:
progressDialog.show();
                startActivity(new 
Intent(profilemain.this,profilemain.class));
                return true;
            case R.id.navigation_dashboard:
                Toast.makeText(profilemain.this,"hi 
hello",Toast.LENGTH_SHORT).show();
                return true;
            case R.id.navigation_notifications:
                Toast.makeText(profilemain.this,"hi 
hello",Toast.LENGTH_SHORT).show();
                return true;
            case R.id.navigation_signout:

                Toast.makeText(profilemain.this,"You have successfully 
Signed out",Toast.LENGTH_SHORT).show();
                mAuth.signOut();
                return true;
        }
        return false;
    }

};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profilemain);
   progressDialog.setMessage("loading");
    progressDialog.setTitle("database is");

    mlistview=(ListView) findViewById(R.id.listview);

    Firebase.setAndroidContext(this);

    DatabaseReference ref = FirebaseDatabase.getInstance()
            .getReference("users");
    //mref=new Firebase("https://stark-1dffd.firebaseio.com/users");
    arrayAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arrayList);
    mlistview.setAdapter(arrayAdapter);


    mref.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            String value= dataSnapshot.getValue(String.class);

            arrayList.add(value);
            arrayAdapter.notifyDataSetChanged();
            //

            //

        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });


    BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    mAuth = FirebaseAuth.getInstance();


    mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            if (firebaseAuth.getCurrentUser()==null){
                startActivity(new
                        Intent(profilemain.this,MainActivity.class));

            }
        }
    };



}

@Override
protected void onStart() {
    super.onStart();

    mAuth.addAuthStateListener(mAuthListener);
}

      Process: com.food.sheenishere.stark, PID: 16408

com.google.firebase.database.DatabaseException: Failed to convert value of type 
java.util.HashMap to String

at com.google.android.gms.internal.zg.zzb(Unknown Source)

at com.google.android.gms.internal.zg.zza(Unknown Source)

at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)

at com.food.sheenishere.stark.home$1.onChildAdded(home.java:49)

at com.google.android.gms.internal.px.zza(Unknown Source)

at com.google.android.gms.internal.vj.zzHX(Unknown Source)

   at com.google.android.gms.internal.vp.run(Unknown Source)

at android.os.Handler.handleCallback(Handler.java:751)

at android.os.Handler.dispatchMessage(Handler.java:95)

at android.os.Looper.loop(Looper.java:154)

at android.app.ActivityThread.main(ActivityThread.java:6119)

at java.lang.reflect.Method.invoke(Native Method)

   at 

com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886)

    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
07-15 20:49:51.645 16408-16435/com.food.sheenishere.stark W/DynamiteModule: 
Local module descriptor class for com.google.firebase.auth not found.

2 个答案:

答案 0 :(得分:2)

你应该初始化进度对话框..比如

 private ProgressDialog pDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    pDialog = new ProgressDialog(profilemain.this);
    pDialog.setMessage("loading..");
}

答案 1 :(得分:1)

代码中的问题是你试图在{{1}上使用show(),setMessage(&#34; loading&#34;)和setTitle(&#34;数据库是&#34;)方法从未初始化的对象。

为了解决您的问题,您需要使用以下代码:

ProgressDialog

它肯定会解决您的问题。