(我知道有很多这样的问题,但是没有人帮助过我。) 我花了将近3天的时间来解决这个问题,并尝试了全部方法来解决此错误,但没有人无法解决。
让我告诉您我对这个错误的理解:您想将一个空值放在孩子身上。
但是当我控制它们时,没有人为空/空。
此错误的原因可能是另一个Java页中的子级吗? (例如FeedActivityTwo.java)
代码:
public class SignUpActivity extends AppCompatActivity {
StorageReference mStorageRef2;
FirebaseAuth mAuthNew;
ArrayAdapter arrayAdapterUniNew,adapterForGender;
EditText userNameNew,profileNameNew,eMailNew,passwordNew;
Spinner universitySpinner,genderSpinner;
Uri selectedImage2;
ImageView UploadImage2;
String genderStatus,universityName;
ArrayList< String> allUserNamesFromDataBase;
String profilename2,userName2,eMail2;
Button button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate ( savedInstanceState );
setContentView ( R.layout.activity_sign_up );
FirebaseAuth.getInstance ().signOut ();
mStorageRef2 = FirebaseStorage.getInstance ().getReference ();
mAuthNew = FirebaseAuth.getInstance ();
UploadImage2 = findViewById ( R.id.userImageView2 );
userNameNew = findViewById ( R.id.nameSurnameEditText );
profileNameNew = findViewById ( R.id.eitTextProfileName );
passwordNew = findViewById ( R.id.editTextPassWordSignUp );
eMailNew = findViewById ( R.id.editTextEmailSignUp );
universitySpinner = findViewById ( R.id.universitesSpinner );
genderSpinner = findViewById ( R.id.genderSpinner );
button2 = findViewById ( R.id.button2 );
allUserNamesFromDataBase = new ArrayList <> ();
arrayAdapterUniNew = ArrayAdapter.createFromResource ( this,R.array.universites,android.R.layout.simple_spinner_dropdown_item );
universitySpinner.setAdapter ( arrayAdapterUniNew );
adapterForGender = ArrayAdapter.createFromResource ( this,R.array.genderSpinner,android.R.layout.simple_spinner_dropdown_item );
genderSpinner.setAdapter ( adapterForGender );
genderSpinner.setOnItemSelectedListener ( new AdapterView.OnItemSelectedListener ( ) {
@Override
public void onItemSelected(AdapterView <?> adapterView, View view, int i, long l) {
if (i==0){
genderStatus="";
}else{
genderStatus = adapterView.getSelectedItem ( ).toString ( );
}
}
@Override
public void onNothingSelected(AdapterView <?> adapterView) {
}
} );
universitySpinner.setOnItemSelectedListener ( new AdapterView.OnItemSelectedListener ( ) {
@Override
public void onItemSelected(AdapterView <?> adapterView2, View view, int i, long l) {
if (i==0){
universityName="";
}else{
universityName = adapterView2.getSelectedItem ().toString ();
}
}
@Override
public void onNothingSelected(AdapterView <?> adapterView) {
Toast.makeText ( getApplicationContext (),"Üniversite Seçin",Toast.LENGTH_SHORT ).show ();
}
} );
button2.setOnClickListener ( new View.OnClickListener ( ) {
@Override
public void onClick(View view) {
profilename2 = profileNameNew.getText ( ).toString ( );
eMail2 = eMailNew.getText ( ).toString ( );
userName2 = userNameNew.getText ( ).toString ( );
if (!eMailNew.getText ( ).toString ( ).isEmpty ( ) && !userNameNew.getText ( ).toString ( )
.isEmpty ( ) && !profileNameNew.getText ( ).toString ( ).isEmpty ( ) && !passwordNew.getText ( ).toString ( )
.isEmpty ( ) && !universityName.isEmpty ( ) && !genderStatus.isEmpty ( )) {
FirebaseDatabase database55 = FirebaseDatabase.getInstance ();
DatabaseReference refNew= database55.getReference ("Users");
refNew.addListenerForSingleValueEvent ( new ValueEventListener ( ) {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren ( )) {
HashMap <String, String> hashMap = (HashMap <String, String>) ds.getValue ( );
allUserNamesFromDataBase.add ( hashMap.get ( "profileNameOfUser" ) );
//Bütün kullanıcı adlarını alıp arrayList'e atıyor
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText ( getApplicationContext ( ), databaseError.getMessage ( ), Toast.LENGTH_SHORT ).show ( );
}
} );
boolean ans = allUserNamesFromDataBase.contains ( profilename2 );
if (ans){
Toast.makeText ( getApplicationContext ( ), "Kullanıcı adı daha önceden alınmış", Toast.LENGTH_SHORT ).show ( );
}else {
mAuthNew.createUserWithEmailAndPassword ( eMailNew.getText ().toString (),passwordNew.getText ().toString () ).addOnCompleteListener ( new OnCompleteListener <AuthResult> ( ) {
@Override
public void onComplete(@NonNull Task <AuthResult> task) {
if (task.isSuccessful ()) {
uploaddata();
}
}
} ).addOnFailureListener ( new OnFailureListener ( ) {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText ( getApplicationContext (),e.getLocalizedMessage (),Toast.LENGTH_SHORT ).show ();
}
} );
}
}
}
} );
}
private void uploaddata() {
Log.d ( "Value",profilename2 );
Log.d ( "Value",userName2 );
Log.d ( "Value",eMail2 );
Log.d ( "Value",genderStatus );
Log.d ( "Value",universityName );
Log.d ( "Value",mAuthNew.getCurrentUser ().getUid () );
FirebaseDatabase myBase = FirebaseDatabase.getInstance ();
DatabaseReference myRef = myBase.getReference ("Users");
myRef.child ( profilename2 ).child ( "profileNameOfUser" ).setValue ( profilename2 );
myRef.child ( profilename2 ).child ( "nameSurname" ).setValue ( userName2 );
myRef.child ( profilename2 ).child ( "eMail" ).setValue ( eMail2 );
myRef.child ( profilename2 ).child ( "gender" ).setValue ( genderStatus );
myRef.child ( profilename2 ).child ( "uniName" ).setValue ( universityName );
myRef.child ( profilename2 ).child ( "profileUid" ).setValue ( mAuthNew.getCurrentUser ().getUid () );
Toast.makeText ( getApplicationContext (),"task başarılı",Toast.LENGTH_SHORT ).show ();
if (selectedImage2 == null){
withoutProfilePicture();
}else{
withProfilePicture();
}
}
private void withoutProfilePicture() {
FirebaseDatabase myBase = FirebaseDatabase.getInstance ();
final DatabaseReference myRef = myBase.getReference ("Users");
UserProfileChangeRequest updateUserProfile = new UserProfileChangeRequest.Builder ()
.setPhotoUri ( Uri.parse ( "https://firebasestorage.googleapis.com/v0/b/sametdeneme1-a2a6e.appspot.com/o/images%2Fdefaultprofilepicture.png?alt=media&token=1f17a684-c740-44a2-87c5-47f3b7dd1211" ) )
.setDisplayName ( profileNameNew.getText ().toString () ).build ();
mAuthNew.getCurrentUser ().updateProfile ( updateUserProfile ).addOnCompleteListener ( new OnCompleteListener <Void> ( ) {
@Override
public void onComplete(@NonNull Task <Void> task) {
if (task.isSuccessful ()){
myRef.child ( profilename2 ).child ( "pictureURL" ).setValue ( "https://firebasestorage.googleapis.com/v0/b/sametdeneme1-a2a6e.appspot.com/o/images%2Fdefaultprofilepicture.png?alt=media&token=1f17a684-c740-44a2-87c5-47f3b7dd1211" );
Toast.makeText ( getApplicationContext ( ), "Fotosuz kullanıcı oluşturuldu!", Toast.LENGTH_SHORT ).show ( );
Intent newIntent35 = new Intent ( getApplicationContext (),FeedActivityTwo.class );
startActivity ( newIntent35 );
}
}
} ).addOnFailureListener ( new OnFailureListener ( ) {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText ( getApplicationContext (),"Fotosuz kullanıcı oluşturmada hata + "+e.getLocalizedMessage (),Toast.LENGTH_SHORT ).show ();
}
} );
}
public void withProfilePicture(){
FirebaseDatabase myBase = FirebaseDatabase.getInstance ();
final DatabaseReference myRef = myBase.getReference ("Users");
UUID uuid = UUID.randomUUID ( );
final String imageName = "images/" + uuid + ".jpg";
StorageReference storageReference = mStorageRef2.child ( imageName );
storageReference.putFile ( selectedImage2 ).addOnSuccessListener ( new OnSuccessListener <UploadTask.TaskSnapshot> ( ) {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
StorageReference newReference = FirebaseStorage.getInstance ( ).getReference ( imageName );
newReference.getDownloadUrl ( ).addOnSuccessListener ( new OnSuccessListener <Uri> ( ) {
@Override
public void onSuccess(Uri uri) {
String downloadURL = uri.toString ( );
myRef.child ( profilename2 ).child ( "pictureURL" ).setValue ( downloadURL );
final FirebaseUser userNow = FirebaseAuth.getInstance ().getCurrentUser ();
UserProfileChangeRequest updateProfile2 = new UserProfileChangeRequest.Builder ( )
.setPhotoUri ( Uri.parse ( downloadURL ) )
.setDisplayName ( profilename2 )
.build ( );
userNow.updateProfile ( updateProfile2 ).addOnCompleteListener ( new OnCompleteListener <Void> ( ) {
@Override
public void onComplete(@NonNull Task <Void> task) {
Intent newIntent35 = new Intent ( getApplicationContext (),FeedActivityTwo.class );
startActivity ( newIntent35 );
Toast.makeText ( getApplicationContext ( ), "Fotolu kullanıcı oluşturuldu!", Toast.LENGTH_SHORT ).show ( );
}
} );
}
} ).addOnFailureListener ( new OnFailureListener ( ) {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText ( getApplicationContext (),"Fotolu kullanıcı + " + e.getLocalizedMessage (),Toast.LENGTH_SHORT ).show ();
}
} );
}
} ).addOnFailureListener ( new OnFailureListener ( ) {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText ( getApplicationContext (),"Fotolu kullanıcı + " +e.getLocalizedMessage (),Toast.LENGTH_SHORT ).show ();
}
} );
}
public void setUserProfileImage (View view){
if (ContextCompat.checkSelfPermission ( this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ){
ActivityCompat.requestPermissions ( this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},3 );
} else {
Intent intent =new Intent ( Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI );
startActivityForResult ( intent,4 );
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode==3){
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
Intent intent =new Intent ( Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI );
startActivityForResult ( intent,4 );
}
}
super.onRequestPermissionsResult ( requestCode, permissions, grantResults );
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
try{
if (requestCode ==4 && resultCode == RESULT_OK && data != null){
selectedImage2 =data.getData ();
Bitmap bitmap = MediaStore.Images.Media.getBitmap ( this.getContentResolver (),selectedImage2 );
UploadImage2.setImageBitmap ( bitmap );}
}catch (IOException e){
Toast.makeText ( getApplicationContext (),e.getLocalizedMessage (),Toast.LENGTH_SHORT ).show ();
}
super.onActivityResult ( requestCode, resultCode, data );
}
}
错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: samet.ercetin.com.sametdeneme1, PID: 19655
java.lang.NullPointerException: Can't pass null for argument 'pathString' in child()
at com.google.firebase.database.DatabaseReference.child(com.google.firebase:firebase-database@@16.0.6:101)
at com.google.firebase.database.DataSnapshot.child(com.google.firebase:firebase-database@@16.0.6:68)
at samet.ercetin.com.sametdeneme1.FeedActivityTwo$1.onDataChange(FeedActivityTwo.java:111)
at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@16.0.6:75)
at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@16.0.6:63)
at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@16.0.6:55)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5551)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:733)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:623)
edit =有时可以正常工作而没有任何错误。