gpt4 book ai didi

android 搜索 View 上的 java.lang.NullPointerException getCount

转载 作者:行者123 更新时间:2023-11-29 05:13:09 25 4
gpt4 key购买 nike

我正在尝试在我的应用程序中实现搜索功能,它部分工作,当它找不到项目时应用程序崩溃并给我以下错误:

12-19 14:35:44.019  29694-29694/W/System.err﹕ at br.com.adapter.DespesaViewAdapter.getCount(DespesaViewAdapter.java:75)

12-19 14:35:44.020 29694-29694/W/System.err﹕ at br.com.representemais.DespesaActivity$1.onQueryTextChange(DespesaActivity.java:241)

上面几行是:

75: 返回 despesa.size();241:lv.setAdapter(广告);

这是我的全部代码:

public class DespesaViewAdapter extends BaseAdapter {


private Repositorio mRepositorio;
private SearchView.OnQueryTextListener query;
private Runnable frag;
private DespesaActivity desp;


private static LayoutInflater inflater=null;


private List<DespesasDaViagemModel> despesa = new ArrayList<DespesasDaViagemModel>();


public DespesaViewAdapter(Context context, Runnable fragmentDespesa, List<DespesasDaViagemModel> despesas) {
this.inflater = LayoutInflater.from( context );
this.despesa = despesas;
mRepositorio = new Repositorio(context);
frag = fragmentDespesa;



}

public DespesaViewAdapter(Context contextx, SearchView.OnQueryTextListener onQueryTextListener, List<DespesasDaViagemModel> despesa) {

this.inflater = LayoutInflater.from( contextx );
this.despesa = despesa;

query = onQueryTextListener;



}




public int getCount() {
return despesa.size();
}

public Object getItem(int position) {
return position;
}

public long getItemId(int position) {
return position;
}

public View getView(int position, View convertView, ViewGroup parent) {

View vi=convertView;
ViewHolder holder;
if(convertView == null){
vi = inflater.inflate(R.layout.despesa_activity, null);
holder=new ViewHolder();
holder.id = (TextView)vi.findViewById(R.id.despesaId);
holder.data = (TextView)vi.findViewById(R.id.dataDespesa);
holder.categoria = (TextView)vi.findViewById(R.id.despesaCategoria);
holder.valor = (TextView)vi.findViewById(R.id.valorDespesa);

View corCategoria = (View) vi.findViewById(R.id.corCategoria);
corCategoria.setBackgroundResource(R.drawable.corner_categoria);
GradientDrawable drawable = (GradientDrawable) corCategoria.getBackground();

String corFormatada = (despesa.get(position).getCor());


drawable.setColor(Color.parseColor(corFormatada));






vi.setTag(holder);
}else{
holder = (ViewHolder)vi.getTag();
}



DespesasDaViagemModel item = new DespesasDaViagemModel();

item = despesa.get(position);
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = df.parse(item.getData());
} catch (ParseException e) {
e.printStackTrace();
}
df.applyPattern("dd/MM/yyyy");
String dataCriado = df.format(date);
holder.id.setText(String.valueOf(item.getId()));
holder.data.setText(dataCriado);
holder.categoria.setText(item.getCategoria());
holder.valor.setText("R$" + item.getValor());




return vi;
}

public void setData(List<DespesasDaViagemModel> despesas){
this.despesa.addAll(despesas);
this.notifyDataSetChanged();
}

public class ViewHolder
{
TextView id;
TextView data;
TextView categoria;
TextView valor;


}
}


public class DespesaActivity extends ActionBarActivity {


private String rm_IdViagem;
private List<DespesasDaViagemModel> despesa = new ArrayList<DespesasDaViagemModel>();
private boolean searchCheck;
private SearchView searchView;
private ProgressBar progressBar;
private ListView lv;
private LinearLayout footerLinearLayout;
private DespesaViewAdapter ad;
private DespesaViewAdapter ads;
private ProgressDialog dialog;


@Override
public void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
// TODO Auto-generated method stub
setContentView(R.layout.despesa);

getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);

SecurePreferences mSessao = new SecurePreferences(DespesaActivity.this, "sessao");
mSessao.put("menuAtual", "Despesas");

lv = (ListView) findViewById(R.id.listaDespesa);


Bundle parametros = getIntent().getExtras();
if (parametros != null) {

rm_IdViagem = parametros.getString("id_viagem");


}

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);


MenuItem searchItem = menu.findItem(R.id.menu_search);


searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setQueryHint(this.getString(R.string.search));

((EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text))
.setHintTextColor(getResources().getColor(R.color.white));
searchView.setOnQueryTextListener(OnQuerySearchView);


if (rm_IdViagem != null) {

new DownloadTask().execute();


}

menu.findItem(Menus.PROCURAR).setVisible(true);
searchCheck = false;

return super.onCreateOptionsMenu(menu);


}

public class DownloadTask extends AsyncTask<Void, Void, Void> {


@Override
protected void onPreExecute() {
dialog = new ProgressDialog(DespesaActivity.this);
// setup your dialog here
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.setTitle("Carregando Despesas");
dialog.setMessage("Aguarde...");
dialog.setCancelable(false);
dialog.show();

}

protected Void doInBackground(Void... params) {



despesa = new ArrayList<DespesasDaViagemModel>();

try {
runOnUiThread(new Runnable() {
@Override
public void run() {


Repositorio mRepositorio = new Repositorio(DespesaActivity.this);

List lista = mRepositorio.getDespesasViagensPorId("", Integer.valueOf(rm_IdViagem));

despesa = lista;


ad = new DespesaViewAdapter(DespesaActivity.this, this, despesa);

lv.setVerticalFadingEdgeEnabled(true);
lv.setVerticalScrollBarEnabled(true);

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

//

}

});
lv.setAdapter(ad);

TextView emptyText = (TextView) findViewById(R.id.SemResultado);
lv.setEmptyView(emptyText);

}
});


} catch (Exception e) {
e.printStackTrace();
}


return null;
}

@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (dialog.isShowing()) {
dialog.dismiss();
}


}
}


private SearchView.OnQueryTextListener OnQuerySearchView = new SearchView.OnQueryTextListener() {


@Override
public boolean onQueryTextSubmit(String arg0) {
// TODO Auto-generated method stub
return false;
}

@Override
public boolean onQueryTextChange(String search) {

// TODO Auto-generated method stub

if (searchCheck) {
// implement your search here




despesa = new ArrayList<DespesasDaViagemModel>();
try {


Repositorio mRepositorio = new Repositorio(DespesaActivity.this);


List listaDeDespesas = mRepositorio.getDespesasViagensPorId(search, Integer.valueOf(rm_IdViagem));

despesa = listaDeDespesas;


ad = new DespesaViewAdapter(DespesaActivity.this, this, despesa);


lv.setVerticalFadingEdgeEnabled(true);
lv.setVerticalScrollBarEnabled(true);

lv.setAdapter(ad);


TextView emptyText = (TextView) findViewById(R.id.SemResultado);
lv.setEmptyView(emptyText);


} catch (Exception e) {
e.printStackTrace();
}

}
return false;
}


};


private static final int REFRESH = 1;
private static final int SEARCH = 2;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {

case Menus.PROCURAR:
searchCheck = true;
break;
case android.R.id.home:
super.onBackPressed();
overridePendingTransition(R.anim.animation_back, R.anim.animation_back_leave);

case REFRESH:

return true;

case SEARCH:

return true;

case R.id.menu_filtro:
this.fnFiltrar();

return true;

case R.id.menu_atualizar:
/* dispara os repositorios a sincronizar */

new Sincronizar(DespesaActivity.this, DespesaActivity.this).execute(0);


return true;

}


return true;

}


@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public void onBackPressed() {

super.onBackPressed();
overridePendingTransition(R.anim.animation_back, R.anim.animation_back_leave);
finish();

}

private void fnFiltrar() {

Intent intent = new Intent(this, FiltrarActivity.class);

startActivity(intent);

}


}

最佳答案

我猜你传递的是 null 而不是数组列表。只需尝试在适配器的 getCount() 方法中打印列表即可。

并在 doInBackground() 中

 List lista = mRepositorio.getDespesasViagensPorId("", Integer.valueOf(rm_IdViagem));
despesa.addAll(lista);

public int getCount() {
if(despesa == null)
{
return 0;
}
return despesa.size();
}

关于android 搜索 View 上的 java.lang.NullPointerException getCount,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27570819/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com