简体   繁体   中英

Why I am getting this exception, what I am doing wrong here

Failed to bounce to type

W/System.err:
at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:183)

caused by : com.fasterxml.jackson.databind.JsonMappingException: 
Can not instantiate value of type [simple type, class hi.example.com.hi.DataModel] from String value; no single-String constructor/factory method.

even if i use single string constructor in my DataModel class , i get one string value at a time in the constructor,, why this happening ,, please someone help me ..

 here is my json file
   {  
      "messages": {
        "matt__walk": {
          "Username": "matt__walk",
          "imageurl": "https://firebasestorage.googleapis.com/v0",
          "text": "hii"
             }
         }
   }

DataModel class:

@JsonIgnoreProperties(ignoreUnknown=true)
public class DataModel {
    private String Username;
    private String imageurl;
    private String text;

    public DataModel(){
    }
    public String getUsername() {
        return Username;
    }
    public String getText() {
        return text;
    }
    public String getImageUrl() {
        return imageurl;
    }
}

MainActivity class :

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    Menu menu;
    private Toolbar toolbar;
    private LinearLayoutManager layoutManager;
    private static RecyclerView recyclerView;
    private Firebase ref;
    private String res;
    private DatabaseReference mDatabase;
    private FirebaseRecyclerAdapter<DataModel, MessageViewHolder>    mFirebaseAdapter;

    public static class MessageViewHolder extends RecyclerView.ViewHolder {
        View mViews;
        CircleImageView img;
        TextView name,data;
        public MessageViewHolder(View views) {
            super(views);
            mViews = views;
        }

        public void setText(String Name) {
            TextView name = (TextView) mViews.findViewById(R.id.Name);
            name.setText(Name);
        }

        public void setData(String Data) {
            TextView data = (TextView) mViews.findViewById(R.id.datas);
            data.setText(Data);
        }

        public void setImage(Context cnt, String imageUrl) {
            CircleImageView img = (CircleImageView) mViews.findViewById(R.id.image);
            Picasso.with(cnt).load(imageUrl).into(img);
        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainactivity);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        Firebase.setAndroidContext(this);
        recyclerView = (RecyclerView) findViewById(R.id.recycler);
        recyclerView.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(this);
        ref = new Firebase("https://hi-34.firebaseio.com/messages/matt__walk");
        mFirebaseAdapter = new FirebaseRecyclerAdapter<DataModel, MessageViewHolder>(
                DataModel.class,
                R.layout.rowplaces,
                MessageViewHolder.class,
                ref
        ) {
            @Override
            protected void populateViewHolder(MessageViewHolder messageViewHolder, DataModel dataModel, int i) {


                        messageViewHolder.setText(dataModel.getUsername());
                        messageViewHolder.setData(dataModel.getText());
                 messageViewHolder.setImage(getApplicationContext(),dataModel.getImageUrl());


            }
        };
        mFirebaseAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
            @Override
            public void onItemRangeInserted(int positionStart, int itemCount) {
                super.onItemRangeInserted(positionStart, itemCount);
                int friendlyMessageCount = mFirebaseAdapter.getItemCount();
    int lastVisiblePosition =    layoutManager.findLastCompletelyVisibleItemPosition();
                if (lastVisiblePosition == -1 ||
                        (positionStart >= (friendlyMessageCount - 1) &&
                                lastVisiblePosition == (positionStart - 1))) {
                    recyclerView.scrollToPosition(positionStart);
                }
            }
        });
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(mFirebaseAdapter);
    }

Recycler views (and list views) show lists of items. The FirebaseRecyclerAdapter is made to populate such a list of items.

If you add a FirebaseRecyclerAdapter on a node, it will try to add the nodes under that node to the recycler view. So in your case that means that it tries to add Username , imageurl and text to the list. That doesn't seem to be what you have in mind. To solve this problem, you'll need to make the adapter listen one level higher in the tree:

ref = new Firebase("https://hi-34.firebaseio.com/messages");
mFirebaseAdapter = new FirebaseRecyclerAdapter<DataModel, MessageViewHolder>(
        DataModel.class,
        R.layout.rowplaces,
        MessageViewHolder.class,
        ref
) {

With that, it will call populateViewHolder for each child of messages , the first/only one of which is matt__walk .

The second problem is that you have a mismatch between the casing of the property names in your JSON and the corresponding getters in your Java class.

If use:

public class DataModel {
    private String username;
    private String imageUrl;
    private String text;

    public DataModel(){
    }
    public String getUsername() {
        return Username;
    }
    public String getText() {
        return text;
    }
    public String getImageUrl() {
        return imageurl;
    }
}

And then:

matt__walk": {
    "username": "matt__walk",
    "imageUrl": "https://firebasestorage.googleapis.com/v0",
    "text": "hii"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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