Android应用程式当机,并在rooturl上显示错误

时间:2018-10-16 20:23:58

标签: android firebase

public class Constant {
    public static final String FIREBASE_CHAT_URL="https://roadcaremap.firebaseio.com/";
    public static final String CHILD_USERS="users";
    public static final String CHILD_CHAT="chat";
    public static final String KEY_SEND_USER="key_send_user";
    public static final String CHILD_CONNECTION="connecttion";
    public static final String CHILD_LATITUDE="latitude";
    public static final String CHILD_LONGITUDE="longitude";
    public static final String KEY_EMAIL="email";
    public static final String KEY_ONLINE="online";
    public static final String KEY_OFFLINE="offline";
    public static final String KEY_CLOSE="key_close";
}

我是Android新手。最近,我在设备上运行应用程序时遇到问题。应用程式当机,并在这行中显示错误讯息,我不知道该如何解决。我添加了logcat的代码和图片。任何帮助将非常感激。错误显示在此行中。

  

rootUrl =新的Firebase(Constant.FIREBASE_CHAT_URL);

package com.snapsofts.demogmap.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.firebase.client.AuthData;
import com.firebase.client.ChildEventListener;
import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.client.ValueEventListener;
import com.firebase.ui.auth.AuthUI;
import com.google.firebase.auth.FirebaseAuth;
import com.google.gson.Gson;
import com.snapsofts.demogmap.R;
import com.snapsofts.demogmap.Services.LocationService;
import com.snapsofts.demogmap.common.Constant;
import com.snapsofts.demogmap.object.User;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity  {
    private Firebase rootUrl;
    private Firebase urlCurrenUser;
    private Firebase urlAllUser;
    private FirebaseAuth mAuth;

    private Firebase.AuthStateListener mAuthStateListener;
    private String currenUserId;
    private String currenUserEmail;
    private ArrayList<User> arrUser;
    private AllUserAdapter allUserAdapter;
    private ArrayList<String> arrStringEmail;
    private  ValueEventListener valueEventListenerUserConnected;
    private User currenUser;
    @BindView(R.id.btnLogout)
    Button btnLogout;
    @BindView(R.id.lvUser)
    ListView lvUser;
    @BindView(R.id.tvUsserName)
    TextView tvUsserName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        MainActivity.this.startService(new Intent(MainActivity.this, LocationService.class));
        arrStringEmail = new ArrayList<>();
        arrUser = new ArrayList<User>();
        allUserAdapter = new AllUserAdapter(MainActivity.this, 0, arrUser);
        lvUser.setAdapter(allUserAdapter);
        lvUser.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(MainActivity.this,MapsActivity.class);
                User user=arrUser.get(position);
                Gson gson=new Gson();
                intent.putExtra(Constant.KEY_SEND_USER,gson.toJson(user).toString()+"---"+gson.toJson(currenUser).toString());
                startActivity(intent);

                mAuth = FirebaseAuth.getInstance();
            }
        });
        rootUrl = new Firebase(Constant.FIREBASE_CHAT_URL);
        mAuthStateListener = new Firebase.AuthStateListener() {
            @Override
            public void onAuthStateChanged(AuthData authData) {
                setAuthenticatedUser();
            }
        };
        rootUrl.addAuthStateListener(mAuthStateListener);
    }

    private void setAuthenticatedUser () {

        if (FirebaseAuth.getInstance().getCurrentUser() != null) {
            currenUserId = FirebaseAuth.getInstance().getCurrentUser().getUid();
            currenUserEmail = FirebaseAuth.getInstance().getCurrentUser().getEmail();
            getCurrenUser();
            getAllUser();
        } else {
            startActivity(new Intent(MainActivity.this, LoginActivity.class));
            finish();
        }
    }

    public void getCurrenUser() {
        urlCurrenUser = new Firebase(Constant.FIREBASE_CHAT_URL).child(Constant.CHILD_USERS).child(FirebaseAuth.getInstance().getCurrentUser().getUid());
        urlCurrenUser.addValueEventListener(valueEventListenerCurrenUser);
        valueEventListenerUserConnected=rootUrl.getRoot().child(".info/connected").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                boolean connected = (Boolean) dataSnapshot.getValue();
                if (connected) {
                    urlCurrenUser.child(Constant.CHILD_CONNECTION).setValue(Constant.KEY_ONLINE);
                    urlCurrenUser.child(Constant.CHILD_CONNECTION).onDisconnect().setValue(Constant.KEY_OFFLINE);
                }
            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {

            }
        });
    }
    private ValueEventListener valueEventListenerCurrenUser = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            User user = dataSnapshot.getValue(User.class);
            tvUsserName.setText("Hello "+user.name);
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    };
    public void getAllUser() {
        urlAllUser = new Firebase(Constant.FIREBASE_CHAT_URL).child(Constant.CHILD_USERS);
        urlAllUser.addChildEventListener(childEventListenerAllUser);
    }
    private ChildEventListener childEventListenerAllUser = new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            User user = dataSnapshot.getValue(User.class);
            if (!dataSnapshot.getKey().equals(currenUserId)){
                arrStringEmail.add(user.email);
                arrUser.add(user);
                allUserAdapter.notifyDataSetChanged();
            }else {
                currenUser=user;
            }
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
            if (!dataSnapshot.getKey().equals(currenUserId)){
                User user = dataSnapshot.getValue(User.class);
                int index = arrStringEmail.indexOf(user.email);
                arrUser.set(index, user);
                allUserAdapter.notifyDataSetChanged();
            }
        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    };
    @Override
    protected void onDestroy() {
        super.onDestroy();
        try {
            rootUrl.removeAuthStateListener(mAuthStateListener);
        } catch (Exception e) {
        }
        try {
            urlCurrenUser.removeEventListener(valueEventListenerCurrenUser);
        } catch (Exception e) {
        }
        try {
            urlAllUser.removeEventListener(childEventListenerAllUser);
        } catch (Exception e) {
        }
        try {
            rootUrl.getRoot().child(".info/connected").removeEventListener(valueEventListenerUserConnected);
        }catch (Exception e){}
    }

    @OnClick(R.id.btnLogout)
    public void btnLogout() {
        if (FirebaseAuth.getInstance().getCurrentUser() != null) {
            stopService(new Intent(this, LocationService.class));
            AuthUI.getInstance().signOut(this);
            Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
            startActivity(intent);
            finish();

        }
    }


    public class AllUserAdapter extends ArrayAdapter<User> {
        private Activity mActivity;
        private ArrayList<User> mArrUser;
        @BindView(R.id.tvNameUser)
        TextView tvNameUser;
        @BindView(R.id.tvStatus)
        TextView tvStatus;

        public AllUserAdapter(Activity mActivity, int resource, ArrayList<User> mArrUser) {
            super(mActivity, resource, mArrUser);
            this.mActivity = mActivity;
            this.mArrUser = mArrUser;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                LayoutInflater layoutInflater = mActivity.getLayoutInflater();
                convertView = layoutInflater.inflate(R.layout.item_list_user, null);
            }
            ButterKnife.bind(this, convertView);
            tvNameUser.setText(mArrUser.get(position).name);
            tvStatus.setText(mArrUser.get(position).connecttion);
            if (mArrUser.get(position).connecttion.equals(Constant.KEY_ONLINE)){
                tvStatus.setTextColor(Color.parseColor("#00FF00"));
            }else {
                tvStatus.setTextColor(Color.parseColor("#FF0000"));
            }
            return convertView;
        }
    }

}

logcat

0 个答案:

没有答案