简体   繁体   English

如何防止android studio跳过活动?

[英]How to prevent android studio from skipping activities?

I have a quiz app that will save the score on firebase. 我有一个测验应用程序,可以将分数保存在Firebase上。 It works properly. 它正常工作。 But whenever I try to retake the quiz, it will just show the first activity, and then skip all the activity after it and proceed to the final activity where it shows the output. 但是,每当我尝试重新参加测验时,它只会显示第一个活动,然后跳过其后的所有活动,并继续到显示输出的最后一个活动。 I checked everything, the IDs are all unique. 我检查了所有内容,这些ID都是唯一的。 I can't find where I went wrong. 我找不到哪里出了问题。

This is the first part of my quiz. 这是我测验的第一部分。 When I retake the quiz, the scores will reset to 0. It works properly. 当我重新参加测验时,分数将重置为0。它可以正常工作。 But when I click next, it will just proceed to the final activity where the output is shown. 但是,当我单击“下一步”时,它将直接进入显示输出的最终活动。

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

firebaseAuth = FirebaseAuth.getInstance();
firebaseDatabase = FirebaseDatabase.getInstance();

phqChoice1 = (RadioButton) findViewById(R.id.phqChoice1);
phqChoice2 = (RadioButton) findViewById(R.id.phqChoice2);
phqChoice3 = (RadioButton) findViewById(R.id.phqChoice3);
phqChoice4 = (RadioButton) findViewById(R.id.phqChoice4);
btnNext = (Button) findViewById(R.id.btnNext);

getAnswer();

final DatabaseReference myRef = firebaseDatabase.getReference(firebaseAuth.getUid()).child("test");

if (myRef != null) {
    myRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            HashMap<String, Object> result = new HashMap<>();
            result.put("anx", 0);
            result.put("dep", 0);
            myRef.updateChildren(result);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            System.out.println("The read failed: " + databaseError.getMessage());
        }
    });


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

            myRef.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    UserHistory userHistory = new UserHistory();
                    userHistory.setAnx(anx);
                    userHistory.setDep(dep);
                    myRef.setValue(userHistory);


                    Intent myIntent = new Intent(Part1.this, Part2.class);
                    startActivity(myIntent);
                    finish();
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {
                    System.out.println("Entering data failed. " + databaseError.getMessage());
                }
            });


        }
    });
}}

 private void getAnswer() {
radioGroup1 = (RadioGroup) findViewById(R.id.radioGroup1);
radioGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (checkedId == R.id.phqChoice2) {
            dep++;
        } else if (checkedId == R.id.phqChoice3) {
            dep = dep + 2;
        } else if (checkedId == R.id.phqChoice4) {
            dep = dep + 3;
        }
    }
});}

This is the part2 activity where android skips these activities and will just proceed to the final activity if I retake the quiz. 这是第2部分活动,其中android跳过这些活动,如果我重新参加测验,它将继续进行最后的活动。

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


    firebaseAuth = FirebaseAuth.getInstance();
    firebaseDatabase = FirebaseDatabase.getInstance();

    btnNext = (Button)findViewById(R.id.btnPart2);



    DatabaseReference myRef = firebaseDatabase.getReference(firebaseAuth.getUid()).child("test");
    myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            UserHistory userHistory = dataSnapshot.getValue(UserHistory.class);
            anxHistory = userHistory.getAnx();
            depHistory = userHistory.getDep();

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            Toast.makeText(Part2.this, databaseError.getCode(), Toast.LENGTH_SHORT).show();
        }
    });

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

            getAnswer();

                    anxValue = anxHistory + anx;

                    final DatabaseReference myRef = firebaseDatabase.getReference(firebaseAuth.getUid()).child("test");

                    myRef.addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                            HashMap<String, Object> result = new HashMap<>();
                            result.put("anx", anxValue);
                            myRef.updateChildren(result);
                        }

                        @Override
                        public void onCancelled(@NonNull DatabaseError databaseError) {
                            System.out.println("The read failed: " + databaseError.getMessage());
                        }
                    });


                Intent part3A = new Intent(Part2.this, Part3.class);
                startActivity(part3A);

        }
    });
}

private void getAnswer() {
    radioGroup2 = (RadioGroup) findViewById(R.id.radioGroup2);
    int selectedItem = radioGroup2.getCheckedRadioButtonId();
    if (selectedItem == R.id.part2Choice2) {
        anx++ ;
    } else if (selectedItem == R.id.part2Choice3)  {
        anx = anx + 2 ;
    }
    else if (selectedItem == R.id.part2Choice4) {
        anx = anx + 3;
    }else {
        anx = 0;
    }

        }

}

I expect the output to proceed as normally whenever I retake the quiz again. 我希望每当我再次参加测验时,输出就可以照常进行。 All activities should proceed as usual and no activity should be skipped. 所有活动均应照常进行,并且不应跳过任何活动。 What actually happens is the first activity is working properly but all the other activities that are related to the quiz ends up being skipped. 实际发生的是第一个活动正常运行,但是与测验有关的所有其他活动最终被跳过。 Please help. 请帮忙。 I am clueless and I don't know what else should I check. 我一无所知,我不知道该检查些什么。

In addition, if I click the radio button A (value is 0), the activity will just continue as usual. 此外,如果单击单选按钮A(值为0),则该活动将照常继续。 If I click other buttons which has a value that is greater than 0, (Value is 1-3), and then the activity will just skip to the final activity wherein the output is displayed. 如果我单击值大于0的其他按钮(值是1-3),则该活动将跳到显示输出的最后一个活动。

To simplify: Click Radio button A (0) > Next activity, Click Radio Button A(0), > Next Activity (Proceeds as normal.). 为简化起见:单击单选按钮A(0)>下一个活动,单击单选按钮A(0),>下一个活动(正常进行。)。 OR Click Radio button B (1) > Skips other activity, proceeds to the final activity. 或单击单选按钮B(1)>跳过其他活动,进入最后的活动。

try to put the Intent outside the btnNext,onClickListener method 尝试将Intent放在btnNext,onClickListener方法之外

Intent part3A = new Intent(Part2.this, Part3.class);
btnNext.setOnClcikListener(){
//here call your intent from Part2

startActivity(part3A);
}

I put the intent activity inside the ValueEventListener so whenever I try to enter new data, it will skip to the Final activity. 我将意图活动放在ValueEventListener内,因此每当我尝试输入新数据时,它都会跳到Final活动。 The fix: 解决方法:

  myRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                UserHistory userHistory = new UserHistory();
                userHistory.setAnx(anx);
                userHistory.setDep(dep);
                myRef.setValue(userHistory);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                System.out.println("Entering data failed. " + databaseError.getMessage());
            }
        });

Intent myIntent = new Intent(Part1.this, Part2.class);
                startActivity(myIntent);
                finish();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM