Question

I do not know how to use variables from another handler in a different method. I tried to set those variables(readBuf, readMessage, and msg) as public, but that did not work. How do I access these variables in separate method? I want those variables listed including .arg1 and .obj in another handler.

The code that is commented out is where the processing for the data originally was.

    public Handler handler;
    public byte[] readBuf; 
    public String readMessage;
    public Object msg;

    handler = new Handler();

    private final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case MESSAGE_STATE_CHANGE:
                if(D) Log.i(TAG, "MESSAGE_STATE_CHANGE: " + msg.arg1);
                switch (msg.arg1) {
            case MESSAGE_READ:
                       //for(int a = 0; a< 8000; a++)
                       //{
                       try
                        {
                        byte[] readBuf = (byte[]) msg.obj;
                        String readMessage = new String(readBuf,0,msg.arg1);
                        mConversationArrayAdapter.add("Voltage: "+ readMessage);
                        //double[] convert = new double[1];
                        //for(int z=0; z <1;z++)
                        //{
                        //convert[z]= Double.parseDouble(readMessage);
                        //}
                        //for(int j=0; j<1;j++)
                        //{
                        //stored[a][j]= convert[j];
                        //}
                        //}
                        catch(NumberFormatException e)
                        {
                         System.err.println("NumberFormatException: "+e.getMessage());
                        }
                        //}
                break;
            }
        }
    };

    public void process()
    {
        new Thread(new Task()).start();
    }

    class Task implements Runnable
    {
        @Override
        public void run() 
        {
                try{
                    Thread.sleep(1000);
                }
                catch(InterruptedException e)
                {
                    e.printStackTrace();
                }
                handler.post(new Runnable()
                {
                    @Override
                    public void run(){
                        for(int a = 0; a< 8000; a++)
                        {
                        byte[] readBuf = (byte[]) msg.obj;//ERROR with .obj
                        try
                        {
                        String readMessage = new String(readBuf,0,msg.arg1);// Error with .arg1
                        mConversationArrayAdapter.add("Voltage: "+ readMessage);
                        double[] convert = new double[1];
                        for(int z=0; z <1;z++)
                        {
                        convert[z]= Double.parseDouble(readMessage);
                        }
                        for(int j=0; j<1;j++)
                        {
                        stored[a][j]= convert[j];
                        }
                        }
                        catch(NumberFormatException e)
                        {
                         System.err.println("NumberFormatException: "+e.getMessage());
                        }
                        }


                    }
                });
        }

    }
}
Was it helpful?

Solution

The way I used the object variables in other methods was by setting new public static variables equal to the object variables. That way I could use them in other methods.

OTHER TIPS

You must create a Message and send by handler, not "share" variables.

Message msg = new Message();
msg.what = ...;
msg.obj  = ...;
....
handler.sendMessage(msg);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top