android studio: java.lang.RuntimeException: An error occured while executing doInBackground()

316 Views Asked by At

i'm trying to make an app by using calimero library for KNX. In my app, i use some buttons, switches, togglebuttons, etc to switch on/off the lights. With 'Button', everything works well. I can switch on/off a light or open/close curtains.

private class button9OnClickListener implements View.OnClickListener {
    public void onClick(View view){

        try {
            falseTask dt = new falseTask();
            String adr = "5/1/0";
            dt.execute(adr);

        } catch (Exception e) {

        }

    }

}

But when i use 'Switch' with either OnClickListener or OnCheckedChangeListenser, it crash.

private class switch1OnCheckedChangeListener implements CompoundButton.OnCheckedChangeListener {
    public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
        if(compoundButton.isChecked()) {

            try {
                trueTask dt = new trueTask();
                String adr = "5/4/6";
                dt.execute(adr);
            } catch (Exception e) {
                e.printStackTrace();
            }


        } else {

            try {
                falseTask dt = new falseTask();
                String adr = "5/4/6";
                dt.execute(adr);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}

the problem is below:

uncaughtException   java.lang.RuntimeException: An error occured while executing doInBackground()

and here's my doInBackground

        protected String doInBackground(String...strings){

        String adr = strings[0];

        try {

            final InetSocketAddress localEP = new InetSocketAddress(InetAddress.getByName(hostip), 0);
            final InetSocketAddress remoteEP = new InetSocketAddress(remoteip, knxServerPort);

            knxLink = new KNXNetworkLinkIP(KNXNetworkLinkIP.TUNNEL, localEP, remoteEP, true, TPSettings.TP1);

            pc = new ProcessCommunicatorImpl(knxLink);
            pc.write(new GroupAddress(adr), true);
            knxLink.close();

        }
        catch (final KNXException e) {

        }
        catch (final UnknownHostException e) {

        }

        return null;
    }

i'm new to java and android programming. Can you help with this problem? i can't understand why this work for 'button' but not for 'switch'.

Thank you in advance

UPDATE

I copy my code and try with eclipse. Everything work fine. I can switch on/off the light i want and there is no error. With android studio, it did not work. Someone know the reason?

2

There are 2 best solutions below

0
On

I tried with ToggleButton and Switch in Eclipse. All work good. I put exactly what I used in android studio. I copy all the code without any change. Can't understand the problem.

0
On

Finally I find the problem. I use 2 Bundle in the MainActivity and I use these 2 Bundle in doInBackground for my AsyncTask in the SecondActivity. But I just pass on the first Bundle to the SecondActivity and I forget to pass on the second Bundle. So I got error while executing doInBackground() What foolish mistake I have made...