Firebase后台通知崩溃

时间:2018-07-28 09:47:29

标签: java android firebase nullpointerexception notifications

我正在使用Firebase构建聊天应用程序,其中我正在使用功能功能日志在android中显示通知。我创建了一个Reqactivity,如果某人向某人发送请求,该请求显示在他的同人android中,则如果某人单击问题是,如果这个小伙子android在前台运行该应用程序,并且他单击它,如果他单击后台通知应用程序由于错误而崩溃,那么如果他在后台运行,它将带他进入Reqactivity。 / p>

  

java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.aquib.chatapp / com.example.aquib.chatapp.ProfileActivity}:java.lang.NullPointerException:println需要消息

这是我出错的地方

at com.example.aquib.chatapp.ProfileActivity.onCreate(ProfileActivity.java:64)

这是活动代码

     //MessagingService Activity
     String notification_click = remoteMessage.getNotification().getClickAction();
    String notification_data = remoteMessage.getData().get("from_userId");
    Intent intent = new Intent(notification_click);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    intent.putExtra("user_id",notification_data);

    //getting it over here ProfileActivity
    final String User_Id = getIntent().getStringExtra("user_id");
    currentUser = FirebaseAuth.getInstance().getCurrentUser();
    progressDialog = new ProgressDialog(ProfileActivity.this);
    databaseReference = FirebaseDatabase.getInstance().getReference().child("Users").child(User_Id);
    databaseFriend =FirebaseDatabase.getInstance().getReference().child("Friends");
    databaseReq = FirebaseDatabase.getInstance().getReference().child("Friend_Req");
    databaseNotification = FirebaseDatabase.getInstance().getReference().child("Notification");

//updated
public class ProfileActivity extends AppCompatActivity {

DatabaseReference onData;
FirebaseAuth auth;
DatabaseReference databaseReq;
DatabaseReference databaseFriend;
DatabaseReference databaseReference;
DatabaseReference databaseNotification;
DatabaseReference mRootref;
ProgressDialog progressDialog;
FirebaseUser currentUser;
String mCurrentState;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);

    auth = FirebaseAuth.getInstance();
    onData = FirebaseDatabase.getInstance().getReference().child("Users").child(auth.getCurrentUser().getUid());

    final String User_Id = getIntent().getStringExtra("user_id");

    mCurrentState = "not_friends";
    final Button Decbutton = findViewById(R.id.button5);
    final Button Reqbutton = findViewById(R.id.button4);
    final TextView user_name = findViewById(R.id.textView5);
    final TextView status_name = findViewById(R.id.textView6);
    final ImageView profile_image = findViewById(R.id.imageView2);

    currentUser = FirebaseAuth.getInstance().getCurrentUser();
    progressDialog = new ProgressDialog(ProfileActivity.this);

        databaseReference = FirebaseDatabase.getInstance().getReference().child("Users").child(User_Id);
        databaseFriend = FirebaseDatabase.getInstance().getReference().child("Friends");
        databaseReq = FirebaseDatabase.getInstance().getReference().child("Friend_Req");
        databaseNotification = FirebaseDatabase.getInstance().getReference().child("Notification");
        mRootref = FirebaseDatabase.getInstance().getReference();
        progressDialog.setTitle("Loading User Data");
        progressDialog.setMessage("Please wait while we load the data");
        progressDialog.setCanceledOnTouchOutside(false);
        progressDialog.show();

        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                String user = dataSnapshot.child("user").getValue().toString();
                String status = dataSnapshot.child("status").getValue().toString();
                String image = dataSnapshot.child("image").getValue().toString();

                user_name.setText(user);
                status_name.setText(status);

                Picasso.with(ProfileActivity.this).load(image).into(profile_image);

                // -------------------- Friend List / Request Feature--------------------

                databaseReq.child(currentUser.getUid()).addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {

                        if (dataSnapshot.hasChild(User_Id)) {

                            String req_type = dataSnapshot.child(User_Id).child("request_type").getValue().toString();

                            if (req_type.equals("received")) {

                                Reqbutton.setText("Accept Friend Request");
                                mCurrentState = "req_received";
                                Decbutton.setVisibility(View.VISIBLE);

                            } else if (req_type.equals("sent")) {

                                mCurrentState = "req_sent";
                                Reqbutton.setText("Cancel Friend Request");
                                Decbutton.setVisibility(View.INVISIBLE);

                            }

                            progressDialog.dismiss();


                        } else {

                            databaseFriend.child(currentUser.getUid()).addListenerForSingleValueEvent(new ValueEventListener() {
                                @Override
                                public void onDataChange(DataSnapshot dataSnapshot) {

                                    if (dataSnapshot.hasChild(User_Id)) {

                                        Reqbutton.setText("Unfriend this Person");
                                        mCurrentState = "friends";
                                        Decbutton.setVisibility(View.INVISIBLE);

                                    }

                                    progressDialog.dismiss();
                                }

                                @Override
                                public void onCancelled(DatabaseError databaseError) {

                                }
                            });

                        }

                        progressDialog.dismiss();

                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

        // -------------------- Friend Request--------------------

        Reqbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Reqbutton.setEnabled(false);

                // -------------------- Not Friend State --------------------

                if (mCurrentState.equals("not_friends")) {

                    DatabaseReference newNotifref = mRootref.child("Notification").child(User_Id).push();
                    String newNotifId = newNotifref.getKey();

                    HashMap<String, String> hashMap = new HashMap<>();
                    hashMap.put("from", currentUser.getUid());
                    hashMap.put("type", "request");


                    Map requestMap = new HashMap();
                    requestMap.put("Friend_Req/" + currentUser.getUid() + "/" + User_Id + "/" + "request_type", "sent");
                    requestMap.put("Friend_Req/" + User_Id + "/" + currentUser.getUid() + "/" + "request_type", "received");
                    requestMap.put("Notification/" + User_Id + "/" + newNotifId, hashMap);

                    mRootref.updateChildren(requestMap, new DatabaseReference.CompletionListener() {
                        @Override
                        public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {

                            if (databaseError == null) {

                                Reqbutton.setEnabled(true);
                                Reqbutton.setText("Cancel Friend Request");
                                Reqbutton.setBackground(getResources().getDrawable(R.color.colorBtn));
                                mCurrentState = "req_sent";
                                Decbutton.setVisibility(View.INVISIBLE);


                            } else {

                                Toast.makeText(ProfileActivity.this, "There was some error in sending request", Toast.LENGTH_SHORT).show();

                            }
                            Reqbutton.setEnabled(true);
                        }
                    });
                }

                // -------------------- Cancel Friend Request--------------------

                if (mCurrentState.equals("req_sent")) {

                    Map map = new HashMap();
                    map.put("Friend_Req/" + currentUser.getUid() + "/" + User_Id, null);
                    map.put("Friend_Req/" + User_Id + "/" + currentUser.getUid(), null);

                    mRootref.updateChildren(map, new DatabaseReference.CompletionListener() {
                        @Override
                        public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {

                            if (databaseError == null) {
                                Reqbutton.setEnabled(true);
                                Reqbutton.setText("Send Friend Request");
                                mCurrentState = "not_friends";
                                Decbutton.setVisibility(View.INVISIBLE);

                            } else {
                                Toast.makeText(ProfileActivity.this, "Error in doing it", Toast.LENGTH_SHORT).show();
                            }

                        }
                    });


                // -------------------- Req Received State --------------------

                if (mCurrentState.equals("req_received")) {

                    final String currentDate = DateFormat.getDateInstance().format(new Date());

                    Map map = new HashMap();
                    map.put("Friends/" + currentUser.getUid() + "/" + User_Id + "/date", currentDate);
                    map.put("Friends/" + User_Id + "/" + currentUser.getUid() + "/date", currentDate);
                    map.put("Friend_Req/" + currentUser.getUid() + "/" + User_Id, null);
                    map.put("Friend_Req/" + User_Id + "/" + currentUser.getUid(), null);

                    mRootref.updateChildren(map, new DatabaseReference.CompletionListener() {
                        @Override
                        public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {

                            if (databaseError == null) {

                                Reqbutton.setText("Unfriend this Person");
                                Reqbutton.setEnabled(true);
                                mCurrentState = "friends";
                                Decbutton.setVisibility(View.INVISIBLE);


                            } else {

                                Toast.makeText(ProfileActivity.this, "Error in Receiving", Toast.LENGTH_SHORT).show();

                            }

                        }
                    });

                //----------------- UnFriend ----------------------

                if (mCurrentState.equals("friends")) {

                    Map map = new HashMap();

                    map.put("Friends/" + currentUser.getUid() + "/" + User_Id, null);
                    map.put("Friends/" + User_Id + "/" + currentUser.getUid(), null);

                    mRootref.updateChildren(map, new DatabaseReference.CompletionListener() {
                        @Override
                        public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {

                            if (databaseError == null) {

                                mCurrentState = "not_friends";
                                Reqbutton.setEnabled(true);
                                Reqbutton.setText("Send Friend Request");
                                Decbutton.setVisibility(View.INVISIBLE);


                            } else {

                                Toast.makeText(ProfileActivity.this, databaseError.getMessage().toString(), Toast.LENGTH_SHORT).show();

                            }

                        }
                    });

                }

            }
        });

        Decbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Map map = new HashMap();
                map.put("Friend_Req/" + currentUser.getUid() + "/" + User_Id, null);
                map.put("Friend_Req/" + User_Id + "/" + currentUser.getUid(), null);

                mRootref.updateChildren(map, new DatabaseReference.CompletionListener() {
                    @Override
                    public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {

                        if (databaseError == null) {

                            Decbutton.setVisibility(View.INVISIBLE);
                            Reqbutton.setText("Send Friend Request");
                            mCurrentState = "not_friends";

                        } else {

                            Toast.makeText(ProfileActivity.this, "Error in the dec btn", Toast.LENGTH_SHORT).show();

                        }

                    }
                });

            }
        });

}

}

1 个答案:

答案 0 :(得分:0)

  

java.lang.NullPointerException: println需要消息

您可能需要将一条消息设置为println,例如以下代码:

System.out.println("Your message");

但是,这不是重点。那是纯Java,我们使用println作为。 最好使用登录android开发。 (您也可以同时使用)

Write log messages in your code