- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在 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/
public class Foo : IFoo ... 和有什么区别 IFoo foo = new Foo(); 和 Foo foo = new Foo(); 最佳答案 区别仅在于变量的声明类型。每当
class Foo { public: explicit Foo() {} explicit Foo(Foo&) {} }; Foo d = Foo(); error: no matc
是 foo as? Foo完全等同于 foo as Foo? ? 如果是,那为什么两者都有? 如果不是,那么有什么区别? 最佳答案 as?是 safe cast operator . 通常,如果您尝试
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 9 年前。 Improve
这个问题在这里已经有了答案: Why is there an injected class name? (1 个回答) 关闭5年前。 一位同事不小心写了这样的代码: struct foo { fo
我遇到了这些关键字::foo、::foo、::bar/foo 和 :bar/foo 您能举例说明差异吗? 最佳答案 :foo 是一个非完全限定的关键字。它没有关联的命名空间。 (name :foo)
有人问我如何简化这个 lambda (Foo foo) -> foo.getName() 还有更好的写法吗? 最佳答案 Foo::getName。 假设getName是一个实例方法,其签名中的参数列表
编写此规则集的 CSS 组合器或简写是什么? foo bar, foo biz, foo gaz > boo, foo tar { ... } 我很确定我在 MDN 的某处读到过有一个。是不是: f
我有一个用这个字符串填充的输入文本 "foo foo"但插入后字符串看起来像这样 "foo foo" .我该如何解决?我想以第一种格式显示字符串! 最佳答案 你可能有这样的事情: " /> 更改 va
假设我有一个不可复制类Foo,它的构造函数之一恰好接收到对 Foo 的引用。 class Foo { public: Foo(Foo& parent) {...} private: v
class Artist @@song_count = [] attr_accessor :name, :songs def initialize(name) @name = name
请解释为什么这些 Perl 函数的调用方式在函数定义之上决定了它们是否运行。 print "Why does this bare call to foo not run?\n"; foo; print
文件名分为三种类型 首先( Root 于某种“当前工作目录”之下) ../foo ./foo bar/foo # really in this group? 和( Root 于绝对路径,独立于 CWD
我想自动连接 foo: @Autowired Foo foo 但我无法修改类 Foo 并将其标记为 @Component。 Autowiring foo 最干净的方法是什么? 顺便说一句,如果您需要使
我一直在使用 Python 的 ElementTree 创建 XML 文档,到目前为止一切顺利。然而我现在面临的问题是,由于项目要求,我需要生成一个 XML 文档,其中包含带有开始和结束标签的元素以及
class Foo { public: Foo(){} private: Foo(const Foo &); }; Foo f(); Foo f1 = Foo(); 我发现当我将 Fo
我有一个 jquery 对话框,上面有一个按钮(我的按钮,不是对话框按钮之一)。 设置对话框后,我有以下代码: $('#bar').click(foo('baz')); 当我加载页面并显示对话框时,f
我遇到了以下变量的情况: var foo: Foo | Foo | Foo; 动态生成(使用 keyof 和 stuff),这在代码的那个点是完全有意的。但是,我需要调用像这样定义的对象内部的方法:
clang 3.5.0 和 gcc 4.9.1 从代码生成的可执行文件 #include struct Foo { Foo() { std::cout << "Foo()" << std::e
对于声明为 Foo& foo = ...; 的 foo,lambdas 的按值捕获和按引用捕获语义之间有什么区别吗? 最佳答案 我认为你已经陷入了一个常见的误解......引用是对真实对象的别名。初始
我是一名优秀的程序员,十分优秀!