方法getCheckedRadioButtonId必须从UI线程Android调用吗?

时间:2016-02-25 12:06:23

标签: android android-asynctask

我在AsyncTask写下了以下代码,但是错误并说:

必须从UI线程**

调用*方法getCheckedRadioButtonId
 new AsyncTask<Void,Void,String>(){

        @Override
        protected String doInBackground(Void... params) {
            long result = 0;
            long t = System.currentTimeMillis();
   HERE---->switch (StartActivity.this.RadioType.getCheckedRadioButtonId()){
                case R.id.radioFibJR:
                    result = FibLib.fibJR(n);
                    break;
                case R.id.radioFibJI:
                    result = FibLib.fibJI(n);
                    break;
                case R.id.radioFibNR:
                    result = FibLib.fibNR(n);
                    break;
                case R.id.radioFibNI:
                    result = FibLib.fibNI(n);
                    break;
            }
            t = System.currentTimeMillis() - t;

        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
        }
    }.execute();

1 个答案:

答案 0 :(得分:1)

将该代码放在AsyncTask之外。这就是诀窍。干杯:)

 int id=StartActivity.this.RadioType.getCheckedRadioButtonId();
 new AsyncTask<Void,Void,String>(){

        @Override
        protected String doInBackground(Void... params) {
            long result = 0;
            long t = System.currentTimeMillis();
             switch (id){
                case R.id.radioFibJR:
                    result = FibLib.fibJR(n);
                    break;
                case R.id.radioFibJI:
                    result = FibLib.fibJI(n);
                    break;
                case R.id.radioFibNR:
                    result = FibLib.fibNR(n);
                    break;
                case R.id.radioFibNI:
                    result = FibLib.fibNI(n);
                    break;
            }
            t = System.currentTimeMillis() - t;

        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
        }
    }.execute();