为什么在第二次点击按钮时,它从一开始就没有循环遍历数组?

时间:2015-08-06 15:45:21

标签: java android android-studio

我有这个按钮点击方法,我从onCreate调用它:

public void addListenerOnButton()
    {

        btnClick = (Button) findViewById(R.id.checkipbutton);

        btnClick.setOnClickListener(new OnClickListener()
        {
            byte[] response = null;
            @Override
            public void onClick(View arg0)
            {

                text = (TextView) findViewById(R.id.textView2);

                Thread t = new Thread(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        for (int i = 0; i < ipaddresses.length; i++)

                        {

                                try
                                {
                                    response = Get(ipaddresses[i]);
                                    if (response == null)
                                    {
                                        text.post(new Runnable()
                                        {
                                            @Override
                                            public void run()
                                            {
                                                counter++;
                                                text.setText("Connection Failed: " + ipaddresses[counter]);
                                            }
                                        });
                                    }
                                }
                                catch (Exception e)
                                {
                                    String err = e.toString();
                                }

                                if (response!=null)
                                {


                                    try
                                    {
                                        final String a = new String(response, "UTF-8");
                                        text.post(new Runnable()
                                        {
                                            @Override
                                            public void run()
                                            {
                                                text.setText(a);
                                            }
                                        });

                                        iptouse = ipaddresses[i].substring(0, 26);
                                        connectedtoipsuccess = true;
                                        Logger.getLogger("MainActivity(inside thread)").info(a);
                                    } catch (UnsupportedEncodingException e)
                                    {
                                        e.printStackTrace();
                                        Logger.getLogger("MainActivity(inside thread)").info("encoding exception");
                                    }

                                    Logger.getLogger("MainActivity(inside thread)").info("test1");
                                    break;

                                }
                                else
                                {

                                }
                        }
                        counter = 0;
                    }
                });
                t.start();
            }
        });

    }

对于测试我做了,字符串数组ipaddresses将包含我的服务器上不存在的两项ips,实际上我的服务器根本不工作,而且仅用于测试。

当我第一次点击按钮时,它会在数组上进行循环,在这两种情况下都会返回该响应为null

但是当它完成后,我再次点击按钮,这次我看到它执行response = Get(ipaddresses[i]);i = 0并且响应为null但是后来进入if (response == null) response = Get(ipaddresses[i]);首先再次i == 1if (response == null),然后它才进入counter = 2然后在里面我看到现在变量counter = 2 counter = 0;

然后我的程序崩溃了。我想它从counter = 2开始崩溃,数组大小也是2.但是在循环之后我做了if (response == null)所以为什么我第二次点击按钮i == 0? / p>

为什么我第二次点击按钮时this时没有进入undefined

1 个答案:

答案 0 :(得分:0)

试试这个:

private int counter = 0;

public void addListenerOnButton()
    {

        btnClick = (Button) findViewById(R.id.checkipbutton);

        btnClick.setOnClickListener(new OnClickListener()
        {
            byte[] response = null;
            @Override
            public void onClick(View arg0)
            {

                text = (TextView) findViewById(R.id.textView2);

                Thread t = new Thread(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        for (int i = 0; i < ipaddresses.length; i++)
                          counter = i;
                        {

                                try
                                {
                                    response = Get(ipaddresses[counter]);
                                    if (response == null)
                                    {
                                        text.post(new Runnable()
                                        {
                                            @Override
                                            public void run()
                                            {
                                                counter++;
                                                text.setText("Connection Failed: " + ipaddresses[counter]);
                                            }
                                        });
                                    }
                                }
                                catch (Exception e)
                                {
                                    String err = e.toString();
                                }

                                if (response!=null)
                                {


                                    try
                                    {
                                        final String a = new String(response, "UTF-8");
                                        text.post(new Runnable()
                                        {
                                            @Override
                                            public void run()
                                            {
                                                text.setText(a);
                                            }
                                        });

                                        iptouse = ipaddresses[i].substring(0, 26);
                                        connectedtoipsuccess = true;
                                        Logger.getLogger("MainActivity(inside thread)").info(a);
                                    } catch (UnsupportedEncodingException e)
                                    {
                                        e.printStackTrace();
                                        Logger.getLogger("MainActivity(inside thread)").info("encoding exception");
                                    }

                                    Logger.getLogger("MainActivity(inside thread)").info("test1");
                                    break;

                                }
                                else
                                {

                                }
                        }
                        counter = 0;
                    }
                });
                t.start();
            }
        });

    }