繁体   English   中英

无法获取Firebase DataSnap快照值字符串

Not able to get firebase datasnapshot value string

提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供   中文繁体   英文版本   中英对照 版本,有任何建议请联系yoyou2525@163.com。

这是我数据库的结构这是从数据库中获取用户名的代码(dataSnapshot)显示Not Annotated参数覆盖@NonNull参数我为settingsActivity添加了整个代码,希望在下面帮助我

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)
问题暂未有回复.您可以查看右边的相关问题.
4 无法获取编码的 XML 字符串的值

我有一个从端点返回的长 xml 字符串。 我试图解析 xml 字符串并检索它具有的Order No属性,该属性是Orders的子级,其父级为File ,父级为Item 我执行了以下操作,但无法检索File或Item节点。 我希望从此 xml 中检索的主要值是 Order 属性No ,如果我无法 ...

7 无法获取Express中查询字符串的值

我正在尝试获取服务器中查询字符串term=+ e.target.value的值,但未显示。 试图将路由写为'/doSomeSearch?term' ,但仍然没有req.query.term在服务器中不显示任何值。 我将标头从json更改为“ Content-Type”:“ applicati ...

8 Android无法正确获取带有“”的字符串。 值中的“

我尝试检索包含十进制数字的字符串值,例如“ 3.5”。 尝试将其检索到android时,它们会带引号移动,但是当传递包含不带小数或不带点的数字的字符串时,有些想法无需将int传递给数据库或不将“ rates”传递给数据库价值int? 这样,我尝试从数据库中获取字符串“ rates”值 ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2021 STACKOOM.COM