2014-03-05 47 views
0

時,這是存儲新聞的一類新聞數組我的Java類,然後輸出它們在列表視圖NullPointerException異常實現定製行佈局

public class News extends Activity { 

List<news> myNews = new ArrayList<news>(); 



@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(com.example.makkosarka.R.layout.activity_news); 

    populateNewsList(); 
    populateListView(); 
} 

private void populateListView() { 
    ArrayAdapter<news> adapter = new MyListAdapter(); 
    ListView list = (ListView) findViewById(com.example.makkosarka.R.id.listview_news); 
    list.setAdapter(adapter); 

} 
public void populateNewsList() { 
      //tuka ja polnis listata 
        myNews.add(new news("title1", 1,"something1")); 
        myNews.add(new news("title2", 2,"something2")); 
        myNews.add(new news("title3", 3,"something3")); 

    } 

private class MyListAdapter extends ArrayAdapter<news>{ 
    public MyListAdapter(){ 
      super(News.this,com.example.makkosarka.R.layout.item_news, myNews); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

    //make sure that we have a View to work with   
    View row = convertView; 
    if (row == null){ 
      row = getLayoutInflater().inflate(com.example.makkosarka.R.layout.item_news, parent,false); 
     } 

    //Find the product to work with 
    news currentNews = myNews.get(position); 

    //Fill the view 

    TextView txtcena = (TextView)findViewById(com.example.makkosarka.R.id.item_txtCena); 

    TextView txtime = (TextView)findViewById(com.example.makkosarka.R.id.item_txtName); 

    txtime.setText((currentNews).getBody()); 
    txtcena.setText((currentNews).getTitle());  


      return row;    
    } 
} 

private class news{ 
    private String Title; 
    private int ImageID; 
    private String Body; 


    public news(String title, int imageID, String body){ 
      this.Title=title; 
      this.ImageID=imageID; 
      this.Body=body; 
    } 
    public String getTitle() { 
      return Title; 
    } 

    public int getImageID() { 
      return ImageID; 
    } 

    public String getBody() { 
      return Body; 
    }}} 

在該行的錯誤NullPointerException異常acour:

txtime.setText((currentNews).getBody()); 
    txtcena.setText((currentNews).getTitle()); 

這個代碼結構有什麼問題? 我正在實施自定義行佈局!

回答

4

更改爲

TextView txtcena = (TextView)row.findViewById(com.example.makkosarka.R.id.item_txtCena); 
TextView txtime = (TextView)row.findViewById(com.example.makkosarka.R.id.item_txtName); 

的觀點屬於膨脹佈局。可以考慮使用ViewHolder

class ViewHolder 
{ 
    TextView txtcena,txtime; 
} 

@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
    ViewHolder holder; 
    if (convertView == null){ 
      holder = new VieHolder(); 
      convertVIew = getLayoutInflater().inflate(com.example.makkosarka.R.layout.item_news, parent,false); 
      holder.txtcena = (TextView)convertView.findViewById(com.example.makkosarka.R.id.item_txtCena); 
      holder.txtime = (TextView)convertVIew.findViewById(com.example.makkosarka.R.id.item_txtName); 
      convertView.setTag(holder); 

     } else{ 
       holder = (ViewHolder)convertView.getTag(); 
     } 
     news currentNews = myNews.get(position); 
     holder.txtime.setText((currentNews).getBody()); 
     holder.txtcena.setText((currentNews).getTitle());  


      return convertView;    
    } 
+0

可以形容? –

+1

@Shayanpourvatan http://developer.android.com/training/improving-layouts/smooth-scrolling.html – Raghunandan

1

當您訪問另一個視圖項到ListView你會發現爲什麼使用`ViewHolder`從該視圖的ID,以便改變這樣

TextView txtcena = (TextView)row.findViewById(com.example.makkosarka.R.id.item_txtCena); 
TextView txtime = (TextView)row.findViewById(com.example.makkosarka.R.id.item_txtName); 
相關問題