使用Firebase登录后,应用程序崩溃

时间:2018-03-12 01:38:06

标签: android firebase firebase-authentication

我尝试在登录后能够在我的应用中看到哪些用户在线。 在Android Studio中没有标记错误,但我的应用程序在尝试获取哪些用户在线状态时仍然崩溃。 调试器显示以下错误:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.luisalonso.trashdriver, PID: 17271
                  com.google.firebase.database.DatabaseException: Class com.firebase.ui.auth.ui.User is missing a constructor with no arguments
                      at com.google.android.gms.internal.zzbtg$zza.zze(Unknown Source)
                      at com.google.android.gms.internal.zzbtg$zza.zzaH(Unknown Source)
                      at com.google.android.gms.internal.zzbtg.zze(Unknown Source)
                      at com.google.android.gms.internal.zzbtg.zzb(Unknown Source)
                      at com.google.android.gms.internal.zzbtg.zza(Unknown Source)
                      at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
                      at com.firebase.ui.database.FirebaseRecyclerAdapter.parseSnapshot(FirebaseRecyclerAdapter.java:151)
                      at com.firebase.ui.database.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:140)
                      at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:183)
                      at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6482)
                      at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6515)
                      at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5458)
                      at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5724)
                      at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5563)
                      at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5559)
                      at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2229)
                      at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1556)
                      at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1516)
                      at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:608)
                      at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3693)
                      at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3410)
                      at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1710)
                      at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:346)
                      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
                      at android.view.Choreographer.doCallbacks(Choreographer.java:686)
                      at android.view.Choreographer.doFrame(Choreographer.java:619)
                      at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
                      at android.os.Handler.handleCallback(Handler.java:739)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:148)
                      at android.app.ActivityThread.main(ActivityThread.java:7325)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at 

com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

这是我的ListOnline.java类:

public class ListOnline extends AppCompatActivity {
    //firebase
    DatabaseReference onlineRef, currentUserRef, counterRef;
    FirebaseRecyclerAdapter<User,ListOnlineViewHolder> adapter;

    //View
    RecyclerView listOnline;
    RecyclerView.LayoutManager layoutManager;


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

        //InitView
        listOnline = (RecyclerView)findViewById(R.id.listOnline);
        listOnline.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(this);
        listOnline.setLayoutManager(layoutManager);

        //Set toolbar and logout/join main_menu
        Toolbar toolbar = (Toolbar)findViewById(R.id.toolBar);
        toolbar.setTitle("Camiones en servicio");
        setSupportActionBar(toolbar);

        //Firebase
        onlineRef = FirebaseDatabase.getInstance().getReference().child(".info/connected");
        counterRef = FirebaseDatabase.getInstance().getReference("lastOnline");
        currentUserRef= FirebaseDatabase.getInstance().getReference("lastOnline")
                .child(FirebaseAuth.getInstance().getCurrentUser().getUid());//Create new child in lastOnline with key is uid
        setupSystem();

        updateList();








    }

    private void updateList() {
        adapter= new FirebaseRecyclerAdapter<User, ListOnlineViewHolder>(
                User.class,
                R.layout.user_layout,
                ListOnlineViewHolder.class,
                counterRef
        ) {
            @SuppressLint("RestrictedApi")
            @Override
            protected void populateViewHolder(ListOnlineViewHolder viewHolder, User model, int position) {
                viewHolder.txtEmail.setText(model.getName());

            }

        };
        adapter.notifyDataSetChanged();
        listOnline.setAdapter(adapter);
    }

    private void setupSystem() {
        onlineRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if(dataSnapshot.getValue(Boolean.class))
                {
                    currentUserRef.onDisconnect().removeValue();// Delete old value
                    counterRef.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
                            .setValue(new com.example.luisalonso.trashdriver.User(FirebaseAuth.getInstance().getCurrentUser().getDisplayName(),"Online"));
                    adapter.notifyDataSetChanged();




                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
        counterRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for(DataSnapshot postSnapshot:dataSnapshot.getChildren())
                {
                    com.example.luisalonso.trashdriver.User user = postSnapshot.getValue(com.example.luisalonso.trashdriver.User.class);
                    Log.d("LOG",""+user.getEmail() +"is "+user.getStatus());
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater =getMenuInflater();
        inflater.inflate(R.menu.main_menu,menu);
    return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId())
        {

            case R.id.action_join:
                counterRef.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
                        .setValue(new com.example.luisalonso.trashdriver.User(FirebaseAuth.getInstance().getCurrentUser().getEmail(),"Online"));
                break;
            case R.id.action_logout:
                currentUserRef.removeValue();// Delete old value
                break;
        }

        return super.onOptionsItemSelected(item);
    }
}

1 个答案:

答案 0 :(得分:0)

错误消息是:

Class com.firebase.ui.auth.ui.User is missing a constructor with no arguments

您显然已导入com.firebase.ui.auth.ui.User作为User,您在通用类型FirebaseRecyclerAdapter<User,ListOnlineViewHolder> adapter中使用了hungry = None while hungry not in (True, False): hungry = input('Hey, are you hungry?') if hungry in ('yes', 'ye', 'y', 'yeah'): print('oh you hungry huh') hungry = True elif hungry in ('no', 'n', 'nah', 'nope'): print('no food for you then') hungry = False else: print('its a simple yes or no question pls') 。这不是对Firebase-UI中User类的有效使用。也许你想要使用其他一些User类而不是Firebase-UI中的那个。