android – 从firebase数据库中获取数据时显示进度对话框

我希望活动必须在从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.

解决方法:

您的代码中的问题是您正在尝试在从未初始化的ProgressDialog对象上使用show(),setMessage(“loading”)和setTitle(“database is”)方法.

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

ProgressDialog progressDialog = new ProgressDialog(this);

它肯定会解决你的问题.

上一篇:在android中创建带有进度条的对话框


下一篇:android – AsyncTask或Handler-哪一个更适合耗时的网络交互和处理?