【问题标题】:App just opens and closes after running from android studio on android phone从 android 手机上的 android studio 运行后,应用程序只是打开和关闭
【发布时间】:2019-01-10 16:30:18
【问题描述】:

应用程序只是在 android studio 上从 android studio 运行后打开和关闭。

还会显示以下警告:

应用 gradle 文件必须依赖于 com.google.firebase:firebase-core,Firebase 服务才能按预期工作。

我尝试根据教程制作应用程序,该应用程序编译成功,但在安卓手机上运行时只是打开和关闭。

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

【问题讨论】:

  • 把你的stacktrace放在这里
  • 从你的 logcat 记录错误
  • @AbnerEscocio 刚刚添加了日志错误。
  • @AbnerEscocio 编辑了我的答案并添加了来自 logcat 的日志错误。请帮忙

标签: android android-studio debugging crash


【解决方案1】:

尝试在 Android Studio 中使用Firebase 工具。选择您要集成的服务,然后按照分步说明进行操作。

【讨论】:

    【解决方案2】:

    假设所有其他设置都正确,错误是不言自明的。您需要在依赖项下的应用程序 gradle 文件中添加对 Firebase Core 的依赖项:

    implementation 'com.google.firebase:firebase-core:16.0.1'
    

    您可能还想查看下面的链接以了解任何缺少的步骤。

    Add Firebase to Android Project

    【讨论】:

    • 但是添加这个后运行应用程序会崩溃
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-18
    • 2015-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多