根据我在envatotuts+上找到的教程,我正在开发一个Android群组聊天应用程序,除了FirebaseListAdapter
之外,一切看起来都很好。该项目拒绝编译,它提供此错误Error:(18, 32) error: package com.firebase.ui.database does not exist
,在进行下面建议的更正后,错误消失,此错误弹出Error:(133, 19) error: cannot find symbol constructor (DatabaseReference,Class<ChatMessage>,int,MainActivity)
。附件是MainActivity.java
文件和我的activityMain.xml
文件。
MainActivity.java
package com.abdul.friendsdemo;
import android.content.Intent;
import android.icu.text.DateFormat;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
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.firebase.auth.FirebaseAuth;
import com.google.firebase.database.FirebaseDatabase;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
private FirebaseListAdapter<ChatMessage> adapter;
private static final int SIGN_IN_REQUEST_CODE = 123;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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
displayChatMessage();
}
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("");
}
});
}
@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();
displayChatMessage();
}else{
Toast.makeText(this, "We couldn't sign you in. Please try again later.",
Toast.LENGTH_LONG).show();
//Close the app
finish();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.sign_out) {
AuthUI.getInstance().signOut(this)
.addOnCompleteListener(new OnCompleteListener<Void>() {
public void onComplete(@NonNull Task<void> task) {
Toast.makeText(MainActivity.this,
"You have been signed out.",
Toast.LENGTH_LONG).show();
startActivity(new Intent(MainActivity.this,
AuthUI.class));
// Close activity;
finish();
}
});
}
return true;
}
private void displayChatMessage() {
ListView listOfMessages = (ListView) findViewById(R.id.list_of_messages);
adapter = new FirebaseListAdapter<Chatmessage>(this, ChatMessage.class,
R.layout.messages, FirebaseDatabase.getInstance().getReference()) {
@Override
protected void populateView(View v, ChatMessage model, int position) {
//Get references to the views of messages.xml
TextView messageText = (TextView) v.findViewById(R.id.message_body);
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
Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR);
int minute = c.get(Calendar.MINUTE);
messageTime.setText(hour + ":" + minute);
}
};
listOfMessages.setAdapter(adapter);
}
}
和activityMain.xml
文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.abdul.friendsdemo.MainActivity">
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:clickable="true"
android:src="@drawable/ic_send_black_24dp"
android:tint="@android:color/white"
app:fabSize="mini" />
<EditText
android:id="@+id/input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="@id/fab" />
<ListView
android:id="@+id/list_of_messages"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/fab"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="@dimen/activity_horizontal_margin"
android:divider="@android:color/transparent"
android:dividerHeight="@dimen/activity_horizontal_margin" />
</RelativeLayout>
app:build gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.0"
defaultConfig {
applicationId "com.abdul.friendsdemo"
minSdkVersion 16
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:design:25.0.1'
compile 'com.android.support:appcompat-v7:25.0.1'
compile 'com.firebaseui:firebase-ui-auth:1.2.0'
compile 'com.google.firebase:firebase-core:10.2.0'
compile 'com.firebaseui:firebase-ui:1.2.0'
compile 'com.google.firebase:firebase-auth:10.2.0'
compile 'com.google.firebase:firebase-database:10.2.0'
testCompile 'junit:junit:4.12'
}
apply plugin: 'com.google.gms.google-services'
我已经尝试了很多其他问题,但是对他们有用的解决方案并不适用于我。
答案 0 :(得分:1)
您的声明订单有误。这样:
adapter = new FirebaseListAdapter<Chatmessage>(this, ChatMessage.class,
R.layout.messages, FirebaseDatabase.getInstance().getReference())...
应该是:
adapter = new FirebaseListAdapter<Chatmessage>(FirebaseDatabase.getInstance().getReference(), ChatMessage.class,
R.layout.messages,this )
这是基于文档here。您可以看到构造函数是:public FirebaseListAdapter(Query mRef, Class<T> mModelClass, int mLayout, Activity activity) {
您在最后一个参数处有参考,而不是第一个参数。活动应该是最后一次。
答案 1 :(得分:1)
如果有人仍然遇到此问题,请使用此功能。
Query query = FirebaseDatabase.getInstance().getReference();
FirebaseListOptions<ChatMessage> options = new FirebaseListOptions.Builder<ChatMessage>()
.setQuery(query, ChatMessage.class)
.build();
adapter = new FirebaseListAdapter<ChatMessage>(options) {
@Override
protected void populateView(View v, ChatMessage model, int position) {
}
};
答案 2 :(得分:0)
答案 3 :(得分:0)
以下解决方案对我有帮助。其他人可能会尝试使用它。
private void displayChatMessage(){
ListView listOfMessage = (ListView) findViewById ( R.id.list_of_message );
Query query = FirebaseDatabase.getInstance().getReference().child("chats");
FirebaseListOptions<ChatMessage> options =
new FirebaseListOptions.Builder<ChatMessage>()
.setQuery(query, ChatMessage.class)
.setLayout(android.R.layout.simple_list_item_1)
.build();
adapter = new FirebaseListAdapter<ChatMessage>(options){
@Override
protected void populateView(@NonNull View v, @NonNull ChatMessage model, int position) {
TextView messageText, messageUser, messageTime;
messageText = v.findViewById ( R.id.message_text );
messageUser = v.findViewById ( R.id.message_user );
messageTime = v.findViewById ( R.id.message_time );
messageText.setText ( model.getMessageText () );
messageUser.setText ( model.getMessageUser () );
messageTime.setText ( DateFormat.format("dd-MM-yyyy(HH:mm:ss)", model.getMessageTime ()) );
}
};
listOfMessage.setAdapter ( adapter );
}