Не удалось получить строку значения снимка экрана в firebase - PullRequest
0 голосов
/ 15 ноября 2018

Это структура моей базы данных Это код для получения имени пользователя из базы данных. (DataSnapshot) показывает, что аннотированный параметр не переопределяет параметр @NonNull Я добавил весь код для настроек ниже. Надеюсь, это поможет

public class SettingsActivity extends AppCompatActivity
{
    private CircleImageView settingsDisplayProfileImage;
    private TextView settingsDisplayname;
    private TextView settingsDisplayStatus;
    private TextView settingsDisplayaddress;
    private TextView settingsDisplayphone;
    private TextView settingsDisplayservice;
    private Button settingsChangeProfileImageButton;
    private Button settingsUpdateProfileButton;


    private final static int Gallery_Pick = 1;
    private StorageReference storeProfileImagestorageRef;


    private DatabaseReference getUserDataReference;
    private FirebaseAuth mAuth;

    Bitmap thumb_bitmap = null;

    private StorageReference thumbImageRef;
    private ProgressDialog loadingBar;
    private String downloadImageUrl;
    private String online_user_id;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        mAuth = FirebaseAuth.getInstance();
        online_user_id = mAuth.getCurrentUser().getUid();
        getUserDataReference = FirebaseDatabase.getInstance().getReference().child("Users").child(online_user_id);
        getUserDataReference.keepSynced(true);


        storeProfileImagestorageRef = FirebaseStorage.getInstance().getReference().child("Profile_Images");

        thumbImageRef = FirebaseStorage.getInstance().getReference().child("Thumb_Images");

        settingsDisplayProfileImage = (CircleImageView) findViewById(R.id.settings_profile_image);
        settingsDisplayname = (TextView) findViewById(R.id.settings_username);
        settingsDisplayaddress = (TextView) findViewById(R.id.settings_user_address);
        settingsDisplayphone = (TextView)findViewById(R.id.settings_user_phone);
        settingsDisplayservice = (TextView) findViewById(R.id.settings_user_service);
        settingsDisplayStatus = (TextView) findViewById(R.id.settings_user_status);
        settingsChangeProfileImageButton = (Button) findViewById(R.id.settings_change_profile_image_button);
        settingsUpdateProfileButton = (Button) findViewById(R.id.settings_update_profile);
        loadingBar= new ProgressDialog(this);

        getUserDataReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                if (dataSnapshot.exists())
                {
                //error occurs here first line below
                String name = dataSnapshot.child("user_name").getValue().toString();
                String status = dataSnapshot.child("user_status").getValue().toString();
                final String image = dataSnapshot.child("user_image").getValue().toString();
                String address = dataSnapshot.child("user_address").getValue().toString();
                String phone = dataSnapshot.child("user_phone").getValue().toString();
                String service = dataSnapshot.child("user_service").getValue().toString();
                String thumb_image = dataSnapshot.child("user_thumb_image").getValue().toString();


                settingsDisplayname.setText(name);
                settingsDisplayservice.setText(service);
                settingsDisplayStatus.setText(status);
                settingsDisplayaddress.setText(address);
                settingsDisplayphone.setText(phone);

                if (!image.equals("default_profile")) {
                    // 

это другая половина кода, но все здесь работает нормально ...

Picasso.with(SettingsActivity.this).load(image).placeholder(R.drawable.default_profile).into(settingsDisplayProfileImage);

                        Picasso.with(SettingsActivity.this).load(image).fit().centerInside().networkPolicy(NetworkPolicy.OFFLINE)
                                .placeholder(R.drawable.default_profile).into(settingsDisplayProfileImage, new Callback() {
                            @Override
                            public void onSuccess() {

                            }

                            @Override
                            public void onError() {
                                Picasso.with(SettingsActivity.this).load(image).fit().centerInside().placeholder(R.drawable.default_profile).into(settingsDisplayProfileImage);
                            }
                        });
                    }

                }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });


            settingsChangeProfileImageButton.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View view)
                {
                    Intent galleryIntent = new Intent();
                    galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
                    galleryIntent.setType("image/*");
                    startActivityForResult(galleryIntent, Gallery_Pick);
                }
            });


            settingsUpdateProfileButton.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View view)
                {
                    String old_status = settingsDisplayStatus.getText().toString();
                    String old_address = settingsDisplayaddress.getText().toString();
                    String old_phone = settingsDisplayphone.getText().toString();
                    String old_service = settingsDisplayservice.getText().toString();

                    Intent profileIntent = new Intent(SettingsActivity.this, ProfileActivity.class);
                    profileIntent.putExtra("user_status", old_status);
                    profileIntent.putExtra("user_service", old_service);
                    profileIntent.putExtra("user_phone", old_phone);
                    profileIntent.putExtra("user_address", old_address);
                    startActivity(profileIntent);
                }
            });

        }


        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data)
        {
            super.onActivityResult(requestCode, resultCode, data);

            if(requestCode==Gallery_Pick && resultCode==RESULT_OK &&data!=null)
            {
                Uri ImageUri = data.getData();

                CropImage.activity()
                        .setGuidelines(CropImageView.Guidelines.ON)
                        .setAspectRatio(1, 1)
                        .start(this);

            }

            if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE)
            {
                CropImage.ActivityResult result = CropImage.getActivityResult(data);

                if (resultCode == RESULT_OK)
                {
                    loadingBar.setTitle("Updating Profile Image");
                    loadingBar.setMessage("Please wait while profile image is updated");
                    loadingBar.show();

                    Uri resultUri = result.getUri();


                    File thumb_filepathUri = new File(resultUri.getPath());



                    String user_id = mAuth.getCurrentUser().getUid();


                    try
                    {
                        thumb_bitmap = new Compressor(this)
                                .setMaxWidth(100)
                                .setMaxHeight(100)
                                .setQuality(50)
                                .compressToBitmap(thumb_filepathUri);
                    }
                    catch (IOException e)
                    {
                        e.printStackTrace();
                    }

                    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                    thumb_bitmap.compress(Bitmap.CompressFormat.JPEG, 50, byteArrayOutputStream);
                    final byte[] thumb_byte = byteArrayOutputStream.toByteArray();



                    StorageReference filePath = storeProfileImagestorageRef.child(user_id + ".jpg");

                    final StorageReference thumb_filePath = thumbImageRef.child(user_id + ".jpg");





                    filePath.putFile(resultUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
                        @Override
                        public void onComplete(@NonNull final Task<UploadTask.TaskSnapshot> task)
                        {
                            if (task.isSuccessful())
                            {
                                Toast.makeText(SettingsActivity.this,
                                        "Saving profile image...", Toast.LENGTH_LONG).show();



                             //   final String downloadUrl = task.getResult().getDownloadUrl().toString();
                                downloadImageUrl =thumb_filePath .getDownloadUrl().toString();

                                UploadTask uploadTask = thumb_filePath.putBytes(thumb_byte);

                                uploadTask.addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>()
                                {
                                    @Override
                                    public void onComplete(@NonNull Task<UploadTask.TaskSnapshot>thumb_task)
                                    {
                                        String thumb_downloadUrl = thumb_filePath.getDownloadUrl().toString();
                                     //  thumb_downloadImageUrl = filePath.getDownloadUrl().toString();
                                        if (task.isSuccessful())
                                        {
                                            Map update_user_data = new HashMap();
                                            update_user_data.put("user_image", downloadImageUrl);
                                            update_user_data.put("user_thumb_image", thumb_downloadUrl);

                                            getUserDataReference.updateChildren(update_user_data)
                                                    .addOnCompleteListener(new OnCompleteListener<Void>()
                                                    {
                                                        @Override
                                                        public void onComplete(@NonNull Task<Void> task)
                                                        {
                                                            Toast.makeText(SettingsActivity.this,
                                                                    "Profile Image Updated Successfully", Toast.LENGTH_SHORT).show();

                                                            loadingBar.dismiss();

                                                        }
                                                    });
                                        }

                                    }
                                });



                            }

                            else
                            {
                                Toast.makeText(SettingsActivity.this, "Error occurred while uploading your profile picture..",
                                                                                Toast.LENGTH_SHORT).show();

                                loadingBar.dismiss();
                            }
                        }
                    });
                }

                else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
                    Exception error = result.getError();
                }
            }


        }
    }

это logcat ниже

11-15 03:22:10.646 27837-27837/com.paddi.paddi.paddi E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.paddi.paddi.paddi, PID: 27837
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
        at com.paddi.paddi.paddi.SettingsActivity$1.onDataChange(SettingsActivity.java:101)
        at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@16.0.4:75)
        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:815)
        at android.os.Handler.dispatchMessage(Handler.java:104)
        at android.os.Looper.loop(Looper.java:207)
        at android.app.ActivityThread.main(ActivityThread.java:5765)
        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)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...