我正在开发的应用程序在单击按钮时崩溃。该按钮应该将图片和文本上传到firebase
。这是我收到错误的代码:
public class ProfileUtils {
private FirebaseFirestore firebaseFirestore = FirebaseFirestore.getInstance();
private FirebaseAuth firebaseAuth;
private StorageReference storageReference;
private Map<String, String> userSetupMap;
private ProcessStatusObservable processStatusObservable;
private StorageReference imgPath;
private String userId;
public ProfileUtils(){
processStatusObservable = new ProcessStatusObservable();
userSetupMap = new HashMap<>();
firebaseAuth = FirebaseAuth.getInstance();
userId = firebaseAuth.getCurrentUser().getUid();
storageReference = FirebaseStorage.getInstance().getReference();
}
private void uploadPicture(String pictureType, Uri uri, StorageReference storageReference){
imgPath.putFile(uri).addOnCompleteListener(task -> { //I GET ERROR HERE//
if(task.isSuccessful()){
Uri imageUri = task.getResult().getDownloadUrl();
userSetupMap.put(pictureType.concat("PictureUrl"), imageUri.toString());
}
else{
}
});
}
public ProfileUtils setProfilePicture(Uri uri){
storageReference = storageReference.child("Profile pictures").child(userId+".jpg");
uploadPicture("profile", uri, storageReference);
return this;
}
public ProfileUtils setCoverPicture(Uri uri){
StorageReference coverPath = storageReference.child("Cover pictures").child(userId+".jpg");
uploadPicture("cover", uri, coverPath);
return this;
}
public ProfileUtils setUserName(String userName){
userSetupMap.put("name", userName);
return this;
}
public void commit(){
firebaseFirestore.collection("Users").document(userId)
.set(userSetupMap).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful()){
processStatusObservable.setStatus(ProcessStatus.NEW_USER_PROFILE_SETUP_SUCCESS);
}
else{
}
}
});
}
}
错误Logcat:
05-18 21:11:09.486 15873-15873/com.sachintitus.instafy.instafy D/AndroidRuntime: Shutting down VM
05-18 21:11:09.487 15873-15972/com.sachintitus.instafy.instafy W/common.FileUtils: Failed to set dir world executable: java.io.FileNotFoundException: /data/user_de/0/com.google.android.gms/files/gmscore_global_dirs.txt: open failed: EACCES (Permission denied)
05-18 21:11:09.488 15873-15873/com.sachintitus.instafy.instafy E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.sachintitus.instafy.instafy, PID: 15873
java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.storage.UploadTask com.google.firebase.storage.StorageReference.putFile(android.net.Uri)' on a null object reference
at com.sachintitus.instafy.instafy.repository.Model.Factories.FirebaseUtils.ProfileUtils.uploadPicture(ProfileUtils.java:46)
at com.sachintitus.instafy.instafy.repository.Model.Factories.FirebaseUtils.ProfileUtils.setProfilePicture(ProfileUtils.java:60)
at com.sachintitus.instafy.instafy.repository.Model.Factories.FireStore.setUpNewUser(FireStore.java:63)
at com.sachintitus.instafy.instafy.repository.Repository.setUpNewUser(Repository.java:58)
at com.sachintitus.instafy.instafy.viewmodel.BioSetupActivityViewModel.setupNewUser(BioSetupActivityViewModel.java:11)
at presenter.BioSetupActivityPresenter.setUpBasicProfile(BioSetupActivityPresenter.java:65)
at com.sachintitus.instafy.instafy.view.BioSetupActivity.lambda$onCreate$1$BioSetupActivity(BioSetupActivity.java:77)
at com.sachintitus.instafy.instafy.view.BioSetupActivity$$Lambda$1.onClick(Unknown Source:21)
at android.view.View.performClick(View.java:6294)
at android.view.View.onKeyUp(View.java:12470)
at android.widget.TextView.onKeyUp(TextView.java:7475)
at android.view.KeyEvent.dispatch(KeyEvent.java:2715)
at android.view.View.dispatchKeyEvent(View.java:11713)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1834)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1834)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1834)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1834)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1834)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1834)
at com.android.internal.policy.DecorView.superDispatchKeyEvent(DecorView.java:440)
at com.android.internal.policy.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1819)
at android.app.Activity.dispatchKeyEvent(Activity.java:3267)
at android.support.v7.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:534)
at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:58)
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.dispatchKeyEvent(AppCompatDelegateImplBase.java:316)
at com.android.internal.policy.DecorView.dispatchKeyEvent(DecorView.java:354)
at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4733)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4605)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4147)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4200)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4166)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4293)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4174)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4350)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4147)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4200)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4166)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4174)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4147)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4200)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4166)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4326)
at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:4487)
at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2435)
at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1998)
05-18 21:11:09.489 15873-15873/com.sachintitus.instafy.instafy E/AndroidRuntime: at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1989)
at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:2412)
at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:325)
at android.os.Looper.loop(Looper.java:142)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
答案 0 :(得分:1)
日志已经给你答案了。
很明显,你的imgPath是null,因为它从未被初始化。
java.lang.NullPointerException:尝试调用虚方法 “com.google.firebase.storage.UploadTask com.google.firebase.storage.StorageReference.putFile(android.net.Uri)” 在null对象引用 com.sachintitus.instafy.instafy.repository.Model.Factories.FirebaseUtils.ProfileUtils.uploadPicture(ProfileUtils.java:46)