我想通過表格佈局獲取聯繫人詳細信息和圖像,並且我在for循環中得到了空指針異常。任何建議將不勝感激...空指針獲取聯繫人時出現異常通過TableLayout列出
代碼是在這裏下面...
public class MainActivity extends Activity {
public static ArrayList<PhoneAddressBookData> tmpList;
TableLayout tableLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tableLayout = (TableLayout) findViewById(R.id.categoriesTable);
getContacts(MainActivity.this,"");
populateTable();
}
public void populateTable() {
tableLayout.removeAllViews();
for (int i = 0; i < tmpList.size(); i++) {
final TableRow row = new TableRow(MainActivity.this);
TableRow.LayoutParams rowLayout = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.MATCH_PARENT);
rowLayout.setMargins(0, 5, 0, 5);
row.setLayoutParams(rowLayout);
row.setId(i);
row.setClickable(true);
RelativeLayout parentLayout = new RelativeLayout(MainActivity.this);
RelativeLayout relativeLayout = new RelativeLayout(MainActivity.this);
relativeLayout.setBackgroundColor(color.black);
relativeLayout.setId(8);
// RelativeLayout.LayoutParams childLayout = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
// parentLayout.addView(relativeLayout, childLayout);
TextView nameTV = new TextView(MainActivity.this);
nameTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
nameTV.setId(1);
nameTV.setTextColor(Color.BLACK);
nameTV.setTextSize(17);
nameTV.setSingleLine(true);
nameTV.setEllipsize(TruncateAt.END);
nameTV.setText(tmpList.get(i).name);
final RelativeLayout waveLayout = new RelativeLayout(MainActivity.this);
waveLayout.setVisibility(View.GONE);
waveLayout.setId(5);
RelativeLayout.LayoutParams waveLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
//waveLayoutParams.addRule(RelativeLayout.BELOW,relativeLayout.getId());
parentLayout.addView(nameTV, waveLayoutParams);
row.addView(parentLayout);
tableLayout.addView(row);
row.setTag(i);
}
}
public static ArrayList<PhoneAddressBookData> getContacts(Context context,String selection){
ArrayList<PhoneAddressBookData> tmpList = new ArrayList<PhoneAddressBookData>();
String sel=ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" LIKE '%"+selection+"%'";
Cursor phones = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, sel,null, null);
while (phones.moveToNext()) {
String contactImageName=null;
String ContactID=null;
String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contactImageName=getGalleryPic(phoneNumber);
if(contactImageName==null)
ContactID = phones.getString(phones.getColumnIndex(BaseColumns._ID));
PhoneAddressBookData con = new PhoneAddressBookData(ContactID,name,phoneNumber,contactImageName);
tmpList.add(con);
}
phones.close();
return tmpList;
}
private static String getGalleryPic(String phoneNumber) {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
好的,但手機#現在沒有顯示。它正在爲名字工作。 –