0

我正在使用Parse.com服務器,我試圖從服務器獲取數據並將其放在具有4字段TextView的自定義列表中。我收到的數據很好,但我有一個問題,在適配器列表的第一個位置,我得到所有textView字段的空指針異常。在其他領域沒有問題。自定義列表視圖中的TextView爲空

這是我使用的代碼:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_coupon_list); 

    init(); 
} 

private void init(){ 
    couponListView = (ListView)findViewById(android.R.id.list); 
    couponList = new ArrayList<ParseObject>(); 
    query = new ParseQuery(COUPON_CLASS_NAME); 

    couponListView.setOnItemClickListener(this); 

    query.findInBackground(new FindCallback() {    
     @Override 
     public void done(List<ParseObject> list, ParseException e) { 
      if (e == null) { //objects retrieved well               
       couponList.addAll(list);               
      } 
      else { 
       toaster("problem find coupons"); 
      } 

      MyAdapter adapter = new MyAdapter(
        getApplicationContext(), 
        android.R.layout.simple_list_item_1, 
        R.id.tv_coupoun_content, 
        couponList); 

      setListAdapter(adapter); 

     } 
    });      
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.activity_coupon_list, menu); 
    return true; 
} 

@Override 
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {} 

public class MyAdapter extends ArrayAdapter<ParseObject> { 

    public MyAdapter(Context context, int resource, int textViewResourceId, ArrayList<ParseObject> couponList) { 
     super(context, resource, textViewResourceId, couponList); 
    } 

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

     LayoutInflater lf = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View row = lf.inflate(R.layout.coupon_row, parent, false);          

     tvCouponContent = (TextView)findViewById(R.id.tv_coupoun_content); 
     tvBusinessName = (TextView)findViewById(R.id.tv_business_name); 
     tvBusinessStreet = (TextView)findViewById(R.id.tv_street); 
     tvBusinessCity = (TextView)findViewById(R.id.tv_city); 

     //in position 0 the log below is true for all in the rest its false 


     Log.d("Shalom","shalom - " + Boolean.toString(tvCouponContent == null)+" "+ 
       Boolean.toString(tvBusinessName == null)+" "+ 
       Boolean.toString(tvBusinessStreet == null)+" "+ 
       Boolean.toString(tvBusinessCity == null)+" "+ 
       Integer.toString(position)); 

     /*tvCouponContent.setText(coupons.getString(COUPON_CONTENT));   
     tvBusinessName.setText(coupons.getString(COUPON_BUSINESS_NAME)); 
     tvBusinessStreet.setText(coupons.getString(COUPON_STREET)); 
     tvBusinessCity.setText(coupons.getString(COUPON_CITY));*/ 

     return row; 
    } 
} 

回答

2

適配器構造函數是錯誤的,使用下面的構造函數:

public MyAdapter(Context context, ArrayList<ParseObject> couponList) { 
    super(context, R.layout.coupon_row, couponList); 
} 

爲實例化它:

MyAdapter adapter = new MyAdapter(getApplicationContext(),couponList); 

爲將項目充氣到列表視圖中,您必須使用充氣對象:

View row = lf.inflate(R.layout.coupon_row, parent, false); 

tvCouponContent = (TextView)findViewById(R.id.tv_coupoun_content); //BAD 

tvCouponContent = (TextView)row.findViewById(R.id.tv_coupoun_content);//GOOD 

你應該使用View持有人模式:http://www.jmanzano.es/blog/?p=166

+0

我來試試TNX你的答案 –

+0

感謝的,你是偉大的!只是補充說:row.findViewById幫助 –

+0

請標記此答案爲接受。謝謝! :) – ClarkXP