从android studio上的android studio运行后,应用只会打开和关闭。
还会显示以下警告:
应用gradle文件必须依赖com.google.firebase:firebase-core,Firebase服务才能正常工作。
我尝试通过教程制作该应用,该应用已成功编译,但是在Android手机上运行时只能打开和关闭。
build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.example.hp.chatapp"
minSdkVersion 16
targetSdkVersion 27
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:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
//add library
implementation 'com.android.support:design:27.1.1'
implementation 'com.firebaseui:firebase-ui-database:3.2.2'
implementation 'com.google.firebase:firebase-auth:10.2.1'
implementation 'com.firebaseui:firebase-ui-auth:3.2.2'
implementation "com.google.android.gms:play-services-gcm:10.2.1"
}
apply plugin: 'com.google.gms.google-services'
build.gradle
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.3'
classpath 'com.google.gms:google-services:4.0.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
主要活动是
public class MainActivity extends AppCompatActivity {
private static int SIGN_IN_REQUEST_CODE= 1;
private FirebaseListAdapter<ChatMessage> adapter;
RelativeLayout activity_main;
FloatingActionButton fab;
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==R.id.menu_sign_out){
AuthUI.getInstance().signOut(this).addOnCompleteListener(new
OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Snackbar.make(activity_main,"You have been signed out.",Snackbar.LENGTH_SHORT).show();
finish();
}
});
}
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu,menu);
return true;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==RESULT_OK){
Snackbar.make(activity_main,"Successfully signed in.Welcome !",Snackbar.LENGTH_SHORT).show();
displayChatMessage();
}
else {
Snackbar.make(activity_main,"We couldn't sign you in.Please try again later",Snackbar.LENGTH_SHORT).show();
finish();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activity_main=(RelativeLayout)findViewById(R.id.activity_main);
fab=(FloatingActionButton)findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText input= (EditText)findViewById(R.id.input);
FirebaseDatabase.getInstance().getReference().push().setValue(new ChatMessage(input.getText().toString(),FirebaseAuth.getInstance().getCurrentUser().getEmail()));
input.setText("");
}
});
//check if not sign in then navigate signin page
if(FirebaseAuth.getInstance().getCurrentUser()==null)
{
startActivityForResult(AuthUI.getInstance().createSignInIntentBuilder().build(),SIGN_IN_REQUEST_CODE);
}
else {
Snackbar.make(activity_main,"Welcome "+FirebaseAuth.getInstance().getCurrentUser().getEmail(),Snackbar.LENGTH_SHORT).show();
}
//load content
displayChatMessage();
}
private void displayChatMessage() {
ListView listOfMessage=(ListView)findViewById(R.id.list_of_message);
//Suppose you want to retrieve "chats" in your Firebase DB:
Query query =
FirebaseDatabase.getInstance().getReference().child("chats");
//The error said the constructor expected FirebaseListOptions - here you create them:
FirebaseListOptions<ChatMessage> options = new FirebaseListOptions.Builder<ChatMessage>()
.setQuery(query, ChatMessage.class)
.setLayout(android.R.layout.simple_list_item_1)
.build();
//Finally you pass them to the constructor here:
adapter = new FirebaseListAdapter<ChatMessage>(options){
@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 messageTime = (TextView)v.findViewById(R.id.message_time);
TextView messageUser=(TextView)findViewById(R.id.message_user);
messageText.setText(model.getMessageText());
messageUser.setText(model.getMessageUser());
messageTime.setText(DateFormat.format("dd-MM-yyyy (HH:mm:ss)",model.getMessageTime()));
}
};
listOfMessage.setAdapter(adapter);
}
}
已编辑
logcat的日志错误
08-03 02:00:47.736 3199-3199/com.example.hp.chatapp E/HAL: PATH3 /odm/lib64/hw/gralloc.qcom.so
PATH2 /vendor/lib64/hw/gralloc.qcom.so
PATH1 /system/lib64/hw/gralloc.qcom.so
PATH3 /odm/lib64/hw/gralloc.msm8953.so
08-03 02:00:47.737 3199-3199/com.example.hp.chatapp E/HAL: PATH2 /vendor/lib64/hw/gralloc.msm8953.so
PATH1 /system/lib64/hw/gralloc.msm8953.so
答案 0 :(得分:0)
尝试在Android Studio中使用Firebase
工具。选择要集成的服务,然后按照分步说明进行操作。
答案 1 :(得分:-1)
假定所有其他设置正确,则该错误不言自明。您需要在依赖项下的应用程序gradle文件中添加对Firebase Core的依赖项:
implementation 'com.google.firebase:firebase-core:16.0.1'
您可能还想查看下面的链接,以了解缺少的步骤。