【问题标题】:Null Pointer Exception in Receiving serializable object接收可序列化对象时出现空指针异常
【发布时间】:2014-09-03 06:21:25
【问题描述】:

我正在尝试将值填充到列表视图中。它工作正常。在单击列表行时,它的描述活动将打开。我在填充值时遇到空指针异常。

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_);

}

我在填充值函数上遇到空指针异常。当我单击任何列表行开始其描述活动时,我得到空指针异常。

【问题讨论】:

  • 请发布 logcat。并提及应用崩溃的行。
  • 在调用 populatevalues 函数时

标签: android serialization


【解决方案1】:

populateValues() 中,您正在为各种文本视图设置文本。

所有其他文本视图都在initViews() 中初始化。但是你错过了初始化waterreq

所以你可能会在线获得 NPE:

waterreq.setText(plant.getwaterrequirement());

请在initViews()函数中初始化。

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-09
    • 1970-01-01
    相关资源
    最近更新 更多