2011-10-24 74 views
0

我想檢索從手機到我們自己的列表視圖的所有聯繫人,但代碼打開手機的聯繫人列表...這是我用於獲取聯繫人的類的代碼..我正在使用聯繫人按鈕開火事件,並獲得我的列表視圖中的聯繫人,但空行指針異常在標記爲*正在發生......
KINDLY幫助我。 由於事先如何在android中的列表視圖中獲取聯繫人列表?

 public class BusinessCardActivity extends Activity { 
      private static final int PICK_CONTACT_REQUEST = 1; 
      private final ContactAccessor mContactAccessor = ContactAccessor.getInstance(); 
      ImageButton contact_button; 
      @Override 
      public void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 

       //setContentView(R.layout.allbuttons); 
       LinearLayout rlayout=new LinearLayout(this); 
       rlayout.setOrientation(LinearLayout.VERTICAL); 
       //RelativeLayout relativeLayout=  (RelativeLayout)findViewById(R.layout.main); 

       //RelativeLayout relateLayout=(RelativeLayout)findViewById(R.layout.allbuttons); 
       LayoutInflater layoutInflater = getLayoutInflater(); 


       rlayout.addView(layoutInflater.inflate(R.layout.main,null)); 
       rlayout.addView(layoutInflater.inflate(R.layout.allbuttons,null)); 
       //pickContact(); 
       // Install a click handler on the Pick Contact button 

       contact_button=new ImageButton(getApplicationContext()); 
       *** contact_button = (ImageButton)findViewById(R.id.button_contacts); 

       contact_button.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         pickContact(); 
        } 
       }); 
       this.setContentView(rlayout); 
      } 


      protected void pickContact() { 
       startActivityForResult(mContactAccessor.getPickContactIntent(), PICK_CONTACT_REQUEST); 
      } 


      @Override 
      protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
       if (requestCode == PICK_CONTACT_REQUEST && resultCode == RESULT_OK) { 
        loadContactInfo(data.getData()); 
       } 
      } 


      private void loadContactInfo(Uri contactUri) { 


       AsyncTask<Uri, Void, ContactInfo> task = new AsyncTask<Uri, Void, ContactInfo>() { 

        @Override 
        protected ContactInfo doInBackground(Uri... uris) { 
         return mContactAccessor.loadContact(getContentResolver(), uris[0]); 
        } 

        @Override 
        protected void onPostExecute(ContactInfo result) { 
         bindView(result); 
        } 
       }; 

       task.execute(contactUri); 
      } 


      protected void bindView(ContactInfo contactInfo) { 
       TextView displayNameView = (TextView) findViewById(R.id.display_name_text_view); 
       displayNameView.setText(contactInfo.getDisplayName()); 

       TextView phoneNumberView = (TextView)findViewById(R.id.phone_number_text_view); 
       phoneNumberView.setText(contactInfo.getPhoneNumber()); 
      } 
     } 
    . 

回答

0

你需要調用setContentView()你打電話之前findViewbyId()否則你沒有一個佈局,找到

+0

該行的setContentView(視圖)被錯誤註釋out..i有運行該行的代碼,甚至與所有可能的版本,但相同的空指針異常,我得到了同一行..我想問一個問題,我已經列入我的TEXTVIEW列表視圖,但我發現它必須被添加到另一個佈局,它通過listadapter動態添加到textview ..你可以告訴我的方式或任何其他可能..感謝 –

+0

是R.id.button_contact如果你確實需要調用rlayout.findViewById(R.id.button_contacts) –

+0

還有什麼理由以編程方式構建佈局,你可以不使用佈局XML文件嗎? –

相關問題