我一直试图从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());
}
});
运行该应用程序并上传任何内容后,该应用程序崩溃,我必须再次启动该应用程序。在上传过程之后,我检查了Firebase数据库中是否有新上传的数据,但只有一半的数据被上传了。我什至尝试在上传和startActivity(intent)之间添加延迟,以便完整的数据被上传,但应用再次崩溃。我尝试从Firebase提取数据时显示错误。它说我有一个空值异常。即使是完整的数据也可以上传到Firebase。我必须再次启动应用程序,然后才能上传数据。
这是我的数据库结构: enter image description here
崩溃时的堆栈跟踪:
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);
答案 0 :(得分:1)
firebase setValue()方法是异步方法。因此,您的每个setValue()方法都将在后台线程中执行,并且无论是否在Firebase上发送或发送所有数据,都会调用您的启动活动代码。最好制作一个模型类并在firebase上发送完整模型。因此,所有项目都将一起上传到Firebase。如果您需要代码方面的帮助,请告诉我。