2014-03-07 117 views
0

我想通過表格佈局獲取聯繫人詳細信息和圖像,並且我在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; 
    } 

回答

0

的問題是,靜態成員tmpList永遠不會受到影響,所以在populateTable()它是空的,因此在調用size()它返回一個NullPointerException。

有2種解決方案。在onCreate()要麼影響:

tmpList = getContacts(MainActivity.this,""); 

或者在getContacts()

public static void getContacts(Context context, String selection) { 
    tmpList = new ArrayList<PhoneAddressBookData>(); 
    … 

,你最終不需要任何返回值了。

+0

好的,但手機#現在沒有顯示。它正在爲名字工作。 –

0

getContacts()第一行:

ArrayList<PhoneAddressBookData> tmpList = new ArrayList<PhoneAddressBookData>(); 

是創建一個名爲tmpList新的本地變量被隱藏了你的類定義的字段tmpList

當您在populateTable()中使用tmpList時,您指的是該字段,但由於它從未初始化過,因此它將爲空; getContacts()中的代碼正在修改本地對象。

要解決此問題,您有3個選項。無論是行更改爲:

tmpList = new ArrayList<PhoneAddressBookData>(); 

或者說,getContacts()加的末尾:

this.tmpList = tmpList; 

或者,因爲你是從該方法返回tmpList,改呼爲:

tmpList = getContacts(MainActivity.this,""); 
+0

tmpList = new ArrayList (); 它現在正在工作,但電話號碼現在不顯示。 。 。 –