即使在提供准确的 firebase 参考后,也会出现 'Failed to bounce to type' 异常。请查看详情

Getting 'Failed to bounce to type' exception even after giving the accurate firebase reference. Please see details

我正在开发一个将 Firebase 用作 BaaS 的应用程序。

问题是,即使在提供了正确的 firebase 参考后,我仍然得到 com.firebase.client.FirebaseException: Failed to bounce to type at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:183) at com.abc.xyz.MainActivity.onDataChange(MainActivity.java:119)。堆栈跟踪中另一个值得注意的行是 Caused by: com.fasterxml.jackson.databind.JsonMappingException: Conflicting setter definitions for property "colorFilter": android.widget.ImageView#setColorFilter(1 params) vs android.widget.ImageView#setColorFilter(1 params).

这里是 Firebase 参考:Firebase mFirebaseRef = new Firebase("https://appname.firebaseio.com/users");

这是我的一段代码:

    mFirebaseRef.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot snapshot) {
                        System.out.println("There are " + snapshot.getChildrenCount() + " blog posts");
                        for (DataSnapshot postSnapshot : snapshot.getChildren()) {
    //error on this line 
UsersDataFromFirebase post = postSnapshot.getValue(UsersDataFromFirebase.class);

                            View nameView = navigationView.getHeaderView(0);
                            userName = (TextView) nameView.findViewById(R.id.userName);
                            userName.setText(post.getUserName());

                            System.out.println(post.getUserName());
                            System.out.println(post.getUserEmail());

    //                        View emailView = navigationView.getHeaderView(0);
    //                        userEmail = (TextView) emailView.findViewById(R.id.userEmail);
    //                        userEmail.setText();

                            userImageUrl = (URL) authData.getProviderData().get("profileImageUrl");

                            String userImageUrlString = userImageUrl.toString();

                            URL url = null;
                            try {
                                url = new URL(userImageUrlString);
                            } catch (MalformedURLException e) {
                                e.printStackTrace();
                            }
                            try {
                                image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
                            } catch (IOException e) {
                                e.printStackTrace();
                            }

                            View imageView = navigationView.getHeaderView(0);
                            userImage = (ImageView) imageView.findViewById(R.id.userImage);
                            userImage.setImageBitmap(image);

                        }
                    }

                    @Override
                    public void onCancelled(FirebaseError firebaseError) {
                        System.out.println("The read failed: " + firebaseError.getMessage());
                        Toast.makeText(getBaseContext(), firebaseError.getMessage(), Toast.LENGTH_LONG).show();
                    }
                });

            } else {
                android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(MainActivity.this);
                builder.setTitle("No internet connection!");
                builder.setMessage("Please connect to the internet.");
                builder.setPositiveButton("Open Settings", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Intent intent = new Intent(Settings.ACTION_SETTINGS);
                        startActivity(intent);
                        //progressBar.setVisibility(View.INVISIBLE);
                    }
                });
                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //progressBar.setVisibility(View.INVISIBLE);
                    }
                });
                builder.show();
            }

这里是 UsersDataFromFirebase.java 文件的代码:

public class UsersDataFromFirebase {
    private String userName;
    private String userEmail;
    private ImageView userImage;

    public UsersDataFromFirebase() {
        // empty default constructor, necessary for Firebase to be able to deserialize blog posts
    }

    public String getUserName() {
        return userName;
    }

    public String getUserEmail() {
        return userEmail;
    }

    public ImageView getUserImage() {
        return userImage;
    }

}

由于 Firebase 对我来说是一个非常新的东西,我无法弄清楚这里出了什么问题。

请告诉我。

提前致谢!

您在 Java class 中包含了一个 ImageView,Jackson/Firebase 不知道如何将数据序列化到。

解决方案是告诉 Firebase/Jackson 在读取对象 from/writes 到 JSON 时忽略 ImageView。

public class UsersDataFromFirebase {
    private String userName;
    private String userEmail;
    private ImageView userImage;

    public UsersDataFromFirebase() {
        // empty default constructor, necessary for Firebase to be able to deserialize blog posts
    }

    public String getUserName() { return userName; }

    public String getUserEmail() { return userEmail; }

    @JsonIgnore    
    public ImageView getUserImage() { return userImage; }
}

请注意,这已包含在我对这个问题的回答中: