gpt4 book ai didi

java - FirebaseListAdapter 无法将 java.lang.String 类型的对象转换为 foo.bar.ChatMessage 类型

转载 作者:行者123 更新时间:2023-12-02 11:18:08 36 4
gpt4 key购买 nike

我正在尝试在 Android 上编写一个 Firebase 应用程序,该应用程序本质上是一个大型实时聊天室。我可以使用登录和注销功能,但是当我尝试从数据库获取消息类的实例时,我收到一个奇怪的错误,涉及将其转换为字符串的问题,从而导致应用程序崩溃。一定有一些我不知道的隐藏调用。

我已将问题范围缩小到主要 Activity 的这一部分:

private void displayChatMessages() {
ListView listOfMessages = (ListView)findViewById(R.id.list_of_messages);

adapter = new FirebaseListAdapter<ChatMessage>(this, ChatMessage.class,
R.layout.message, FirebaseDatabase.getInstance().getReference()) {
@Override
protected void populateView(View v, ChatMessage model, int position) {
// Get references to the views of message.xml
TextView messageText = (TextView)v.findViewById(R.id.message_text);
TextView messageUser = (TextView)v.findViewById(R.id.message_user);
TextView messageTime = (TextView)v.findViewById(R.id.message_time);

// Set their text
messageText.setText(model.getMessageText());
messageUser.setText(model.getMessageUser());

// Format the date before showing it
messageTime.setText(DateFormat.format("dd-MM-yyyy (HH:mm:ss)",
model.getMessageTime()));
}
};

listOfMessages.setAdapter(adapter);
}

我推测这与我对 populateView 的调用有关,尽管我不知道为什么。

这是我的日志猫的错误:

om.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type foo.bar.ChatMessage
at com.google.android.gms.internal.zg.zzb(Unknown Source:870)
at com.google.android.gms.internal.zg.zza(Unknown Source:0)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source:10)
at com.firebase.ui.database.FirebaseListAdapter.parseSnapshot(FirebaseListAdapter.java:127)
at com.firebase.ui.database.FirebaseListAdapter.getItem(FirebaseListAdapter.java:116)
at com.firebase.ui.database.FirebaseListAdapter.getView(FirebaseListAdapter.java:146)
at android.widget.AbsListView.obtainView(AbsListView.java:2408)
at android.widget.ListView.makeAndAddView(ListView.java:2052)
at android.widget.ListView.fillDown(ListView.java:786)
at android.widget.ListView.fillFromTop(ListView.java:847)
at android.widget.ListView.layoutChildren(ListView.java:1798)
at android.widget.AbsListView.onLayout(AbsListView.java:2207)
at android.view.View.layout(View.java:19653)
at android.view.ViewGroup.layout(ViewGroup.java:6082)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1080)
at android.view.View.layout(View.java:19653)
at android.view.ViewGroup.layout(ViewGroup.java:6082)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:19653)
at android.view.ViewGroup.layout(ViewGroup.java:6082)
at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:443)
at android.view.View.layout(View.java:19653)
at android.view.ViewGroup.layout(ViewGroup.java:6082)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:19653)
at android.view.ViewGroup.layout(ViewGroup.java:6082)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
at android.view.View.layout(View.java:19653)
at android.view.ViewGroup.layout(ViewGroup.java:6082)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:758)
at android.view.View.layout(View.java:19653)
at android.view.ViewGroup.layout(ViewGroup.java:6082)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2626)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2321)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1466)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6982)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:927)
at android.view.Choreographer.doCallbacks(Choreographer.java:729)
at android.view.Choreographer.doFrame(Choreographer.java:658)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:913)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:180)
at android.app.ActivityThread.main(ActivityThread.java:6950)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:835)

这是我的主要 Activity

package foo.bar;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.firebase.ui.auth.AuthUI;
import com.firebase.ui.database.FirebaseListAdapter;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.FirebaseDatabase;


public class MainActivity extends AppCompatActivity {

private FirebaseAuth mAuth; // object used to authorize the app to connect to Firebase
private FirebaseListAdapter<ChatMessage> adapter;

int SIGN_IN_REQUEST_CODE = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

int SIGN_IN_REQUEST_CODE = 1;

//Start the user sign-in display if the user is not logged in.
if(FirebaseAuth.getInstance().getCurrentUser() == null) {
// Start sign in/sign up activity
startActivityForResult(
AuthUI.getInstance()
.createSignInIntentBuilder()
.build(),
SIGN_IN_REQUEST_CODE
);
} else {
// User is already signed in. Therefore, display
// a welcome Toast
Toast.makeText(this,
"Welcome " + FirebaseAuth.getInstance()
.getCurrentUser()
.getDisplayName(),
Toast.LENGTH_LONG)
.show();

// Load chat room contents
displayChatMessages();
}

//Attach a listener to the FAB so that users can send messages using it.
FloatingActionButton fab = (FloatingActionButton)findViewById(R.id.fab);

fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText input = (EditText)findViewById(R.id.input);

// Read the input field and push a new instance
// of ChatMessage to the Firebase database
FirebaseDatabase.getInstance()
.getReference()
.push()
.setValue(new ChatMessage(input.getText().toString(),
FirebaseAuth.getInstance()
.getCurrentUser()
.getDisplayName())
);

// Clear the input
input.setText("");
}
}); //end of FAB listener

} //end of onCreate

private void displayChatMessages() {
ListView listOfMessages = (ListView)findViewById(R.id.list_of_messages);

adapter = new FirebaseListAdapter<ChatMessage>(this, ChatMessage.class,
R.layout.message, FirebaseDatabase.getInstance().getReference()) {
@Override
protected void populateView(View v, ChatMessage model, int position) {
// Get references to the views of message.xml
TextView messageText = (TextView)v.findViewById(R.id.message_text);
TextView messageUser = (TextView)v.findViewById(R.id.message_user);
TextView messageTime = (TextView)v.findViewById(R.id.message_time);

// Set their text
messageText.setText(model.getMessageText());
messageUser.setText(model.getMessageUser());

// Format the date before showing it
messageTime.setText(DateFormat.format("dd-MM-yyyy (HH:mm:ss)",
model.getMessageTime()));
}
};

listOfMessages.setAdapter(adapter);
}

@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if(requestCode == SIGN_IN_REQUEST_CODE) {
if(resultCode == RESULT_OK) {
Toast.makeText(this,
"Successfully signed in. Welcome!",
Toast.LENGTH_LONG)
.show();
displayChatMessages();
} else {
Toast.makeText(this,
"We couldn't sign you in. Please try again later.",
Toast.LENGTH_LONG)
.show();

// Close the app
finish();
}
}

}

//Configure and implement the side menu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}

//Sign out activity function
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.menu_sign_out) {//if the sign-out option is selected from the menu

//Sign the user out
AuthUI.getInstance().signOut(this)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Toast.makeText(MainActivity.this,
"You have been signed out.",
Toast.LENGTH_LONG)
.show();

// Close activity
finish();
}
});
}
return true;
}
//End of sign out functions
}

还有我的 ChatMessage 模型:

package foo.bar;


import java.util.Date;

public class ChatMessage {

private String messageText;
private String messageUser;
private long messageTime;

public ChatMessage(String messageText, String messageUser) {
this.messageText = messageText;
this.messageUser = messageUser;

// Initialize to current time
messageTime = new Date().getTime();
}

public ChatMessage(){

}

public String getMessageText() {
return messageText;
}

public void setMessageText(String messageText) {
this.messageText = messageText;
}

public String getMessageUser() {
return messageUser;
}

public void setMessageUser(String messageUser) {
this.messageUser = messageUser;
}

public long getMessageTime() {
return messageTime;
}

public void setMessageTime(long messageTime) {
this.messageTime = messageTime;
}
}

list :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="foo.bar">

<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

应用程序 Gradle:

apply plugin: 'com.android.application'

android {
compileSdkVersion 26
defaultConfig {
applicationId "ucr.cs100chat"
minSdkVersion 22
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.0.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
implementation 'com.google.firebase:firebase-auth:11.0.4'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
compile 'com.google.firebase:firebase-core:11.0.4'
compile 'com.firebaseui:firebase-ui-auth:2.3.0'
compile 'com.google.firebase:firebase-database:11.0.4'
compile 'com.google.firebase:firebase-auth:11.0.4'
compile 'com.firebaseui:firebase-ui:0.6.0'
}

apply plugin: 'com.google.gms.google-services'

项目 Gradle:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {

repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath 'com.google.gms:google-services:3.2.0' //Firebase requirement

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}

allprojects {
repositories {
google()
jcenter()
}
}

task clean(type: Delete) {
delete rootProject.buildDir
}

最佳答案

这个:

FirebaseDatabase.getInstance().getReference()

将检索数据库的全部内容,并且在 rootNode 下有一个字符串,因此出现错误。

要解决此问题,请添加一个 child("Message")

FirebaseDatabase.getInstance()
.getReference()
.child("Message")
.push()
.setValue(new ChatMessage(input.getText().toString(),
FirebaseAuth.getInstance()
.getCurrentUser()
.getDisplayName())
);

同时检索子消息节点:

FirebaseDatabase.getInstance().getReference().child("Message")

关于java - FirebaseListAdapter 无法将 java.lang.String 类型的对象转换为 foo.bar.ChatMessage 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50122337/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com