从android手机上的android studio运行后,应用只会打开和关闭 [英] App just opens and closes after running from android studio on android phone
问题描述
从android studio上的android studio运行后,应用只会打开和关闭.
App just opens and closes after running from android studio on android studio.
还会显示以下警告:
应用gradle文件必须依赖com.google.firebase:firebase-core,Firebase服务才能正常运行.
The app gradle file must have a dependency on com.google.firebase:firebase-core for Firebase services to work as intended.
我尝试通过教程制作该应用程序,该应用程序已成功编译,但是在Android手机上运行时只能打开和关闭.
I tried making making the app from a tutorial, the app is compiled successfully but just opens and closes when it is run on android phone.
build.gradle
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
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的日志错误
log error from 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
推荐答案
尝试在Android Studio中使用Firebase
工具.选择要集成的服务,然后按照分步说明进行操作.
Try using the Firebase
tool within Android Studio. Select the service you want to integrate, and follow the step-by-step instructions.
这篇关于从android手机上的android studio运行后,应用只会打开和关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!