任何人都可以帮助诊断此问题吗?我是android studio的新手,正在尝试一个新项目,该项目需要用户输入并将其粘贴到Firebase数据库中。问题是,直到我引入了Firebase Store为止,一切都运转良好。现在,当我输入用户数据而不附加图像时,该应用程序将崩溃。错误显示Attempt to invoke virtual method 'java.lang.String com.google.firebase.storage.StorageReference.toString()' on a null object reference
public class Signup extends AppCompatActivity {
private EditText inputEmail, inputPassword, confirmPassword;
private FirebaseAuth mAuth;
private Button btnRegister;
private EditText nameEt;
private EditText phoneNumberEt;
private EditText userNameEt;
private LinearLayout uploadPhoto;
DatabaseReference databaseReference;
//FirebaseStorage mStorageRef;
StorageReference mStorageRef;
FirebaseDatabase database;
ImageView profilephoto;
private ProgressDialog PD;
private FirebaseAuth.AuthStateListener mAuthListener;
private String name, userName, phoneNumber;
public static final int REQUEST_CODE = 1234;
Uri imgUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_signup);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setTitle("Sign Up");
mAuth = FirebaseAuth.getInstance();
PD = new ProgressDialog(Signup.this);
database = FirebaseDatabase.getInstance();
databaseReference = database.getReference("User").child("Clients");
inputEmail = (EditText) findViewById(R.id.inputEmail);
inputPassword = (EditText) findViewById(R.id.inputPassword);
confirmPassword = (EditText) findViewById(R.id.confirmPassword);
btnRegister = (Button) findViewById(R.id.register);
nameEt = (EditText) findViewById(R.id.nameEt);
phoneNumberEt = (EditText) findViewById(R.id.phoneNumberEt);
userNameEt = (EditText) findViewById(R.id.userNameEt);
uploadPhoto = (LinearLayout) findViewById(R.id.uploadPhoto);
profilephoto = (ImageView) findViewById(R.id.profilephoto);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if ((requestCode == REQUEST_CODE) && resultCode == RESULT_OK && data != null && data.getData() != null) {
imgUri = data.getData();
profilephoto.setImageURI(imgUri);
mStorageRef = FirebaseStorage.getInstance().getReference().child("images/" + System.currentTimeMillis() + " " + getImageExt(imgUri)).child(imgUri.getLastPathSegment());
try {Bitmap bm = MediaStore.Images.Media.getBitmap(getContentResolver(), imgUri);
ImageView profilephoto = (ImageView) findViewById(R.id.profilephoto);
profilephoto.setImageBitmap(bm);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public String getImageExt(Uri uri) {
ContentResolver contentResolver = getContentResolver();
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
return mimeTypeMap.getExtensionFromMimeType(contentResolver.getType(uri));
}
public void btnUpload_Click(View view) {
final String name = nameEt.getText().toString().trim();
final String password = inputPassword.getText().toString().trim();
final String email = inputEmail.getText().toString().trim();
final String cnfirmpassword =confirmPassword.getText().toString().trim();
final String phoneNumber = phoneNumberEt.getText().toString().trim();
final String imageaddress = mStorageRef.toString();
if (imageaddress!=null &&!name.isEmpty() && !password.isEmpty() && !email.isEmpty() && !cnfirmpassword.isEmpty() && !phoneNumber.isEmpty()) {
PD.setMessage("Signing up...");
PD.show();
mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
String user_id = mAuth.getCurrentUser().getUid();
DatabaseReference user_Details = databaseReference.child(user_id);
user_Details.child("name").setValue(name);
user_Details.child("email").setValue(email); user_Details.child("phonenumber").setValue(phoneNumber); user_Details.child("imageAddress").setValue(imageaddress);
mStorageRef.putFile(imgUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(getApplicationContext(), "Image uploaded", Toast.LENGTH_LONG).show();
PD.dismiss();
Intent intent = new Intent(Signup.this, Login.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) { Toast.makeText(getApplicationContext(), "Upload failed: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
})
.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>(){
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
PD.show();
}});
} else if (!task.isSuccessful()) {
Toast.makeText(getApplicationContext(), "User already exist", Toast.LENGTH_LONG).show();
PD.dismiss();
}
}
});
}
else if (name.isEmpty()) {
// If sign in fails, display a message to the user.
Toast.makeText(Signup.this, "Please Enter Name", Toast.LENGTH_SHORT).show();
} else if (password.length() < 6) {
Toast.makeText(Signup.this, "Please Enter Password", Toast.LENGTH_SHORT).show();
} else if (!password.equals(cnfirmpassword)) {
Toast.makeText(Signup.this, "Password do not match", Toast.LENGTH_SHORT).show();
} else if (phoneNumber.isEmpty()) {
Toast.makeText(Signup.this, "Please enter phone number", Toast.LENGTH_SHORT).show();
} else if (cnfirmpassword.isEmpty()) {
Toast.makeText(Signup.this, "Please confirm password", Toast.LENGTH_SHORT).show();
}}
public void onClick1 (View view) {
Intent intent = new Intent();
intent.setType("image/*");enter code here
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select image"), REQUEST_CODE);
}
}
谢谢。