android – Variable does not update in java-ThrowExceptions

Exception or error:

I am a beginner programmer on android with java and I have a problem. Sometimes my variables Wyb1, Wyb2 and Wyb3 with the SetOnclickListener function do not update and these variables after re-executing the function sometimes remain with the same value as before.`Please help in my code.

Wyb1, Wyb2 and Wyb3 not update sometimes

TextView proba,Dz,W1,W2,W3,l;
private int a,b,c,l1,l2,p=0,h;
ImageButton Wyb1,Wyb2,Wyb3;
ProgressBar mProgressBar;
CountDownTimer mCountDownTimer;
int i=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game_single);

    proba = findViewById(R.id.textView12);
    Dz = findViewById(R.id.Dzialanie);
    W1 = findViewById(R.id.Wpozycja1);
    W2 = findViewById(R.id.Wpozycja2);
    W3 = findViewById(R.id.Wpozycja3);
    l = findViewById(R.id.Level);
    Wyb1 = findViewById(R.id.imageButton11);
    Wyb2 = findViewById(R.id.imageButton12);
    Wyb3 = findViewById(R.id.imageButton13);
    mProgressBar=findViewById(R.id.progressBar);
    l.setText("0");



    if (getIntent().getExtras()!=null) {
        int buttonPressed = getIntent().getExtras().getInt(MenuGameActivity.INTENT_KEY);

        if (buttonPressed == MenuGameActivity.FIRST_OPTION) {
            // first button is clicked
            proba.setText("dodawanie");
            Do();
        } else if (buttonPressed == MenuGameActivity.SECOND_OPTION) {
            //second button is clicked
            proba.setText("odejmowanie");
        } else if (buttonPressed == MenuGameActivity.THIRD_OPTION) {
            //second button is clicked
            proba.setText("odejmowanie");
        }else if (buttonPressed == MenuGameActivity.FOURTH_OPTION) {
            //second button is clicked
            proba.setText("odejmowanie");
        }else if (buttonPressed == MenuGameActivity.FIFTH_OPTION) {
            //second button is clicked
            proba.setText("odejmowanie");
        }else if (buttonPressed == MenuGameActivity.SIXTH_OPTION) {
            //second button is clicked
            proba.setText("odejmowanie");
        }

    }
}
public void Do()
{
    los();
    proba.setText("dodawanie");
    c = Dodawanie(a,b);
    String numberAsString = String.valueOf(c);

    Random liczba = new Random();
    h = liczba.nextInt(5);
    String H = String.valueOf(h);
    String L1 = String.valueOf(l1);
    String L2 = String.valueOf(l2);
    Toast.makeText(getApplicationContext(), H, Toast.LENGTH_LONG).show();
    //////////////////////////////////////////////////////////////////////
    mProgressBar.setProgress(i);
    mCountDownTimer=new CountDownTimer(15000,1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            Log.v("Log_tag", "Tick of Progress"+ i+ millisUntilFinished);
            i++;
            mProgressBar.setProgress((int)i*100/(15000/1000));

        }

        @Override
        public void onFinish() {
            //Do what you want
            i++;
            //mProgressBar.setProgress(100);
        }
    };
    mCountDownTimer.start();
    ///////////////////////////////////////////
        if(h==0||h==3) {
            W1.setText(numberAsString);
            Wyb1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    p++;
                    String L = String.valueOf(p);
                    l.setText(L);
                    Do();
                    i=0;
                }
            });
            W2.setText(L1);
            Wyb2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getApplicationContext(), "Zle", Toast.LENGTH_LONG).show();
                }
            });
            W3.setText(L2);
            Wyb3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getApplicationContext(), "Zle", Toast.LENGTH_LONG).show();
                }
            });
        }
        else if(h==1||h==4){
            W2.setText(numberAsString);
            Wyb2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    p++;
                    String L = String.valueOf(p);
                    l.setText(L);
                    Do();
                    i=0;
                }
            });
            W1.setText(L1);
            Wyb1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getApplicationContext(), "Zle", Toast.LENGTH_LONG).show();
                }
            });
            W3.setText(L2);
            Wyb3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getApplicationContext(), "Zle", Toast.LENGTH_LONG).show();
                }
            });
        if(h==2||h==5)
            {
                W3.setText(numberAsString);
                Wyb3.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        p++;
                        String L = String.valueOf(p);
                        l.setText(L);
                        Do();
                        i=0;
                    }
                });
                W1.setText(L1);
                Wyb1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(getApplicationContext(), "Zle", Toast.LENGTH_LONG).show();
                    }
                });
                W2.setText(L2);
                Wyb2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(getApplicationContext(), "Zle", Toast.LENGTH_LONG).show();
                    }
                });
            }

    }
}

public int Dodawanie(int a,int b)
{
    String A = String.valueOf(a);
    String B = String.valueOf(b);
    Dz.setText(A+" + "+B);
    return a + b;
}
public void los()
{
    Random liczba = new Random();
    a = liczba.nextInt(101);
    b = liczba.nextInt(101);
    l1= liczba.nextInt(101)+30;
    l2= liczba.nextInt(101)+30;
    String L1 = String.valueOf(l1);
    String L2 = String.valueOf(l2);



}

}

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *