我试图使用runOnUiThread来获取错误,因为String var我正在使用应该是final我该如何解决它?

时间:2015-09-19 09:51:28

标签: java

@Override
        public boolean onTouchEvent(MotionEvent event)
        {
            float eventX = event.getX();
            float eventY = event.getY();

            float lastdownx = 0;
            float lastdowny = 0;

            switch (event.getAction())
            {
                case MotionEvent.ACTION_DOWN:
                    lastdownx = eventX;
                    lastdowny = eventY;

                    Thread t = new Thread(new Runnable()
                    {
                        @Override
                        public void run()
                        {
                            byte[] response = null;
                            if (connectedtoipsuccess == true)
                            {

                                if (is_start == true)
                                {
                                    response = Get(iptouse + "start");
                                    is_start = false;
                                } else
                                {
                                    response = Get(iptouse + "stop");
                                    is_start = true;
                                    servercheckCounter = 0;
                                }
                                if (response != null)
                                {
                                    String a = null;
                                    try
                                    {
                                        a = new String(response, "UTF-8");

                                        MainActivity.this.runOnUiThread(new Runnable()
                                        {
                                            @Override
                                            public void run()
                                            {
                                                if (a.equals("Recording started"))
                                                {
                                                    status1.setText("Recording");
                                                }
                                                if (a.equals("Recording stopped and preparing the file to be shared on youtube"))
                                                {
                                                    status1.setText("Recording Stopped");
                                                }
                                            }
                                        });
                                        textforthespeacch = a;
                                        MainActivity.this.initTTS();
                                    } catch (UnsupportedEncodingException e)
                                    {
                                        e.printStackTrace();
                                    }
                                    Logger.getLogger("MainActivity(inside thread)").info(a);
                                }
                            }
                        }
                    });
                    t.start();
                    return true;
                case MotionEvent.ACTION_MOVE:
                    break;
                case MotionEvent.ACTION_UP:
                    break;
                default:
                    return false;
            }
            return true;
        }

这是我更改后添加的runOnUiThread:

MainActivity.this.runOnUiThread(new Runnable()
                                        {
                                            @Override
                                            public void run()
                                            {
                                                if (a.equals("Recording started"))
                                                {
                                                    status1.setText("Recording");
                                                }
                                                if (a.equals("Recording stopped and preparing the file to be shared on youtube"))
                                                {
                                                    status1.setText("Recording Stopped");
                                                }
                                            }
                                        });

现在的问题是变量' a'应该是最终的,但如果我将它作为最终,那么我就不能签署一个新的字符串(响应," UTF-8");在这种情况下我该怎么做?

1 个答案:

答案 0 :(得分:0)

你可以将“a”移动到类字段(类属性),它不需要更改最终