-2

I am working on a project to control lights using arduino + ethernet shield + android app. I am using android studio for app development purpose. The issue is, try-catch block has been implemented inside the OnClickListener() which doesnt seem to work. I am new to android app development and cant think of a solution for the same. The app does get installed but the buttons do not perform their function. i.e. the server doesnt receive any package. Actually, initially the targetSdkVersion was set to 8, hence the holo theme and the buttons worked properly. Once i set it to 22 (lollipop) the material theme gets applied by default and the buttons no longer work. Thanking you in advance.

public void led(String s) throws Exception
{

    byte[] b=(s.getBytes());
    if(isOnline())
    {

    serverHostname1 = new String ("192.168.1.177");
    ip = InetAddress.getByName(serverHostname1); 
    d1 = new DatagramSocket();//}

    try{
        send =  new DatagramPacket(b,b.length, ip, 8032);   
    }catch(Exception e){

    }


    d1.send(send); 
    d1.setSoTimeout(10000);
    d1.receive(rec); 
    modifiedSentence =   new String(rec.getData());
    InetAddress returnIPAddress = rec.getAddress();

    Toast.makeText(getApplicationContext(),"Reply from Server:"+returnIPAddress,Toast.LENGTH_LONG).show();

    d1.close(); 
    }
    else
    {
        Toast.makeText(getApplicationContext(),"No network",Toast.LENGTH_LONG).show();
    }
}

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button on= (Button)findViewById(R.id.on);
    Button off= (Button)findViewById(R.id.off);


    on.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            try {
                //ArduinoActivity a=new ArduinoActivity();

                led("1");
                  Toast.makeText(getApplicationContext(),"ON",Toast.LENGTH_SHORT).show();


            } catch (Exception e) {
                // TODO Auto-generated catch block
                System.out.println("Error::"+e);
            }

        }
    });


    off.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            try {
                //ArduinoActivity b=new ArduinoActivity();
                led("2");
                Toast.makeText(getApplicationContext(), "OFF",Toast.LENGTH_SHORT).show();

            } catch (Exception e) {
                 //TODO Auto-generated catch block
                System.out.println("Error::"+e);
            }

        }
    });  
}
}
user3289834
  • 3
  • 1
  • 8
  • 1
    What do you mean it doesn't work? What's the problem? Is there an error? – Anubian Noob Jul 01 '15 at 23:21
  • @AnubianNoob The app does get installed but the buttons do not perform their function. i.e. the server doesnt receive any package. Actually, initially the targetSdkVersion was set to 8, hence the holo theme and the buttons worked properly. Once i set it to 22 (lollipop) the material theme gets applied by default and the buttons no longer work. Correct me if im wrong, but after a bit of research i realised that due to some reason, try-catch block inside OnClickListener() no longer works. – user3289834 Jul 01 '15 at 23:26
  • 2
    The question is very unclear -- please clarify greatly. Edit: I now see your comment above, most of this information should be added to the original question itself by [editing the original question](http://stackoverflow.com/posts/31173219/edit) (click link please). Also, when you state that you've done research and have found that try-catch does not work inside of on click listener, you will want to back this up with links to appropriate sites that demonstrate what you are claiming. – Hovercraft Full Of Eels Jul 01 '15 at 23:26
  • @HovercraftFullOfEels Have clarified in the above comment. Sorry for the inconvenience. The app does get compiled and installed without any errors but the buttons do not perform their function of 'led("1");' which is needed to turn on the led on the server side (arduino). The server does not receive any package. – user3289834 Jul 01 '15 at 23:30
  • @HovercraftFullOfEels apologies. I do understand your concern and will keep it in mind. Would like to mention that i did try placing the toast outside try-catch block and it worked while it dint show up on click inside the try-catch. Hence the conclusion. – user3289834 Jul 01 '15 at 23:37

1 Answers1

-1

The issue was not with the try-catch block but the networking operations being carried out inside UI thread (sending of packet) which is not allowed in android versions 3.0+ . The solution is to make use of Asynctask for the same. Thank you :)

user3289834
  • 3
  • 1
  • 8