我正在尝试将值填充到列表视图中。它工作正常。点击列表行,其描述活动将打开。我在填充值时遇到空指针异常。
ListView mylistview = null;
mylistview = (ListView) findViewById(R.id.list);
CustomAdapter listAdapter = new CustomAdapter(SearchActivityResult.this, 0, temp);
mylistview.setAdapter(listAdapter);
list=temp;
mylistview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
Intent i = new Intent(SearchActivity.this, DescriptionActivity.class);
Bundle b = new Bundle();
b.putSerializable("plant", list.get(pos));
i.putExtras(b);
startActivity(i);
然后收到如下可序列化对象:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle b = getIntent().getExtras();
plant = (Plant) b.getSerializable("plant");
setContentView(R.layout.description_activity);
setTitle("Flora Enroute");
View titleView = getWindow().findViewById(android.R.id.title);
if (titleView != null) {
ViewParent parent = titleView.getParent();
if (parent != null && (parent instanceof View)) {
View parentView = (View)parent;
parentView.setBackgroundColor(Color.parseColor("#99CC00"));
}
}
initViews();
populateValues();
}
private void populateValues() {
Picasso.with(this).load(plant.getimagepath()).error(R.drawable.plant_icon).resize(240,320).into(ivImage);
commonname.setText(plant.getcommonname());
scientificname.setText(plant.getscientificname());
planttype.setText(plant.getplanttype());
season.setText(plant.getseason());
growthhabit.setText(plant.getgrowthhabit());
duration.setText(plant.getduration());
leafshape.setText(plant.getleafshape());
flowershape.setText(plant.getflowershape());
flowercolor.setText(plant.getflowercolor());
lightexposure.setText(plant.getlightexposure());
growthrate.setText(plant.getgrowthrate());
waterreq.setText(plant.getwaterrequirement());
sid.setText(plant.getsid());
features.setText(plant.getfeatures());
}
private void initViews() {
commonname = (TextView) findViewById(R.id.commonname);
duration = (TextView) findViewById(R.id.duration);
features = (TextView) findViewById(R.id.features);
flowercolor = (TextView) findViewById(R.id.flowercolor);
flowershape = (TextView) findViewById(R.id.flowershape);
growthhabit = (TextView) findViewById(R.id.growthhabit);
growthrate = (TextView) findViewById(R.id.growthrate);
leafshape = (TextView) findViewById(R.id.leafshape);
lightexposure = (TextView) findViewById(R.id.lightexposure);
planttype = (TextView) findViewById(R.id.planttype);
scientificname = (TextView) findViewById(R.id.scientificname);
season = (TextView) findViewById(R.id.season);
sid = (TextView) findViewById(R.id.sid);
showMap = (TextView) findViewById(R.id.show_map);
ivImage = (ImageView) findViewById(R.id.iv_);
}
我在poulate值函数上得到空指针异常。当我单击任何列表行以开始其描述活动时,我得到空指针异常。
答案 0 :(得分:0)
在populateValues()
中,您要为各种文字视图设置文字。
所有其他观看次数均在initViews()
初始化。但您错过了初始化waterreq
所以你可能会在线获得NPE:
waterreq.setText(plant.getwaterrequirement());
请在initViews()
函数中初始化。
希望这有帮助。