Question

I have a global boolean variable on my class, and also i have a handler on my class. I want that the handler does some code only when that variable of the class is true, but it is not working, because for the handler that variable is ALWAYS false. I dont understand why, because that value is true sometimes, i checked it with a log of the variable value, but for the handler is always false.

This is some example code of what im doing:

my class global variable, i tryed it with and without private and with and without static modifier:

private boolean cancelTimers;

in some part of my code i do this:

cancelTimers=true;

my handler:

    hand=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(cancelTimers==true){
                System.out.println("handler canceled");
                return;
            }else{
                System.out.println("handler done");
                super.handleMessage(msg);   
            }           
       }
    };

The problem is that for my handler, cancelTimners is always false, even when the variable is true, because i am writting his value before calling the handler.

Était-ce utile?

La solution 2

Do something like this

TextView content;
    private boolean cancelTimers=false;
    Handler hand;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        content = (TextView)findViewById(R.id.output);

        content.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                 Log.e("above  valuueeeee", ""+cancelTimers);
                cancelTimers=true;
                 Log.e("below valuueeeee", ""+cancelTimers);
            }
        });

        Log.e("valuueeeee", ""+cancelTimers);
        hand=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                if(cancelTimers==true){
                   Log.e("messs","handler canceled");
                    return;
                }else{
                     Log.e("messs","handler done");
                    super.handleMessage(msg);   
                }           
           }
        };


    }

Autres conseils

Remove private from

  private boolean cancelTimers;

and make cancelTimers global

Make somethig like this:

public class A extends Activity  {


private boolean cancelTimers;

private Handler handler = new Handler() {

    public void handleMessage(android.os.Message msg) {
         if(cancelTimers==true){
            System.out.println("handler canceled");
            return;
        }else{
            System.out.println("handler done");
            super.handleMessage(msg);   
        }       

    }
};

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


}

Declare variable as global.

Declare the variable with static modifier.

private static boolean cancelTimers = false;

If you declare a field with static modifier then the value of a static field will be the same across all instances of the class.

For better understanding of the use of static modifier, you can see this Java doc.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top