聊天消息传递应用程序崩溃(使用Firebase)

时间:2018-08-11 21:35:54

标签: java android firebase firebase-realtime-database

我正在尝试使用Firebase在我的项目中实现一个简单的聊天消息功能。尝试从我的设备或Firebase发送消息时,该应用始终崩溃。

  

找到名称冲突的吸气剂:isChangingConfigurations

这是我引用的教程:https://code.tutsplus.com/tutorials/how-to-create-an-android-chat-app-using-firebase--cms-27397

这是成功遵循同一教程的人的github:  https://github.com/tutsplus/how-to-create-an-android-chat-app-using-firebase/blob/master/app/src/main/java/com/tutsplus/mychatapp/MainActivity.java

我已经坚持了一个星期,有人可以帮忙吗?

艺术家页面,选择聊天按钮:ArtistStartChatActivity.java

package com.plugmeinapp.plugmeinapp;
// this activity deals with Usertype:Artist to message artist or edit profile

import android.content.Intent;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.firebase.ui.database.FirebaseListAdapter;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import java.text.DateFormat;

public class ArtistStartChatActivity extends AppCompatActivity {

private FirebaseAuth mAuth;
private FirebaseUser mCurrentUser;
private DatabaseReference mRef, mUsersRef, mCurrentUserRef, mChatsRef;

private Button button_Artist_startChat, button_Artist_EditProfile;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_artist_start_chat);    // set to xml file layout


    //FireBase
    // initialize to get current user's information
    mAuth = FirebaseAuth.getInstance();
    mCurrentUser = mAuth.getCurrentUser();
    mRef = FirebaseDatabase.getInstance().getReference();
    mUsersRef = mRef.child("Users");
    mChatsRef = mRef.child("Chats");

    try {// this is created so that user authorization is empty(turned off app, signed out to come back)
        mAuth = FirebaseAuth.getInstance();
        mCurrentUser = mAuth.getCurrentUser();

        // if currentUser turned out to be null now Start from login activity
        if (mAuth == null && mCurrentUser == null) {
            Intent i = new Intent(ArtistStartChatActivity.this, LoginActivity.class);
            startActivity(i);
            finish();
        }
    } catch (NullPointerException e) {
        e.printStackTrace();
    }

    // choose either chat or edit profile for Artist option
    button_Artist_startChat = findViewById(R.id.button_Artist_startChat);
    button_Artist_EditProfile = findViewById(R.id.button_Artist_EditProfile);

    //when user clicks to Start Chat, move the page to the Chat activity
    button_Artist_startChat.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent startArtistChat = new Intent(ArtistStartChatActivity.this, Chat.class);
            startActivity(startArtistChat);
            finish();
        }
    });

    //when user clicks to go edit UserProfile, move the page to the UserProfile activity
    button_Artist_EditProfile.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent openArtistProfile = new Intent(ArtistStartChatActivity.this, UserProfileActivity.class);
            startActivity(openArtistProfile);
            finish();
        }
    });
  }

}

Chat.Java

package com.plugmeinapp.plugmeinapp;
import android.content.Intent;
import android.content.Loader;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.constraint.ConstraintLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Date;

import com.firebase.ui.auth.AuthUI;
import com.firebase.ui.database.FirebaseListAdapter;
import com.firebase.ui.database.FirebaseListOptions;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.iid.FirebaseInstanceId;
//import com.google.instrumentation.stats.Tag;


public class Chat extends ArtistStartChatActivity {


private FirebaseAuth mAuth;
private static final int SIGN_IN_REQUEST_CODE = 1;
private FirebaseListAdapter<ChatElements> adapter;
FloatingActionButton fab;




@Override
public boolean onOptionsItemSelected(MenuItem item){
    if (item.getItemId() == R.id.signOut){
        AuthUI.getInstance().signOut(this).addOnCompleteListener(new OnCompleteListener<Void>(){
            @Override
            public  void onComplete(@NonNull Task<Void> task){
                //Snackbar.make(activity_chat,"Signed out",Snackbar.LENGTH_SHORT).show();
                Toast.makeText(Chat.this,"Signed out.",Toast.LENGTH_LONG).show();
                finish();
            }
        });
    }
    return true;
}

@Override
public boolean onCreateOptionsMenu(Menu menu){
    getMenuInflater().inflate(R.menu.menu,menu);
    return true;
}

@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){
            //Snackbar.make(activity_chat, "Welcome!",Snackbar.LENGTH_SHORT).show();
            Toast.makeText(this,"Welcome!",Toast.LENGTH_LONG).show();
            displayChatElements();
        }else{
            //Snackbar.make(activity_chat,"Could not sign in." ,Snackbar.LENGTH_SHORT).show();
            Toast.makeText(this,"Could not sign in.",Toast.LENGTH_LONG).show();
            // Close the app
            finish();
        }
    }

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_chat);    // set to xml file layout

    mAuth = FirebaseAuth.getInstance();

    //FireBase
    //Check if not sign-in then navigation SignIn page
    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
        Toast.makeText(this, "Welcome!" + FirebaseAuth.getInstance().getCurrentUser().getDisplayName(), Toast.LENGTH_LONG).show();
        //Load Chat Content
        displayChatElements();
    }

    FloatingActionButton fab = findViewById(R.id.fab);
    if (fab != null)
    fab.setOnClickListener(new View.OnClickListener() {
        //Get User Data, Send/Receive to Firebase
        @Override
        public void onClick(View view) {
            EditText input = (EditText) findViewById(R.id.input);

            // Read the input field and push a new instance
            // of ChatElement to the Firebase database
            FirebaseDatabase
                    .getInstance()
                    .getReference()
                    .push()
                    //Logcat ERROR Here!!!!!
                    .setValue(new ChatElements(input.getText().toString(), FirebaseAuth.getInstance().getCurrentUser().getDisplayName()));

            // Clear the input
            input.setText("");
        }
    });
}





private void displayChatElements() {
        ListView listOfMessage = (ListView) findViewById(R.id.list_of_messages);



    //Retrieve "chats" in your Firebase DB:
    Query query =     
FirebaseDatabase.getInstance().getReference().child("chats");

//Create FirebaseListOptions
    FirebaseListOptions<ChatElements> options = new FirebaseListOptions.Builder<ChatElements>()
            .setQuery(query, ChatElements.class)
            .setLayout(android.R.layout.activity_list_item)
            .build();


    //Finally you pass them to the constructor here:
    adapter = new FirebaseListAdapter<ChatElements>(options){
            @Override
            protected void populateView(View v, ChatElements model, int position) {
                //get references to the view of chat_list_item.xml
                TextView messageText, messageUser, messageTime;
                messageText = (TextView) v.findViewById(R.id.message_text);
                messageUser = (TextView) v.findViewById(R.id.message_user);
                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(android.text.format.DateFormat.format("dd-MM-yyyy (HH:mm:ss)", model.getMessageTime()));
            }
        };
        listOfMessage.setAdapter(adapter);
}

@Override
public void onStart() {
    super.onStart();
    adapter.startListening();

}

@Override
protected void onStop() {
    super.onStop();
    adapter.stopListening();
  }
}

Build.gradle(app)

apply plugin: 'com.android.application'

android {

compileSdkVersion 27
defaultConfig {
    applicationId "com.plugmeinapp.plugmeinapp"
    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(include: ['*.jar'], dir: 'libs')
//implementation 'com.android.support:appcompat-v7:27.1.1'//Chat Messaging
//implementation 'com.android.support:design:27.1.1' //Chat Messaging
//implementation 'com.android.support:cardview-v7:27.1.1' //Chat Messaging
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
implementation 'com.google.firebase:firebase-messaging:17.1.0'
//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'
implementation 'com.google.firebase:firebase-core:16.0.1'
implementation 'com.google.firebase:firebase-auth:16.0.2'
implementation 'com.google.firebase:firebase-firestore:17.0.4'
implementation 'com.firebaseui:firebase-ui-database:4.0.0'
implementation 'com.firebaseui:firebase-ui-auth:4.0.0'
implementation 'com.firebaseui:firebase-ui:4.0.0'
implementation 'de.hdodenhof:circleimageview:2.2.0'
implementation 'com.squareup.picasso:picasso:2.71828'
implementation 'com.google.android.gms:play-services-auth:15.0.1'


}

apply plugin: 'com.google.gms.google-services'

LogCat

08-11 13:41:19.726 9209-9209/com.plugmeinapp.plugmeinapp D/AndroidRuntime: Shutting down VM
08-11 13:41:19.728 9209-9209/com.plugmeinapp.plugmeinapp E/AndroidRuntime:   FATAL EXCEPTION: main
Process: com.plugmeinapp.plugmeinapp, PID: 9209
com.google.firebase.database.DatabaseException: Found conflicting getters for name: isChangingConfigurations
    at com.google.android.gms.internal.firebase_database.zzku.<init>(Unknown Source)
    at com.google.android.gms.internal.firebase_database.zzkt.zza(Unknown Source)
    at com.google.android.gms.internal.firebase_database.zzkt.zzi(Unknown Source)
    at com.google.android.gms.internal.firebase_database.zzkt.zzh(Unknown Source)
    at com.google.firebase.database.DatabaseReference.zza(Unknown Source)
    at com.google.firebase.database.DatabaseReference.setValue(Unknown Source)
    at com.plugmeinapp.plugmeinapp.Chat$2.onClick(Chat.java:120) //Chat.Java File
    at android.view.View.performClick(View.java:5624)
    at android.view.View$PerformClick.run(View.java:22441)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6316)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
08-11 13:46:11.078 9209-10777/com.plugmeinapp.plugmeinapp I/FirebaseAuth: 
[FirebaseAuth:] Loading module via FirebaseOptions.
08-11 13:46:11.080 9209-10777/com.plugmeinapp.plugmeinapp I/FirebaseAuth: 
[FirebaseAuth:] Preparing to create service connection to gms implementation

ChatElements.java

package com.plugmeinapp.plugmeinapp;

import java.util.Date;

public class ChatElements extends Chat {

private String messageText;
private String messageUser;
private long messageTime;

public ChatElements(String messageText, String messageUser) {
    this.messageText = messageText;
    this.messageUser = messageUser;

    // Initialize to current time
    messageTime = new Date().getTime();
}


public ChatElements() {
}

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;
}

1 个答案:

答案 0 :(得分:0)

这些是您的项目所需的库:

p = Popen(["crontab -e"], stdin=PIPE, stdout=PIPE, shell=True)
p.stdin.write("@reboot python test.py")

然后将implementation 'com.android.support:appcompat-v7:27.1.1' implementation 'com.android.support:design:27.1.1' implementation 'com.android.support:cardview-v7:27.1.1' 更改为apply plugin