错误:“必须从UI线程调用方法getText(),当前推断的线程是worker。”在Android Studio中

时间:2015-11-26 07:19:09

标签: android multithreading android-studio android-asynctask

我在我的应用程序中添加了一个asynctask,因为它有很多要搜索的数据,但我在受保护的Void doInBackground中遇到错误,并且它说必须从UI线程调用Method getText(),当前推断的线程是worker。我的应用程序由具有editText和Spinner的对话框组成。请帮助我,并提前感谢。

doctorLocationEditText = (EditText)dialog.findViewById(R.id.doctorlocation);

doctorHospital = (Spinner) dialog.findViewById(R.id.spinnerdoctorhospital);
ArrayAdapter<String> hospitalAdapter = new ArrayAdapter<String>(Doctor.this,R.layout.spinner_layout, db.getAllHospitalName());
hospitalAdapter.setDropDownViewResource(R.layout.spinner_layout);
doctorHospital.setAdapter(hospitalAdapter);

Button button = (Button) dialog.findViewById(R.id.btndoctorsearch);
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        new DoctorSearch().execute();
        dialog.dismiss();

这是我的asynctask有错误

class DoctorSearch extends AsyncTask<Void, Void, Void> {

        ProgressDialog Asycdialog = new ProgressDialog(Doctor.this);

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            Asycdialog.setMessage("Loading...");
            Asycdialog.show();
        }

        @Override
        protected Void doInBackground(Void... arg0) {
            GetSearchDoctor = dbhelper.getDoctorSearch(index2, doctorLocationEditText.getText().toString(), doctorHospital.getSelectedItem().toString());
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            lv3 = (ListView) findViewById(R.id.searchdoctor_list);
            lv3.setAdapter(new ViewAdapterSearchDoctor());
            super.onPostExecute(result);
            Asycdialog.dismiss();
        }
    }

1 个答案:

答案 0 :(得分:2)

enter image description here

  • 您不应该从后台线程访问UI线程。
  • 在onPreExecute()或AsyncTask构造函数&amp;中获取文本将它传递给doInBackground。