Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 错误:“;方法getText()必须从UI线程调用,当前推断的线程是worker;在安卓工作室_Android_Multithreading_Android Studio_Android Asynctask - Fatal编程技术网

Android 错误:“;方法getText()必须从UI线程调用,当前推断的线程是worker;在安卓工作室

Android 错误:“;方法getText()必须从UI线程调用,当前推断的线程是worker;在安卓工作室,android,multithreading,android-studio,android-asynctask,Android,Multithreading,Android Studio,Android Asynctask,我在我的应用程序中添加了一个asynctask,因为它有很多数据要搜索,但我在protected Void doInBackground中出错,它说必须从UI线程调用方法getText(),当前推断的线程是worker。我的应用程序由具有editText和微调器的对话框组成。请帮助我,并提前表示感谢 doctorLocationEditText = (EditText)dialog.findViewById(R.id.doctorlocation); doctorHospital = (Spi

我在我的应用程序中添加了一个asynctask,因为它有很多数据要搜索,但我在protected Void doInBackground中出错,它说必须从UI线程调用方法getText(),当前推断的线程是worker。我的应用程序由具有editText和微调器的对话框组成。请帮助我,并提前表示感谢

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();
doctorLocationEditText=(EditText)dialog.findviewbyd(R.id.doctorlocation);
doctorHospital=(微调器)dialog.findViewById(R.id.spinnerdoctorhospital);
ArrayAdapter hospitalAdapter=新的ArrayAdapter(Doctor.this,R.layout.spinner_layout,db.getAllHospitalName());
hospitalAdapter.setDropDownViewResource(R.layout.spinner\u布局);
doctorHospital.setAdapter(hospitalAdapter);
Button Button=(Button)dialog.findViewById(R.id.btendorSearch);
setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
新建DoctorSearch().execute();
dialog.dismise();
这是有错误的异步任务

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();
        }
    }
class DoctorSearch扩展了异步任务{
ProgressDialog Asycdialog=新建ProgressDialog(Doctor.this);
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
Asycdialog.setMessage(“加载…”);
Asycdialog.show();
}
@凌驾
受保护的Void doInBackground(Void…arg0){
GetSearchDoctor=dbhelper.getDoctorSearch(index2,DoctorLocationEdit.getText().toString(),doctorHospital.getSelectedItem().toString());
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
lv3=(ListView)findViewById(R.id.searchdoctor_列表);
lv3.setAdapter(新的ViewAdapterSearchDoctor());
super.onPostExecute(结果);
Asycdialog.disclose();
}
}

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