将整数传递给其他活动失败

时间:2018-02-23 08:04:50

标签: android android-intent android-activity

我有一个测验应用程序。用户选择一个答案,然后单击"验证"按钮(在我的程序中是button13)。如果答案是错误的,它会将其重定向到另一个显示悲伤面部的意图;当他点击悲伤的脸(实际上是一个带有图像作为背景的按钮)时,它会将他返回到上一个活动。问题是他被允许只有3次机会,之后程序将关闭。我试图实现这一点,但我不知道我错在哪里......

我在主要活动中宣布了一个全局变量:

 ['k', 'l', 'm', 'n', 's', 's', 'b', 'n', 'b', 'n', 's', 'b', 'n', 'b', 's']
 ['k', 'l', 'm', 's', 'b', 'b', 'n', 's', 'n', 's', 'b', 'n', 's', 'n', 'b']

然后,这是主要活动的按钮:

int contor;

这是第二项活动:

final Button button13 = (Button) findViewById(R.id.button13);

button13.setOnClickListener(new  View.OnClickListener() {

        @Override
        public void onClick(View v) {

//....checking the answer....

//if the answer is wrong
               if(contor == 3) //if the user has reached the maximum available answers, then exit

                    {
                        Intent intent = new Intent(Intent.ACTION_MAIN);
                        intent.addCategory(Intent.CATEGORY_HOME);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                    }

                        else
                    {
                        contor = contor + 1;

                        Intent intent = new Intent(FirstGame1.this, TryAgain.class);
                        intent.putExtra("integerNumber", contor);
                        startActivity(intent);

//retrieve once again the number of incorrect answers from the second activity

                        Intent mIntent = getIntent();
                        int v2 = mIntent.getIntExtra("integerNumber", 0);
                        contor = v2;

                       // Toast.makeText(getApplicationContext(),"Button clicked count is"+ contor, Toast.LENGTH_LONG).show();
                    }
                }

我尝试使用Toast来查看结果。在第一个错误的答案,它显示:

final Button button7 = (Button) findViewById(R.id.button7);

    button7.setOnClickListener(new  View.OnClickListener() {

        @Override
        public void onClick(View v) {

//get the number of incorrect answers from the previous activity

            Intent mIntent = getIntent();
            int v2 = mIntent.getIntExtra("integerNumber", 0);

         //   Toast.makeText(getApplicationContext(),"Button clicked count is"+ v2, Toast.LENGTH_LONG).show();

            Intent intent = new Intent(TryAgain.this, FirstGame1.class);

//send the number of incorrect answers back to the first activity

            intent.putExtra("integerNumber", v2);
            startActivity(intent);
        }
    });

在第二个错误答案后,显示:

0 (in the second activity)
1 (in the main activity)

无论得到多少错误的答案,它都会坚持这个结果。

1 个答案:

答案 0 :(得分:0)

尝试以下代码

<强> MainActivity

     button13.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            contor++;

            if(contor  > 3) {
                Log.d("===>>>", "Reached three tries");
                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.addCategory(Intent.CATEGORY_HOME);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            } else {
                Intent secondIntent = new Intent(TryAgain.this, FirstGame1.class);
                startActivity(secondIntent);
            }
        }
    });

第二项活动

((Button) findViewById(R.id.button7)).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            onBackPressed();
        }
    });

contor是MainActivity类

中的成员变量