1

電話から自分のリストビューにすべての連絡先を取得したいのですが、コードは電話の連絡先リストを開いています...これは連絡先を取得するために使用しているクラスのコードです..連絡先ボタンを使用してイベントを実行し、リストビューで連絡先を取得しますが、 *でマークされた行でnullポインタ例外が発生しています...
親切に助けてください。前もって感謝します

        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());
            }
        }
    .
4

1 に答える 1

1

setContentView()電話をかける前に電話をかける必要がありfindViewbyId()ます。そうしないと、ビューを見つけるためのレイアウトがありません。

于 2011-10-24T15:50:37.387 に答える