无法完成活动 [英] Can´t finish Activity

查看:97
本文介绍了无法完成活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用className.this.finish和finishActivity()调用活动的完成过程,并使用以下方法初始化了活动-startActivityForResult()或finishAndRemoveTask()

I have tried className.this.finish and finishActivity() to invoke the finishing of my activity and I have initialised the activity with the following methods - startActivityForResult() or finishAndRemoveTask()

此活动的建议是注册一个设备,这是在设备"/(设备的MAC地址)/用户"/用户"下保存的,最多可存储4个不同用户的4个不同的通知令牌.所以我想做的是,要检查数字1是否具有信息,检查2是否具有信息,检查3,是否具有信息,检查4是否具有信息,该应用程序会显示一条消息,说您已达到最大用户数."但是,如果有一个可用号码,则它应该保存该号码,并且仅在该号码中保存通知令牌.

The propose of this activity is to register a device this is done when under Devices/(MAC address of the device)/Users/ it save in fire base up to 4 different notification tokens of 4 users different. So what I'm trying to do but it falling is to check if the number 1 have info, check 2 if have info, check 3 and if this have info, check the 4 if all have info the app show a message saying "you have reached the maximum number of users".But if one number available it suppose to save in that number and only in that number the notification token.

正在发生的事情是,当我单击按钮时,循环永远不会结束.它向四个用户发送相同的通知令牌,显示提示您已达到最大用户数",然后返回到该活动之前的活动,但仍在执行注册代码.我知道这是因为,如果我在Firebase中删除了一个用户,它会立即重新发送信息,并且您会再次看到消息您已经达到最大用户数"

What it is happening is that when I click the button the loop never end. it send the same notification token for the four users show the mensage "you have reached the maximum number of users" and then it come back to the activity before this one but it still executing the registration code. I know this because if I deleted an user in firebase it immediately resend the info and you can see the message "you have reached the maximum number of users" again

这是发送信息的活动的代码:

this is the code of the activity that send the info:

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

    fAuth = FirebaseAuth.getInstance();
    entr = findViewById(R.id.AddButton);
    mDatabase = FirebaseDatabase.getInstance().getReference();
    sDatabase = FirebaseDatabase.getInstance().getReference();
    aDatabase = FirebaseDatabase.getInstance().getReference();
    MAC =findViewById(R.id.macCerca);
    NOM = findViewById(R.id.momCerca);
    configured = false;
    getUserProfile();
    getNotificationId();

    entr.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            MacCerca = MAC.getText().toString().toUpperCase();
            NombreCerca = NOM.getText().toString();
            //mDatabase.child("Devices").child(MacCerca).child("Id").setValue("AD:23");
            mDatabase.child("Devices").child(MacCerca).child("Users").addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    String user1 = (String) dataSnapshot.child("1").getValue();
                    String user2 = (String) dataSnapshot.child("2").getValue();
                    String user3 = (String) dataSnapshot.child("3").getValue();
                    String user4 = (String) dataSnapshot.child("4").getValue();
                    if(user1 == null){
                        aDatabase.child("Devices").child(MacCerca).child("Users").child("1").setValue(token);
                        aDatabase.child("Devices").child(MacCerca).child("NombreCerca").setValue(NombreCerca);
                        aDatabase.child("Users").child(mail).child("Device").setValue(MacCerca);
                        AddCerca.this.finish();
                    }else if(user2 ==null){
                        aDatabase.child("Devices").child(MacCerca).child("Users").child("2").setValue(token);
                        aDatabase.child("Devices").child(MacCerca).child("NombreCerca").setValue(NombreCerca);
                        aDatabase.child("Users").child(mail).child("Device").setValue(MacCerca);
                        AddCerca.this.finish();
                    }else if(user3 ==null){
                        aDatabase.child("Devices").child(MacCerca).child("Users").child("3").setValue(token);
                        aDatabase.child("Devices").child(MacCerca).child("NombreCerca").setValue(NombreCerca);
                        aDatabase.child("Users").child(mail).child("Device").setValue(MacCerca);
                        AddCerca.this.finish();
                    }else if(user4 ==null){
                        aDatabase.child("Devices").child(MacCerca).child("Users").child("4").setValue(token);
                        aDatabase.child("Devices").child(MacCerca).child("NombreCerca").setValue(NombreCerca);
                        aDatabase.child("Users").child(mail).child("Device").setValue(MacCerca);
                        AddCerca.this.finish();
                    }else{
                        Toast.makeText(AddCerca.this, "Límite de usuarios registrados exedidos", Toast.LENGTH_SHORT).show();
                        AddCerca.this.finish();
                    }
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });
        }
    });
}

我创建这样的活动:

startActivity(new Intent(CercaElectrica.this, AddCerca.class));

Firebase看起来像这样:

Firebase look like this:

如果它仅存在于用户1中,或者如果用户1有一些数据,则必须更新用户2并用3和4来更新用户

When it suposed to be only in the user 1, or if user 1 have some data it have to update user 2 and the same with 3 and 4

推荐答案

我发现了错误.插入:

mDatabase.child("Devices").child(MacCerca).child("Users").addValueEventListener(new ValueEventListener() {

更改为:

mDatabase.child("Devices").child(MacCerca).child("Users").addListenerForSingleValueEvent(new ValueEventListener() {

这篇关于无法完成活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆