com.google.firebase.database.DatabaseException:

com.google.firebase.database.DatabaseException:无法将 java.lang.String 类型的对象转换为类型...


还有一个错误没有连接适配器;跳过布局 - 但似乎不是严重错误 ///// 请帮助,我是编程新手,这个项目对我来说非常重要。/////


我的消息活动


public class MessageActivity extends AppCompatActivity {


CircleImageView ivProfileImage;

TextView tvUsername;


ImageButton btnSend;

EditText text_send;


MessageAdapter messageAdapter;

List<Chat> mChat;


FirebaseUser firebaseUser;

DatabaseReference databaseReference;


RecyclerView recycler_view;


Intent intent;


@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_message);


    Toolbar toolbar = findViewById(R.id.toolbar);

    setSupportActionBar(toolbar);

    getSupportActionBar().setTitle("");

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    toolbar.setNavigationOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View v) {

            finish();

        }

    });


    recycler_view = findViewById(R.id.recycler_view);

    recycler_view.setHasFixedSize(true);

    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());

    linearLayoutManager.setStackFromEnd(true);

    recycler_view.setLayoutManager(linearLayoutManager);


    ivProfileImage = findViewById(R.id.ivProfileImage);

    tvUsername = findViewById(R.id.tvUsername);

    btnSend = findViewById(R.id.btnSend);

    text_send = findViewById(R.id.text_send);


    intent = getIntent();

    final String userid = intent.getStringExtra("userid");

    firebaseUser = FirebaseAuth.getInstance().getCurrentUser();


    });



开满天机
浏览 62回答 2
2回答

慕哥9229398

使用以下代码行时:databaseReference.child("Chats").push().setValue(hashMap);DatabaseReference 的push()方法:创建对自动生成的子位置的引用。这基本上意味着,您正在使用节点Chat下的自动生成键创建一个新对象Chats,在您的架构中应该如下所示:Firebase-root&nbsp; |&nbsp; --- Chats&nbsp; &nbsp; &nbsp; &nbsp;|&nbsp; &nbsp; &nbsp; &nbsp;--- pushedId&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;--- message: "fggfg"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;--- receiver: "0AQZ ... FK02"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;--- sender: "CqjS ... QqE3"但不幸的是,这不是您的数据库中的样子。看,在Chats节点下,没有推送的id,你的属性直接存在于Chats节点下。要解决这个问题,请删除节点下存在的那些属性并使用以下代码行Chats添加对象:ChatChat chat = new Chat(sender, receiver, message);DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();DatabaseReference chatsRef = rootRef.child("Chats");String key = chatsRef.child("Chats").push().getKey();chatsRef.child(key).setValue(chat);你的问题将得到解决。

潇湘沐

您正在尝试将字符串文本转换为聊天类型。用这个 -&nbsp;String chat = snapshot.getValue(String.class);代替&nbsp;Chat chat = snapshot.getValue(Chat.class);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java