2014-09-03 88 views
-1

我正試圖將值填充到列表視圖中。它工作正常。在點擊列表行時,其描述活動即將打開。我在填充值時收到空指針異常。接收可序列化對象中的空指針異常

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

請發佈logcat。並提及哪些應用程序崩潰的行。 – 2014-09-03 06:21:59

+0

在調用populatevalues函數 – user3598397 2014-09-03 06:27:15

回答

0

populateValues()中,您正在設置各種文字瀏覽的文本。

所有其他textviews在initViews()初始化。但是,你已經錯過了初始化waterreq

上線所以,你可能會得到NPE:

waterreq.setText(plant.getwaterrequirement()); 

initViews()函數初始化。

希望這會有所幫助。

+0

您是對的。謝謝。 – user3598397 2014-09-03 06:32:04