0

当我单击“Buscar”(来自葡萄牙语:搜索)按钮时,我试图在执行时使用 mysql 数据库中的数据填充微调器,但是当我单击此按钮时,出现以下异常:“android.view.ViewRootImpl $CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触及其视图。”

我的代码

btnBuscarProduto.setOnClickListener(new View.OnClickListener() {  
        public void onClick(View v) {
            new Thread(new Runnable() {
                public void run() {
                    Looper.prepare();
                    /*This is a string*/resultadoBusca = buscar(edtBuscaProduto.getText().toString());
                    System.out.println("Resultado da Busca: "+resultadoBusca);
                    if(resultadoBusca.equalsIgnoreCase("Vazio")){
                        Toast toast = Toast.makeText(getActivity(), "Nada Encontrado", Toast.LENGTH_SHORT);
                        toast.show(); 
                    }else{
                        /*This is a List<String>*/listaBusca = makeList(resultadoBusca);
                        System.out.println("Lista da Busca"+listaBusca);
                        ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item, list);
                        spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
                        spnProdutos.setAdapter(spinnerArrayAdapter);

                    }                                             
                }
              }).start();                 
        }
    });

public String buscar(String termo){
String resp = null;
try{
        httpclient = new DefaultHttpClient();
        httppost = new HttpPost("http://192.168.1.101/android/busca.php");
        nameValuePairs = new ArrayList<NameValuePair>(1);
        nameValuePairs.add(new BasicNameValuePair("busca", termo)); 
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));            
        response=httpclient.execute(httppost);           
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        resp = httpclient.execute(httppost, responseHandler);
    }catch(Exception e){
        Toast toast = Toast.makeText(getActivity(), "Erro: "+e.getMessage(), Toast.LENGTH_SHORT);
        toast.show(); 
    }
return resp;
}

public List<String> makeList(String input){ 
    List<String> list = new ArrayList<String>();
    String[] newArray = input.split("\\|");
    for (int i =0; i<newArray.length; i++){
        list.add(newArray[i].toString());
    }   
    return list;
}  
4

4 回答 4

1

您收到此异常是因为您尝试在此处更新UI背景Thread

 Toast toast = Toast.makeText(getActivity(), "Nada Encontrado", Toast.LENGTH_SHORT);
                    toast.show();

有几种方法可以处理这个问题,我最喜欢的一种方法是使用AsyncTask

您可以执行AsyncTaskwhich 将在其中执行您的网络内容,doInBackground()然后您可以将结果返回到onPostExecute()您可以UI根据结果更新的地方。

这是使用 AsyncTask 的示例

于 2013-11-09T00:15:02.393 回答
0

我认为您不能从 UI 线程以外的其他线程更改 UI。看runOnUiThread。得到搜索结果后,你应该进入UI线程,然后可以修改UI。

于 2013-11-08T23:52:41.383 回答
0

为了简单起见,我会这样做:

btnBuscarProduto.setOnClickListener(new View.OnClickListener() {  
    public void onClick(View v) {
        new Thread(new Runnable() {
            public void run() {
                Looper.prepare();
                /*This is a string*/resultadoBusca = buscar(edtBuscaProduto.getText().toString());
                System.out.println("Resultado da Busca: "+resultadoBusca);
               MyActivity.this.runOnUiThread(new Runnable(){

                    @Override
                    public void run() {
                        if(resultadoBusca.equalsIgnoreCase("Vazio")){
                            Toast toast = Toast.makeText(getActivity(), "Nada Encontrado", Toast.LENGTH_SHORT);
                            toast.show(); 
                         }else{
                           /*This is a List<String>*/listaBusca = makeList(resultadoBusca);
                           System.out.println("Lista da Busca"+listaBusca);
                           ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item, list);
                           spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
                           spnProdutos.setAdapter(spinnerArrayAdapter);

                        }               
                    }});

            }
          }).start();                 
    }
});
于 2013-11-08T23:57:55.363 回答
0

非 ui 线程不允许接触视图。通过 Context 提供的 runOnUIThread 方法在 UI 线程上执行视图操作。

于 2013-11-09T00:12:48.417 回答