Android工作室中的Firebase聊天应用程序

时间:2017-06-16 23:37:51

标签: java android android-manifest firebase-authentication firebase-cloud-messaging

在Android工作室我使用Fire-base创建一个消息传递应用程序,但当我启动我的应用程序时,它显示一个main_activity一秒钟,然后显示登录页面,当我成功登录应用程序崩溃我该怎么办请帮助我的代码是完美的我认为我的清单中有黑色的东西请帮助我解决这个问题..

这是最清晰的代码......

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.syedqadri.message">

     <uses-permission android:name="android.permission.INTERNET" />

        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher_icon"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
                 </activity>


        </application>

    </manifest>

这是我的毕业生(项目)

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.2'
        classpath 'com.google.gms:google-services:3.1.0'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

这是我的Gradle(app)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.3"

    defaultConfig {
        applicationId "com.example.syedqadri.message"
        minSdkVersion 23
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support:design:25.3.1'
    compile 'com.firebaseui:firebase-ui:0.6.2'



    // compile 'com.google.android.gms:play-services-location:9.2.0'
    //compile 'com.google.firebase:firebase-core:9.2.0'
    //compile 'com.google.firebase:firebase-auth:9.2.0'
    //compile 'com.google.firebase:firebase-messaging:9.2.0'
   // compile 'com.android.support:multidex:1.0.1'

}

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

这是我的java MainActivity.java

package com.example.syedqadri.message;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
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.RelativeLayout;
import android.widget.TextView;
import android.text.format.DateFormat;

import com.firebase.ui.auth.AuthUI;
import com.firebase.ui.database.FirebaseListAdapter;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.FirebaseDatabase;

public class MainActivity extends AppCompatActivity {

    private static int SIGN_IN_REQUEST_CODE = 1;
    private FirebaseListAdapter<chatmessage> adapter;
    RelativeLayout activity_main;
    FloatingActionButton sent;


    @Override
    public boolean onOptionsItemSelected(MenuItem item){

        if(item.getItemId() == R.id.menu_signout){
            AuthUI.getInstance().signOut(this).addOnCompleteListener(new OnCompleteListener<Void>(){

                @Override
                public void onComplete(@NonNull Task<Void> task){

                    Snackbar.make(activity_main, "You Have Been Sign 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 == SIGN_IN_REQUEST_CODE){

            if(resultCode == RESULT_OK){
                Snackbar.make(activity_main, "Signing in SucessFully.Welcome ...",Snackbar.LENGTH_SHORT).show();
                displaychatmessage();

            }
            else{
                Snackbar.make(activity_main, "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);
        sent =   (FloatingActionButton)findViewById(R.id.sent);
        sent.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("");
                input.requestFocus();
              //  displaychatmessage();
            }
        });

        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();
            displaychatmessage();
        }
        //load content
        displaychatmessage();
    }

    private void displaychatmessage() {

        ListView listOfMessage = (ListView)findViewById(R.id.lst_message);
        adapter =  new FirebaseListAdapter<chatmessage>(this,chatmessage.class,R.layout.list,FirebaseDatabase.getInstance().getReference()) {


            @Override
            protected void populateView(View v, chatmessage model, int position) {
                //get reference to the list view items.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);

                messageText.setText(model.getMessaegeText());
                messageUser.setText(model.getMessageUser());
                messageTime.setText(DateFormat.format("dd-MM-yyyy (HH:mm:ss)",model.getMessageTime()));

            }
        };

        listOfMessage.setAdapter(adapter);
    }
}

这是我的Chatmessage.java

package com.example.syedqadri.message;

import java.util.Date;

/**
 * Created by Syed Qadri on 09-Jun-17.
 */
public class chatmessage {

    private String messaegeText;
    private  String messageUser;
    private long messageTime;

    public chatmessage(String messaegeText, String messageUser) {
       this.messaegeText = messaegeText;
       this.messageUser = messageUser;

        messageTime = new Date().getTime();
    }
    public chatmessage() {
    }
    public String getMessaegeText() {
        return messaegeText;
    }

    public void setMessaegeText(String messaegeText) {
        this.messaegeText = messaegeText;
    }

    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)

嗯..我想您忘了在清单文件中添加 Internet权限 将此行放在您的应用程序标记上方:

<uses-permission android:name="android.permission.INTERNET"/>