2016-04-14 28 views
0

我想顯示我的手機中的聯繫人列表作爲列表。我將單個列表項存儲在字符串緩衝區中,但我無法將其轉換爲List類型。列表是我所做的一個類。NullPointerException在自定義ListView的setAdapter和ArrayList創建時的語法錯誤

list.java

public class list { 
    public StringBuffer item; 
    public list(StringBuffer title) { 
     super(); 
     this.item = title; 
    } 
    public StringBuffer getTitle() { 
     return item; 
    } 
    public void setTitle(StringBuffer title) { 
     this.item = title; 
    } 
    @Override 
    public String toString() { 
     return item + "\n" ; 
    } 
} 

listAdapter.java

public class listAdapter extends ArrayAdapter<list> { 

    Context context; 
    int layoutResourceId; 
    list data[] = null; 

    public listAdapter(Context context, int layoutResourceId, List<list> items) { 
     super(context, layoutResourceId, items); 
     this.layoutResourceId = layoutResourceId; 
     this.context = context; 
    } 

    /*private view holder class*/ 
    private class ViewHolder { 
     TextView txtTitle; 

    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder = null; 
     list list = getItem(position); 

     LayoutInflater mInflater = (LayoutInflater) context 
       .getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 
     if (convertView == null) { 
      convertView = mInflater.inflate(R.layout.list_item, null); 
      holder = new ViewHolder(); 
      holder.txtTitle = (TextView) convertView.findViewById(R.id.textView1); 
      convertView.setTag(holder); 
     } else 
      holder = (ViewHolder) convertView.getTag(); 

     holder.txtTitle.setText(list.getTitle()); 


     return convertView; 
    } 
} 

片段類

public class MainActivity extends Fragment { 
    private ArrayList<StringBuffer> conNumbers; 
    List<list> rowitems; 
    ListView listview; 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.allcontacts, container, false); 
     conNumbers = new ArrayList<>(); 
     listview=(ListView)getActivity().findViewById(R.id.list); 
     rowitems=new ArrayList<>(); 
     fetchContacts(); 
     return v; 
    } 

    public void fetchContacts() { 

     String phoneNumber = null; 
     String email = null; 

     Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI; 
     String _ID = ContactsContract.Contacts._ID; 
     String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME; 
     String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER; 

     Uri PhoneCONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; 
     String Phone_CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID; 
     String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER; 

     Uri EmailCONTENT_URI = ContactsContract.CommonDataKinds.Email.CONTENT_URI; 
     String EmailCONTACT_ID = ContactsContract.CommonDataKinds.Email.CONTACT_ID; 
     String DATA = ContactsContract.CommonDataKinds.Email.DATA; 

     StringBuffer output = new StringBuffer(); 

     ContentResolver contentResolver = getActivity().getContentResolver(); 

     Cursor cursor = contentResolver.query(CONTENT_URI, null, null, null, null); 

     // Loop for every contact in the phone 
     if (cursor.getCount() > 0) { 

      while (cursor.moveToNext()) { 

       String contact_id = cursor.getString(cursor.getColumnIndex(_ID)); 
       String name = cursor.getString(cursor.getColumnIndex(DISPLAY_NAME)); 

       int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(HAS_PHONE_NUMBER))); 

       if (hasPhoneNumber > 0) { 

        output.append("\n First Name:" + name); 

        // Query and loop for every phone number of the contact 
        Cursor phoneCursor = contentResolver.query(PhoneCONTENT_URI, null, Phone_CONTACT_ID + " = ?", new String[]{contact_id}, null); 

        while (phoneCursor.moveToNext()) { 
         phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER)); 
         output.append("\n Phone number:" + phoneNumber); 


        } 

        phoneCursor.close(); 

        // Query and loop for every email of the contact 
        Cursor emailCursor = contentResolver.query(EmailCONTENT_URI, null, EmailCONTACT_ID + " = ?", new String[]{contact_id}, null); 

        while (emailCursor.moveToNext()) { 

         email = emailCursor.getString(emailCursor.getColumnIndex(DATA)); 

         output.append("\nEmail:" + email); 


        } 

        emailCursor.close(); 
       } 

       output.append("\n"); 
       } 
      rowitems.add(output); *I get an error here saying add(packagename.list) cannot be applied to (java.lang.StringBuffer)* 


     } 

     listAdapter adapter=new listAdapter(getActivity(),R.layout.list_item,rowitems); 
     listview.setAdapter(adapter); 
} 

回答

1

rowitems需要list類的對象。

rowitems.add(new list(output)); 

您應該將list類重命名爲更有意義的類。類似於ContactDetailsList

更新

爲了解決NullPointerException而設置適配器,更改以下行,

listview = (ListView) getActivity().findViewById(R.id.list); 

listview = (ListView) v.findViewById(R.id.list); 

隨着getActivity().findViewById()你試圖找到屬於父視圖片段的活動。

但是你想在當前佈局R.layout.allcontacts中找到一個視圖。因此,在當前充氣版本v上使用findViewById

+0

好的,這工作!但現在它顯示一個空對象引用在我設置適配器的行 - java.lang.NullPointerException:嘗試調用空虛對象上的虛擬方法'void android.widget.ListView.setAdapter(android.widget.ListAdapter)'參考 – TeeKay

+0

你能幫我一下嗎?這將是一個很大的幫助。 – TeeKay

+0

@TeeKay查看更新的答案。 –

相關問題