通过TableLayout获取联系人列表时出现空指针异常

时间:2014-03-07 12:57:15

标签: java nullpointerexception

我试图通过表格布局获取联系人详细信息和图像,我在for循环中得到了空指针异常。任何建议将不胜感激......

代码如下......

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;
    }

2 个答案:

答案 0 :(得分:0)

问题是静态成员tmpList永远不会受到影响,因此在populateTable()中它是空的,因此在其中调用size()会返回NullPointerException。

有两种解决方案。要么影响onCreate()

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

getContacts()

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

您最终不再需要任何返回值。

答案 1 :(得分:0)

getContacts()的第一行:

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

正在创建一个名为tmpList的新局部变量,该变量隐藏了您为该类定义的字段tmpList

当您在tmpList内使用populateTable()时,您指的是该字段,但这将是null,因为它从未初始化; getContacts()中的代码正在修改本地对象。

要解决此问题,您有3个选项。将该行更改为:

tmpList = new ArrayList<PhoneAddressBookData>();

或者,在getContacts()结束时添加:

this.tmpList = tmpList;

或者,由于您从该方法返回tmpList,请将调用更改为:

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