我的android项目有问题。我有一个列表,从数据库中获取数据并将此列表放在一起我有2个按钮,一个用于视图,另一个用于排除,非常类似于联系人列表。但是当列表已满并且我尝试滚动到结尾或开始时,我接收到ListAdapter类中的按钮的nullpointerexception,我无法理解正在发生的事情或如何解决。当您不滚动列表时,按钮会正常工作。抱歉我的英文。
我的ListFragment
public class ListFragment extends Fragment implements View.OnClickListener{
private Button btNovaBiometria;
private ListView lvBiometria;
private SQLiteDatabase database;
private BancoDeDados bancoDeDados;
private Context contexto;
private View viewPai;
private ListAdapter listAdapter;
public static ListFragment listFragment;
public static final String LIST_VIEW_TAG = "LIST_VIEW_TAG";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
viewPai = inflater.inflate(R.layout.fragment_list, container, false);
((PrincipalActivity) getActivity()).getSupportActionBar().setSubtitle("Lista de biometrias");
contexto = getActivity().getApplicationContext();
listFragment = this;
bancoDeDados = new BancoDeDados(contexto);
database = bancoDeDados.getWritableDatabase();
btNovaBiometria = (Button) viewPai.findViewById(R.id.bt_nova_biometria);
btNovaBiometria.setOnClickListener(this);
lvBiometria = (ListView) viewPai.findViewById(R.id.lv_biometria);
ArrayList<Biometria> biometrias = buscarTodasBiometrias();
listAdapter = new ListAdapter(contexto,biometrias, listFragment);
lvBiometria.setAdapter(listAdapter);
return viewPai;
}
public void onClick(View view){
if (view == btNovaBiometria){
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
getFragmentManager().beginTransaction().addToBackStack("Lista de biometrias").replace(R.id.principal, new BiometriaFragment(), ListFragment.LIST_VIEW_TAG).commit();
}
}
public ArrayList<Biometria> buscarTodasBiometrias(){
ArrayList<Biometria> biometrias = new ArrayList<Biometria>();
String sql = "SELECT _id, Tanque_id, qtd_total_animais, peso_liquido_total, media, data FROM Biometria; ";
Cursor cursor = database.rawQuery(sql, null);
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
Biometria biometria = new Biometria();
biometria.set_id(cursor.getLong(0));
biometria.setTanque_id(cursor.getInt(1));
biometria.setAnimaisTotal(cursor.getInt(2));
biometria.setPesoLiquidoTotal(cursor.getFloat(3));
biometria.setMedia(cursor.getFloat(4));
biometria.setData(cursor.getString(5));
biometrias.add(biometria);
}
cursor.close();
return biometrias;
}
}
我的ListViewHolder:
public class ListViewHolder {
ImageButton btVisualizar;
TextView tvItem;
ImageButton btExcluir;
}
我的ListAdapter:
public class ListAdapter extends BaseAdapter {
private Context contexto;
private ArrayList<Biometria> biometriaArrayList;
private SQLiteDatabase database;
private BancoDeDados bancoDeDados;
private ListFragment listFragment;
public ListAdapter(Context contexto, ArrayList<Biometria> biometriaArrayList, ListFragment listFragment) {
this.contexto = contexto;
this.biometriaArrayList = biometriaArrayList;
this.listFragment=listFragment;
}
@Override
public int getCount() {
return biometriaArrayList.size();
}
@Override
public Biometria getItem(int position) {
return biometriaArrayList.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, final View convertView, ViewGroup parent) {
final View row;
bancoDeDados = new BancoDeDados(contexto);
database = bancoDeDados.getWritableDatabase();
final ListViewHolder listViewHolder;
if(convertView == null){
LayoutInflater layoutInflater = (LayoutInflater) contexto.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.listview_item,parent,false);
listViewHolder = new ListViewHolder();
listViewHolder.btExcluir = (ImageButton) row.findViewById(R.id.bt_excluir);
listViewHolder.tvItem = (TextView) row.findViewById(R.id.tv_Item);
listViewHolder.btVisualizar = (ImageButton) row.findViewById(R.id.bt_visualizar);
}else {
row=convertView;
listViewHolder = (ListViewHolder) row.getTag();
}
final Biometria biometria = biometriaArrayList.get(position);
/**the nullpointer occurs exactly on that line to scroll the list view**/ listViewHolder.btExcluir.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(listFragment.getActivity());
builder.setTitle("Excluir");
builder.setMessage("Tem certeza que deseja excluir este item?")
.setPositiveButton("Sim", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
for (Biometria biometrias : biometriaArrayList) {
if (biometrias.get_id() == biometria.get_id()) {
try {
listFragment.getFragmentManager().beginTransaction().replace(R.id.principal, new ListFragment()).commit();
String sql = "DELETE FROM Biometria WHERE _id='" + biometria.get_id() + "'";
DatabaseUtils.longForQuery(database, sql, null);
} catch (SQLException ex) {
}
break;
}
}
Toast.makeText(contexto, "Voce excluiu o item", Toast.LENGTH_LONG).show();
return;
}
})
.setNegativeButton("Não", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
});
listViewHolder.btVisualizar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle bundle = new Bundle();
bundle.putSerializable("Biometria", biometria);
BiometriaResumoFragment fragobj= new BiometriaResumoFragment();
fragobj.setArguments(bundle);
listFragment.getFragmentManager().beginTransaction().addToBackStack("Lista de biometrias").replace(R.id.principal, fragobj).commit();
Toast.makeText(contexto, "Voce Visualizou o item", Toast.LENGTH_LONG).show();
}
});
listViewHolder.tvItem.setText(String.valueOf(getItem(position)));
return row;
}
}
错误日志:
03-30 16:03:00.821 21565-21565/br.com.agrtec.www.biometriamodulo E/AndroidRuntime: FATAL EXCEPTION: main
Process: br.com.agrtec.www.biometriamodulo, PID: 21565
java.lang.NullPointerException
at br.com.agrtec.www.biometriamodulo.ListAdapter.getView(ListAdapter.java:78)
at android.widget.AbsListView.obtainView(AbsListView.java:2255)
at android.widget.ListView.makeAndAddView(ListView.java:1790)
at android.widget.ListView.fillUp(ListView.java:725)
at android.widget.ListView.fillGap(ListView.java:664)
at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5143)
at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3243)
at android.widget.AbsListView.onTouchMove(AbsListView.java:3587)
at android.widget.AbsListView.onTouchEvent(AbsListView.java:3431)
at android.view.View.dispatchTouchEvent(View.java:7706)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2210)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1945)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2068)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1515)
at android.app.Activity.dispatchTouchEvent(Activity.java:2458)
at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2016)
at android.view.View.dispatchPointerEvent(View.java:7886)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3947)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3826)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3392)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3442)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3411)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3518)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3419)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3575)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3392)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3442)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3411)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3419)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3392)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5532)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5512)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5483)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5612)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Nativ
答案 0 :(得分:1)
您永远不会设置tag
的{{1}}。更新您的代码,以便在row
时调用row.setTag(listViewHolder)
。
convertView == null