我一直收到错误
Process: com.attendanceapp.obare.attendanceapp, PID: 8494
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.contains(java.lang.CharSequence)' on a null object reference
at android.content.ContentResolver.checkLeakDetectIgnoreList(ContentResolver.java:2695)
at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1178)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:958)
at android.content.ContentResolver.openInputStream(ContentResolver.java:678)
at com.google.firebase.storage.UploadTask.<init>(Unknown Source)
at com.google.firebase.storage.StorageReference.putFile(Unknown Source)
at com.Healthcareapp.medicalapp.AddEmployeePackage.Add_employee$4.onClick(Add_employee.java:142)
at android.view.View.performClick(View.java:5274)
at android.view.View$PerformClick.run(View.java:21543)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5728)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
代码如下所示,addOnSuccessListener发生错误。任何帮助将不胜感激。一切都很好。
final StorageReference onlineStoragePhotoRef = mStorageReference.child(“WorkForce_Photos”)。child(FirstName).child(photoUri.getLastPathSegment());
progressBar.setMessage("Uploading file to Firebase");
progressBar.show();
final String finalFirstNameText = FirstName;
final String finalLastNameText = LastName;
final String finalIDnumText = IDnum;
final String finalContactText = Contactnum;
final String finaWorkBadgeText = WBadge;
onlineStoragePhotoRef.putFile(photoUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
progressBar.setMessage("Success");
photoUrl = taskSnapshot.getDownloadUrl().toString();
WorkForceData workForceData = new WorkForceData(finalFirstNameText, finalLastNameText, finalIDnumText, finalContactText, finaWorkBadgeText,photoUrl);
mDatabaseReference.child("WorkForce").push().setValue(workForceData);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(Add_employee.this, e.getMessage(), Toast.LENGTH_LONG).show();
photoUrl = "";
WorkForceData workForceData = new WorkForceData(finalFirstNameText, finalLastNameText, finalIDnumText, finalContactText, finaWorkBadgeText, photoUrl);
mDatabaseReference.child("Users").push().setValue(workForceData);
}
});
progressBar.dismiss();
finish();
}
});
}
答案 0 :(得分:2)
成功侦听器在上载完成后异步执行。这是在之后发生,你关闭进度条并用这些语句完成活动:
progressBar.dismiss();
finish();
重构您的代码,以便在上传完成时执行这些操作:
onlineStoragePhotoRef.putFile(photoUri)
.addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
if (task.isSuccessful()) {
progressBar.setMessage("Success");
photoUrl = task.getResult().getDownloadUrl().toString();
WorkForceData workForceData = new WorkForceData(finalFirstNameText, finalLastNameText, finalIDnumText, finalContactText, finaWorkBadgeText,photoUrl);
mDatabaseReference.child("WorkForce").push().setValue(workForceData);
} else {
Toast.makeText(Add_employee.this, task.getException().getMessage(), Toast.LENGTH_LONG).show();
photoUrl = "";
WorkForceData workForceData = new WorkForceData(finalFirstNameText, finalLastNameText, finalIDnumText, finalContactText, finaWorkBadgeText, photoUrl);
mDatabaseReference.child("Users").push().setValue(workForceData);
}
progressBar.dismiss();
finish();
}
});