我有两个按钮执行相同的方法来用 Mysql 数据库中的数据填充自己的 Spinner,但问题是只有一个 Spinner 被填充,当我单击另一个“Buscar”(来自葡萄牙语:“Search”)按钮我得到“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;
}