异常:ArrayAdapter中的java.lang.NullPointerException <string> </string>

时间:2013-11-09 14:26:02

标签: java android mysql spinner android-arrayadapter

我有两个按钮执行相同的方法,用来自Mysql数据库的数据填充自己的Spinners,但问题是只填充了一个Spinner,当我点击另一个“Buscar”时(来自葡萄牙语:“搜索“)按钮我得到”java.lang.NullPointerException“并且应用程序中止。

我的代码

这是第一个按钮,此代码可以工作并填充微调器:

    btnBuscarProduto.setOnClickListener(new View.OnClickListener() {  
            public void onClick(View v) {
                new Thread(new Runnable() {
                    public void run() {
                        buscarPor = "produtos";
                        System.out.println("Buscando por: "+buscarPor);
                        Looper.prepare();
                        resultadoBusca = buscar(edtBuscaProduto.getText().toString());
                        System.out.println("Resultado da busca: "+resultadoBusca);
                        ((Activity)context).runOnUiThread(new Runnable(){
                            @Override
                            public void run() {
                                if(resultadoBusca.equalsIgnoreCase("vazio")){
                                    Toast toast = Toast.makeText(getActivity(), "Nada Encontrado", Toast.LENGTH_SHORT);
                                    toast.show(); 
                                 }else{
                                   listaBusca = makeList(resultadoBusca);
                                   ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_dropdown_item, listaBusca);
                                   spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
                                   spnProdutos.setAdapter(spinnerArrayAdapter);
                                }               
                            }});

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

这是第二个按钮,此代码有效,但不会填充微调器:

    btnBuscarPizza.setOnClickListener(new View.OnClickListener() {  
            public void onClick(View v) {
                new Thread(new Runnable() {
                    public void run() {
                        buscarPor = "pizzas";
                        System.out.println("Buscando por: "+buscarPor);
                        Looper.prepare();
                        resultadoBusca = buscar(edtBuscaPizza.getText().toString());
                        System.out.println("Resultado da busca: "+resultadoBusca);
                        ((Activity)context).runOnUiThread(new Runnable(){
                            @Override
                            public void run() {
                                if(resultadoBusca.equalsIgnoreCase("vazio")){
                                    Toast toast = Toast.makeText(getActivity(), "Nada Encontrado", Toast.LENGTH_SHORT);
                                    toast.show(); 
                                }else{
                                    listaBusca = makeList(resultadoBusca);
                                    System.out.println("Lista da busca: "+listaBusca);
                                    ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_dropdown_item, listaBusca);
                                    spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
                                    spnPizzas.setAdapter(spinnerArrayAdapter);
                                }               
                            }});

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

在上面的方法中,错误在以下几行中:

    ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_dropdown_item, listaBusca);
                                        spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
                                        spnPizzas.setAdapter(spinnerArrayAdapter);

其他方法:

    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>(2);
                nameValuePairs.add(new BasicNameValuePair("buscarPor", buscarPor));
                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;
    }

0 个答案:

没有答案