报错: java.lang.NullPointerException: Attempt to read from field ‘android.widget.TextView com.example.demoone_personspace.MutiLayOutAdapter$Hq_deal_handler.txt_dName1’ on a null object reference
出现场景: deal_handler赋值后依然为Null,导致下方deal_handler.setText()报空指针异常。 deal_handler:用于处理一个ListView的列的处理类。
问题解决: 将getViewTypeCount()方法命名正确
原因分析: 在Android Studio中,未成功使用的方法或变量会显示灰色。
而在BaseAdapter中,有四个需要重写的方法,getViewypeCount()就是其中的一个,其内部程序会自动调用,但我们需要重写好对应的内容才行。
我则是因为没有重写好getViewTypeCount()方法,导致deal_hanler无法读取,无法赋值。(具体如何读取,谁来读取,调用,未看源码,不做分析)
要点牢记: 1:AS中找BUG时先观察有没有灰色的方法或者变量 2:再重写BaseAdapter时要注意按照正确的命名重写接口方法。
代码错误出现处:
public int getViewTypeCount(){
return 5;
}
if(deal != null ){
deal_handler.txt_dName1.setText(deal.getdName1());
deal_handler.txt_dName2.setText(deal.getdName2());
deal_handler.txt_dName3.setText(deal.getdName3());
deal_handler.txt_dName4.setText(deal.getdName4());
deal_handler.txt_dName5.setText(deal.getdName5());
deal_handler.txt_dId1.setText(deal.getdId1());
deal_handler.txt_dId2.setText(deal.getdId2());
deal_handler.txt_dId3.setText(deal.getdId3());
deal_handler.txt_dId4.setText(deal.getdId4());
deal_handler.txt_dId5.setText(deal.getdId5());
deal_handler.txt_dPrice1.setText(deal.getdPrice1());
deal_handler.txt_dPrice2.setText(deal.getdPrice2());
deal_handler.txt_dPrice3.setText(deal.getdPrice3());
deal_handler.txt_dPrice4.setText(deal.getdPrice4());
deal_handler.txt_dPrice5.setText(deal.getdPrice5());
deal_handler.txt_dDeal1.setText(deal.getdDeal1());
deal_handler.txt_dDeal2.setText(deal.getdDeal2());
deal_handler.txt_dDeal3.setText(deal.getdDeal3());
deal_handler.txt_dDeal4.setText(deal.getdDeal4());
deal_handler.txt_dDeal5.setText(deal.getdDeal5());
}
break;
|