即使在提供准确的firebase参考后,也会“无法退回以键入”异常。请看细节

时间:2016-03-28 03:21:14

标签: java android firebase firebase-realtime-database

我正在开发一个将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$2.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对我来说是一个非常新的东西,我无法弄清楚这里出了什么问题。

请告诉我。

提前致谢!

1 个答案:

答案 0 :(得分:0)

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

解决方案是告诉Firebase / Jackson在从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; }
}

请注意,这个问题在我对这个问题的回答中有所涉及:Why do I get "Failed to bounce to type" when I turn JSON from Firebase into Java objects?