- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在我的应用程序中实现搜索功能,它部分工作,当它找不到项目时应用程序崩溃并给我以下错误:
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/
在 Tomcat 6/Ubuntu 12.04 上启动 Grails 2.1.0 应用程序时出现以下错误。 Error 500 - Internal Server Error. groovy.lang
在运行 Storm 拓扑时,我收到此错误。拓扑完美运行 5 分钟,没有任何错误,然后失败。我正在使用 Config.TOPOLOGY_TICK_TUPLE_FREQ_SECS as 300 sec i
我有一个 jsp 代码在其中一台机器上运行良好。但是当我复制到另一台机器时,我得到了这个 no such method found 异常。我是 Spring 的新手。有人可以解释我错过了什么吗? 以下
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我的代码在下面给出了一个错误; Exception in thread "main" java.lang.NoSuchMethodError: com/myApp/Client.cypherCBC(L
我正在尝试一个 Restful web 服务示例,所以当我要访问 url 时,我遇到了异常 java.lang.NoSuchMethodError: jersey.repackaged.com.goo
我正在将一个 Spring web 项目转换为一个 Maven 项目,但我收到了这个错误: java.lang.NoSuchMethodError: org.jboss.logging.Logger.
在我的项目中,我有一个像这样的枚举: public enum MyEnum { FIRST(1), SECOND(2); private int value; private MyEnum(int v
我创建了这个简单的示例,用于读取 Linux 正常运行时间: public String getMachineUptime() throws IOException { String[] di
我正在使用 Eclipse,并且正在使用 Java。我的目标是使用 bogoSort 方法对 vector 进行排序在一个 vector (vectorExample)中适应我的 vector 类型,
我正在运行以下查询。它显示一条错误消息。如何解决这个错误? ListrouteList=null; List companyList = session.createS
我有以下模型类: @Entity @Table(name="user_content") @org.hibernate.annotations.NamedQueries({ @org.
我有那个错误。这是我的代码: GmailSettingsService service = new GmailSettingsService(APPLICATION_NAME, DOMAIN_NAME
实际上我在执行我的java程序时遇到了下面提到的错误 Exception in thread "pool-1-thread-1" java.lang.ClassCastException: jav
java.lang.ClassCastException: java.lang.Float cannot be cast to java.lang.String 我在以下代码中遇到此异常: Strin
我正在尝试从 linkedhashset 中检索随机元素。下面是我的代码,但它每次都给我异常。 private static void generateRandomUserId(Set userIds
我已经完成了 Android 中的代码: List spinnerArray = new ArrayList(); for (int i = 0; i item = (LinkedTreeMap)
这个问题已经有答案了: Explanation of ClassCastException in Java (12 个回答) 已关闭 6 年前。 我已经编写了 java 到 Json 的代码,同时从页
这个问题在这里已经有了答案: ClassCastException java.lang.Long cannot be cast to clojure.lang.IFn (4 个答案) 关闭 6 年前
我在运行时遇到问题来编译这段代码,这给我一个错误,java.lang.Integer 无法转换为 Java.lang.Double。如果有人帮助我更正此代码,我将非常高兴 double x; pu
我是一名优秀的程序员,十分优秀!