当前位置: 首页 > 知识库问答 >
问题:

在android studio上将上传数据取到firebase实时数据库时,应用程序崩溃

长孙宜
2023-03-14

我一直试图从firebase实时数据库中获取一些上传的数据,但应用程序一直崩溃。

我将从“profile_tab”转到“project_creation_activity”,以便将数据上载到Firebase,处理数据上载的代码如下所示

btnUpload.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                final ProgressDialog progressDialog=new ProgressDialog(Project_Creation_Activity.this);
                progressDialog.setMessage("Updating Info..");
                progressDialog.show();

                FirebaseUser currentUser=mAuth.getCurrentUser();

                if(projectTitle.getText().toString().equals(""))
                {
                    Toasty.error(Project_Creation_Activity.this,"Project title missing",Toasty.LENGTH_SHORT,true).show();
                }
                else
                {
                    if(projectDescription.getText().toString().equals(""))
                    {
                        Toasty.error(Project_Creation_Activity.this,"Project description missing",Toasty.LENGTH_SHORT,true).show();
                    }
                    else
                    {
                        String skill= Arrays.toString(skills);


                        //Adding project data to user
                         mDatabase.child("users").child(uid).child("userProjects").child(projectTitle.getText().toString()).child("projectTitle").setValue(projectTitle.getText().toString());
                         mDatabase.child("users").child(uid).child("userProjects").child(projectTitle.getText().toString()).child("skills").setValue(skill);
                         mDatabase.child("users").child(uid).child("userProjects").child(projectTitle.getText().toString()).child("projectDescription").setValue(projectDescription.getText().toString());
                         mDatabase.child("users").child(uid).child("userProjects").child(projectTitle.getText().toString()).child("leaderUid").setValue(uid);

                        //Adding data to project
                         mDatabase.child("projects").child(projectTitle.getText().toString()).child("projectTitle").setValue(projectTitle.getText().toString());
                         mDatabase.child("projects").child(projectTitle.getText().toString()).child("skills").setValue(skill);
                         mDatabase.child("projects").child(projectTitle.getText().toString()).child("projectDescription").setValue(projectDescription.getText().toString());
                         mDatabase.child("projects").child(projectTitle.getText().toString()).child("leaderUid").setValue(uid);

                        Toasty.success(Project_Creation_Activity.this,"Uploaded",Toasty.LENGTH_SHORT,true).show();
                        Intent intent=new Intent(Project_Creation_Activity.this,Social_Home_Activity.class);
                        startActivity(intent);
                        finish();
                    }
                }
                progressDialog.dismiss();
            }
        });
mDatabase.child("users").child(currentUser.getUid()).child("userProjects").addChildEventListener(new ChildEventListener(){
            @Override
            public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s)
            {
                String title=(String)dataSnapshot.child("projectTitle").getValue();
                String description=(String)dataSnapshot.child("projectDescription").getValue();
                String skill=(String)dataSnapshot.child("skills").getValue();
                Log.i("Pratik",title);
                Log.i("Pratik",description);
                Log.i("Pratik",skill);
                Create_Project_Card(dataSnapshot.child("projectTitle").getValue().toString(),dataSnapshot.child("projectDescription").getValue().toString(),dataSnapshot.child("skills").getValue().toString(),view);
            }

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

            }

            @Override
            public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

            }

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

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError)
            {
                Log.i("Pratik",databaseError.getDetails());
            }
        });

2019-11-24 11:40:16.347 15445-15445/com.example.collaborator E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.collaborator, PID: 15445
    java.lang.NullPointerException: println needs a message
        at android.util.Log.println_native(Native Method)
        at android.util.Log.i(Log.java:179)
        at com.example.collaborator.Profile_Tab$4.onChildAdded(Profile_Tab.java:511)
        at com.google.firebase.database.core.ChildEventRegistration.fireEvent(com.google.firebase:firebase-database@@16.0.4:79)
        at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@16.0.4:63)
        at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@16.0.4:55)
        at android.os.Handler.handleCallback(Handler.java:874)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:198)
        at android.app.ActivityThread.main(ActivityThread.java:6729)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

行Profile_Tab行511是:log.i(“pratik”,description);

共有1个答案

赫连宏伯
2023-03-14

firebase setValue()方法是异步方法。因此,您的每个setValue()方法都将在后台线程中执行,并且无论所有数据是否在FireBase上发送,您的启动活动代码都将被调用。最好制作一个model类,在FireBase上发送完整的model。因此,以这种方式,所有的项目将一起上传在Firebase。如果您需要代码方面的帮助,请告诉我。

 类似资料: